From f31fcf2d1fa7cf4410a455ebf86b50ed5b9fe5f9 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Sat, 14 Mar 2020 11:54:37 +0530 Subject: [PATCH] Add ranger plugin --- .gitignore | 111 +++++++++++++++++++++++++++++++++++++++++++++++ ranger_zoxide.py | 38 ++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 ranger_zoxide.py diff --git a/.gitignore b/.gitignore index f7e1b1b..1c137f9 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,114 @@ **/*.rs.bk # End of https://www.gitignore.io/api/rust + + +# Created by https://www.gitignore.io/api/python +# Edit at https://www.gitignore.io/?templates=python + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# Mr Developer +.mr.developer.cfg +.project +.pydevproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# End of https://www.gitignore.io/api/python diff --git a/ranger_zoxide.py b/ranger_zoxide.py new file mode 100644 index 0000000..a19cd13 --- /dev/null +++ b/ranger_zoxide.py @@ -0,0 +1,38 @@ +import ranger.api +import ranger.core.fm +import ranger.ext.signals +import subprocess + +hook_init_old = ranger.api.hook_init + + +def hook_init(fm: ranger.core.fm.FM): + def zoxide_add(signal: ranger.ext.signals.Signal): + path = signal.new.path + process = subprocess.Popen(["zoxide", "add", path]) + process.wait() + + fm.signal_bind("cd", zoxide_add) + return hook_init_old(fm) + + +ranger.api.hook_init = hook_init + + +class z(ranger.api.commands.Command): + def execute(self): + try: + output = subprocess.check_output(["zoxide", "query"] + self.args[1:]) + output = output.decode("utf-8") + + query_prefix = "query:" + + if output.startswith(query_prefix): + directory = output[len(query_prefix) :].strip() + self.fm.cd(directory) + self.fm.notify(directory) + else: + self.fm.notify("no match found", bad=True) + + except Exception as e: + self.fm.notify(e, bad=True)