mirror of
https://github.com/jchook/ranger-zoxide.git
synced 2025-12-26 18:34:56 +08:00
commit
363df97af3
1 changed files with 13 additions and 17 deletions
30
__init__.py
30
__init__.py
|
|
@ -2,15 +2,13 @@ import os.path
|
|||
import ranger.api
|
||||
import ranger.core.fm
|
||||
import ranger.ext.signals
|
||||
from subprocess import Popen, PIPE
|
||||
from subprocess import Popen, PIPE, run
|
||||
|
||||
hook_init_prev = ranger.api.hook_init
|
||||
|
||||
def hook_init(fm):
|
||||
def zoxide_add(signal):
|
||||
path = signal.new.path
|
||||
process = Popen(["zoxide", "add", path])
|
||||
process.wait()
|
||||
Popen(["zoxide", "add", signal.new.path])
|
||||
|
||||
fm.signal_bind("cd", zoxide_add)
|
||||
fm.commands.alias("zi", "z -i")
|
||||
|
|
@ -35,22 +33,20 @@ class z(ranger.api.commands.Command):
|
|||
|
||||
def query(self, args):
|
||||
try:
|
||||
p = self.fm.execute_command("zoxide query {}".format(" ".join(self.args[1:])),
|
||||
stdout=PIPE
|
||||
)
|
||||
stdout, stderr = p.communicate()
|
||||
zoxide = self.fm.execute_command(f"zoxide query {' '.join(self.args[1:])}",
|
||||
stdout=PIPE
|
||||
)
|
||||
stdout, stderr = zoxide.communicate()
|
||||
|
||||
if not stdout:
|
||||
return None
|
||||
|
||||
if p.returncode == 0:
|
||||
if zoxide.returncode == 0:
|
||||
output = stdout.decode("utf-8").strip()
|
||||
if output:
|
||||
return output.splitlines()
|
||||
else:
|
||||
self.fm.notify("zoxide exited with status {}".format(p.returncode), bad=True)
|
||||
return output.splitlines()
|
||||
elif zoxide.returncode == 1: # nothing found
|
||||
return None
|
||||
elif zoxide.returncode == 130: # user cancelled
|
||||
return None
|
||||
else:
|
||||
output = stderr.decode("utf-8").strip() or "zoxide: unexpected error"
|
||||
output = stderr.decode("utf-8").strip() or f"zoxide: unexpected error (exit code {zoxide.returncode})"
|
||||
self.fm.notify(output, bad=True)
|
||||
except Exception as e:
|
||||
self.fm.notify(e, bad=True)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue