def commit(self): bufNum = vim.current.buffer.number Shell.run('GIT_EDITOR=false git commit') vim.command('tabe {}'.format(self.cur_repo.commit_edit_msg)) vim.command('setlocal bufhidden=wipe') vim.command( 'autocmd bufunload <buffer> :py3 netrGit.post_commit({})'.format( bufNum))
def view_pdf(self): if not Shell.isinPATH('pdftoppm'): self.view_mime() else: if self.path not in self.tempfile_cache: fname = tempfile.mkstemp()[1] Shell.touch(fname) self.tempfile_cache[self.path] = fname fname = self.tempfile_cache[self.path] Shell.run(f'pdftoppm -png -f 1 -singlefile "{self.path}" {fname}') self.path = self.tempfile_cache[self.path] + '.png' self.view_image()
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 run_cmd(self, cmd): if not self.rcd_started: Vim.AsyncRun( f'rclone {self._flags} rcd --rc-no-auth --rc-addr=localhost:{self.rclone_rcd_port}' ) self.rcd_started = True # Ensure the server running before executing the next command. time.sleep(.1) return json.loads( Shell.run( f'rclone {self._flags} rc --rc-addr=localhost:{self.rclone_rcd_port} {cmd}' ))
def __init__(self, path): self.rules = [] if not os.path.isfile(path): Shell.cp(os.path.join(config_dir, 'rifle.conf'), path) with open(path, 'r') as f: for i, line in enumerate(f): try: # remove things after the first # (including) line = line[:line.index('#')] except ValueError: pass line = line.strip() if len(line) == 0: continue sp = line.split('=') if len(sp) != 2: Vim.ErrorMsg( 'invalid rule: rifle.conf line {}. There should be one' ' and only one "=" for each line'.format(i + 1)) continue tests = [] for test in sp[0].strip().split(','): testSp = [e for e in test.split(' ') if e != ''] tests.append(globals()[testSp[0]](*testSp[1:])) command = sp[1].strip() # simple case, used specify only the command # For sophisicated command like bash -c "command {}" # user should add '{}' themselves if '{}' not in command: command = command + ' {}' self.rules.append((tests, command))
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 sync(self, lpath, direction): src, dst = self.sync_src_dst(lpath, direction) Shell.run(f'rclone {self._flags} sync "{src}" "{dst}"')
def mkdir(self, name): Shell.mkdir(name)
def touch(self, name): Shell.touch(name)
def rename(self, src, dst): Shell.run( f'rclone {self._flags} moveto "{self.rpath(src)}" "{self.rpath(dst)}"' ) super(Rclone, self).rename(src, dst)
def ensure_remote_downloaded(self, lpath): if os.stat(lpath).st_size == 0: src, dst = self.sync_src_dst(lpath, Rclone.SyncDirection.DOWN) Shell.run( f'rclone {self._flags} copyto --tpslimit=10 "{src}" "{dst}"')
def run_cmd(self, cmd): return Shell.run('git -C {} {}'.format(self.path, cmd))
def __call__(self, fname): return Shell.isinPATH(self.args[0])