Beispiel #1
0
def _cd(view: View, defx: Defx, context: Context) -> None:
    """
    Change the current directory.
    """
    source_name = defx._source.name
    is_parent = context.args and context.args[0] == '..'
    prev_cwd = Path(defx._cwd)

    if is_parent:
        path = prev_cwd.parent
    else:
        if context.args:
            if len(context.args) > 1:
                source_name = context.args[0]
                path = Path(context.args[1])
            else:
                path = Path(context.args[0])
        else:
            path = Path.home()
        path = prev_cwd.joinpath(path)
        if not readable(path):
            error(view._vim, f'{path} is invalid.')
        path = path.resolve()
        if source_name == 'file' and not path.is_dir():
            error(view._vim, f'{path} is invalid.')
            return

    view.cd(defx, source_name, str(path), context.cursor)
    if is_parent:
        view.search_file(prev_cwd, defx._index)
Beispiel #2
0
 def get(self, context: Context, candidate: typing.Dict[str,
                                                        typing.Any]) -> str:
     path = candidate['action__path']
     if not readable(path):
         return str(' ' * self._length)
     return time.strftime(self.vars['format'],
                          time.localtime(path.stat().st_mtime))
Beispiel #3
0
 def get(self, context: Context, candidate: typing.Dict[str,
                                                        typing.Any]) -> str:
     path = candidate['action__path']
     if not readable(path) or path.is_dir():
         return ' ' * 8
     size = self._get_size(path.stat().st_size)
     return '{:>5s}{:>3s}'.format(size[0], size[1])
Beispiel #4
0
    def gather_candidates(
            self, context: Context,
            path: Path) -> typing.List[typing.Dict[str, typing.Any]]:
        if not readable(path):
            error(self.vim, f'"{path}" is not readable file.')
            return []

        if path.is_dir():
            # Fallback to file source
            return File(self.vim).gather_candidates(context, path)

        candidates = []
        with path.open() as f:
            for line in f:
                entry = Path(line.rstrip('\n'))
                if not entry.exists():
                    continue
                candidates.append({
                    'word':
                    str(entry) +
                    ('/' if safe_call(entry.is_dir, False) else ''),
                    'is_directory':
                    safe_call(entry.is_dir, False),
                    'action__path':
                    entry,
                })
        return candidates
Beispiel #5
0
 def gather_candidates(
         self, context: Context,
         path: Path) -> typing.List[typing.Dict[str, typing.Any]]:
     candidates = []
     if not readable(path) or not path.is_dir():
         error(self.vim, f'"{path}" is not readable directory.')
         return []
     try:
         for entry in path.iterdir():
             candidates.append({
                 'word':
                 entry.name.replace('\n', '\\n') +
                 ('/' if safe_call(entry.is_dir, False) else ''),
                 'is_directory':
                 safe_call(entry.is_dir, False),
                 'action__path':
                 entry,
             })
         if context.show_parent:
             candidates.append({
                 'word': '../',
                 'is_directory': True,
                 'action__path': path.resolve().parent,
             })
     except OSError:
         pass
     return candidates
Beispiel #6
0
 def get_with_highlights(
         self, context: Context,
         candidate: Candidate) -> typing.Tuple[str, Highlights]:
     path = candidate['action__path']
     if not readable(path):
         return (str(' ' * self._length), [])
     text = time.strftime(self.vars['format'],
                          time.localtime(path.stat().st_mtime))
     return (text, [(self.highlight_name, self.start, self._length)])
Beispiel #7
0
 def get_with_highlights(
     self, context: Context, candidate: Candidate
 ) -> typing.Tuple[str, Highlights]:
     path = candidate['action__path']
     if not readable(path) or path.is_dir():
         return (' ' * self._length, [])
     size = self._get_size(path.stat().st_size)
     text = '{:>6s}{:>3s}'.format(size[0], size[1])
     return (text, [(self.highlight_name, self.start, self._length)])
Beispiel #8
0
    def _init_candidates(self) -> None:
        self._candidates = []
        for defx in self._defxs:
            root = defx.get_root_candidate()
            root_path = root['action__path']
            defx._mtime = (root_path.stat().st_mtime if readable(root_path)
                           and root_path.is_dir() else -1)

            candidates = [root]
            candidates += defx.tree_candidates(
                defx._cwd, 0, self._context.auto_recursive_level)
            for candidate in candidates:
                candidate['_defx_index'] = defx._index
            self._candidates += candidates
Beispiel #9
0
def _cd(view: View, defx: Defx, context: Context) -> None:
    """
    Change the current directory.
    """
    path = Path(context.args[0]) if context.args else Path.home()
    path = Path(defx._cwd).joinpath(path).resolve()
    if not readable(path) or not path.is_dir():
        error(view._vim, f'{path} is not readable directory')
        return

    prev_cwd = defx._cwd
    view.cd(defx, str(path), context.cursor)
    if context.args and context.args[0] == '..':
        view.search_file(Path(prev_cwd), defx._index)
Beispiel #10
0
 def gather_candidates(
         self, context: Context, path: Path
 ) -> typing.List[typing.Dict[str, typing.Any]]:
     candidates = []
     if not readable(path) or not path.is_dir():
         error(self.vim, f'"{path}" is not readable directory.')
         return []
     for entry in path.iterdir():
         candidates.append({
             'word': entry.name + ('/' if safe_call(entry.is_dir, False)
                                   else ''),
             'is_directory': safe_call(entry.is_dir, False),
             'action__path': entry,
         })
     return candidates
Beispiel #11
0
    def restore_previous_buffer(self, bufnr: int) -> None:
        if (not self._vim.call('buflisted', bufnr)
                or self._vim.call('win_getid') != self._winid):
            return

        # Note: Convert to full path to prevent error
        # "1" matches "123" buffer
        prev_bufname = self._vim.call('fnamemodify',
                                      self._vim.call('bufname', bufnr), ':p')
        path_prev = Path(prev_bufname)
        if (not self._vim.call('buflisted', prev_bufname)
                or not readable(path_prev) or path_prev.is_dir()):
            return

        self._vim.call('setreg', '#',
                       self._vim.call('fnameescape', prev_bufname))
Beispiel #12
0
def _time(
    candidates: typing.List[typing.Dict[str, typing.Any]]
) -> typing.List[typing.Dict[str, typing.Any]]:
    return sorted(candidates,
                  key=(lambda x: int(x['action__path'].stat().st_mtime)
                       if readable(x['action__path']) else 0))
Beispiel #13
0
def _time(candidate: typing.Dict[str, typing.Any]) -> typing.Any:
    return (int(candidate['action__path'].stat().st_mtime)
            if readable(candidate['action__path']) else 0)
Beispiel #14
0
 def is_readable(self, path: Path) -> bool:
     return readable(path)
Beispiel #15
0
def _size(
    candidates: typing.List[typing.Dict[str, typing.Any]]
) -> typing.List[typing.Dict[str, typing.Any]]:
    return sorted(candidates,
                  key=(lambda x: x['action__path'].stat().st_size
                       if readable(x['action__path']) else -1))