def init(self, root_dir, remote_remap): self._flags = Vim.Var("_NETRRcloneFlags", default="") self.rclone_rcd_port = Vim.Var('NETRcloneRcdPort') if root_dir[-1] == '/': root_dir = root_dir[:-1] self.root_dir = root_dir self.rplen = len(root_dir) + 1 self.rcd_started = False for remote, root in remote_remap.items(): if root[-1] != '/': remote_remap[remote] += '/' self.remote_remap = remote_remap Shell.mkdir(root_dir)
def list_remotes_in_vim_buffer(self): if not Shell.isinPATH('rclone'): self.install_rclone() remotes = set(self.cmd_listremotes()) local_remotes = set(super(Rclone, self).ls(self.root_dir)) for remote in remotes.difference(local_remotes): Shell.mkdir(os.path.join(self.root_dir, remote)) for remote in local_remotes.difference(remotes): Shell.rm(os.path.join(self.root_dir, remote)) if len(remotes) > 0: Vim.command(f'NETRTabdrop {self.root_dir}') else: Vim.ErrorMsg( "There's no remote now. Run 'rclone config' in a terminal to " "setup remotes and restart vim again.")
def view_image(self): try: import ueberzug except ModuleNotFoundError: Vim.ErrorMsg('Please install ueberzug for image preview') self.view_mime() return path = self.path if self.path.endswith('gif') and Shell.isinPATH('convert'): if self.path not in self.tempfile_cache: dir = tempfile.TemporaryDirectory().name Shell.mkdir(dir) self.tempfile_cache[self.path] = dir dir = self.tempfile_cache[self.path] path = dir Shell.run(f'convert -deconstruct "{self.path}" {dir}/a.png') Vim.AsyncRun(f'{util.GenNetRangerScriptCmd("image_preview")}\ "{path}" {self.total_width} {self.preview_width}', term=True, termopencmd='') Vim.command('setlocal nocursorline')
def install_rclone(self): import platform import zipfile rclone_dir = Vim.UserInput( 'Rclone not in PATH. Install it at (modify/enter)', os.path.expanduser('~/rclone')) Shell.mkdir(rclone_dir) system = platform.system().lower() processor = 'amd64' if '386' in platform.processor(): processor = '386' else: # Should support arm?? pass url = f'https://downloads.rclone.org/rclone-current-{system}-{processor}.zip' zip_fname = os.path.join(rclone_dir, 'rclone.zip') Shell.urldownload(url, zip_fname) zip_ref = zipfile.ZipFile(zip_fname, 'r') zip_ref.extractall(rclone_dir) for entry in zip_ref.NameToInfo: if entry.endswith('rclone'): Shell.cp(os.path.join(rclone_dir, entry), rclone_dir) Shell.chmod(os.path.join(rclone_dir, 'rclone'), 755) zip_ref.close() os.remove(zip_fname) shellrc = Vim.UserInput( 'Update PATH in (leave blank to set manually later)', Shell.shellrc()) if len(shellrc) > 0: with open(shellrc, 'a') as f: f.write(f'PATH={rclone_dir}:$PATH\n') os.environ['PATH'] += ':' + rclone_dir
def ls(self, dirname, cheap_remote_ls=False): if not cheap_remote_ls and len(dirname) > len(self.root_dir): local_files = set([ name for name in Shell.run(f'ls -p "{dirname}"').split('\n') if len(name) > 0 ]) remote_files = set([ name for name in Shell.run( f'rclone {self._flags} lsf "{self.rpath(dirname)}"').split( '\n') if len(name) > 0 ]) for name in remote_files.difference(local_files): if name[-1] == '/': Shell.mkdir(os.path.join(dirname, name)) else: Shell.touch(os.path.join(dirname, name)) for name in local_files.difference(remote_files): # TODO use Vim.AsyncRun instead Shell.run_async(f'rclone {self._flags} copyto --tpslimit=10 \ "{os.path.join(dirname, name)}" \ "{os.path.join(self.rpath(dirname), name)}"') return super(Rclone, self).ls(dirname)
def mkdir(self, name): Shell.mkdir(name)