99 lines
3.5 KiB
Python
99 lines
3.5 KiB
Python
# This is a sample commands.py. You can add your own commands here.
|
|
#
|
|
# Please refer to commands_full.py for all the default commands and a complete
|
|
# documentation. Do NOT add them all here, or you may end up with defunct
|
|
# commands when upgrading ranger.
|
|
|
|
# A simple command for demonstration purposes follows.
|
|
# -----------------------------------------------------------------------------
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
# You can import any python module as needed.
|
|
import os
|
|
import subprocess
|
|
|
|
# You always need to import ranger.api.commands here to get the Command class:
|
|
from ranger.api.commands import Command
|
|
from ranger.ext.img_display import ImageDisplayer, register_image_displayer
|
|
|
|
|
|
class show_files_in_finder(Command):
|
|
"""
|
|
:show_files_in_finder
|
|
|
|
Present selected files in finder
|
|
"""
|
|
|
|
def execute(self):
|
|
for f in self.fm.thistab.get_selection():
|
|
self.fm.run('open "%s"' % f.path)
|
|
|
|
|
|
class fzf_select(Command):
|
|
"""
|
|
:fzf_select
|
|
Find a file using fzf.
|
|
With a prefix argument to select only directories.
|
|
|
|
See: https://github.com/junegunn/fzf
|
|
"""
|
|
|
|
def execute(self):
|
|
import subprocess
|
|
import os
|
|
from ranger.ext.get_executables import get_executables
|
|
|
|
if 'fzf' not in get_executables():
|
|
self.fm.notify('Could not find fzf in the PATH.', bad=True)
|
|
return
|
|
|
|
fd = None
|
|
if 'fdfind' in get_executables():
|
|
fd = 'fdfind'
|
|
elif 'fd' in get_executables():
|
|
fd = 'fd'
|
|
|
|
if fd is not None:
|
|
hidden = ('--hidden' if self.fm.settings.show_hidden else '')
|
|
exclude = "--no-ignore-vcs --exclude '.git' --exclude '*.py[co]' --exclude '__pycache__'"
|
|
only_directories = ('--type directory' if self.quantifier else '')
|
|
fzf_default_command = '{} --follow {} {} {} --color=always'.format(
|
|
fd, hidden, exclude, only_directories
|
|
)
|
|
else:
|
|
hidden = ('-false' if self.fm.settings.show_hidden else r"-path '*/\.*' -prune")
|
|
exclude = r"\( -name '\.git' -o -iname '\.*py[co]' -o -fstype 'dev' -o -fstype 'proc' \) -prune"
|
|
only_directories = ('-type d' if self.quantifier else '')
|
|
fzf_default_command = 'find -L . -mindepth 1 {} -o {} -o {} -print | cut -b3-'.format(
|
|
hidden, exclude, only_directories
|
|
)
|
|
|
|
env = os.environ.copy()
|
|
env['SHELL'] = "bash"
|
|
env['FZF_DEFAULT_COMMAND'] = fzf_default_command
|
|
env['FZF_DEFAULT_OPTS'] = '--height=40% --layout=reverse --ansi --preview="{}"'.format('''
|
|
(
|
|
batcat --color=always {} ||
|
|
bat --color=always {} ||
|
|
cat {} ||
|
|
tree -ahpCL 3 -I '.git' -I '*.py[co]' -I '__pycache__' {}
|
|
) 2>/dev/null | head -n 100
|
|
''')
|
|
|
|
fzf = self.fm.execute_command('fzf --no-multi', env=env,
|
|
universal_newlines=True, stdout=subprocess.PIPE)
|
|
stdout, _ = fzf.communicate()
|
|
if fzf.returncode == 0:
|
|
selected = os.path.abspath(stdout.strip())
|
|
if os.path.isdir(selected):
|
|
self.fm.cd(selected)
|
|
else:
|
|
self.fm.select_file(selected)
|
|
|
|
@register_image_displayer("wez")
|
|
class WezImageDisplayer(ImageDisplayer):
|
|
def draw(self, path, start_x, start_y, width, height):
|
|
path = os.path.abspath(path)
|
|
subprocess.call(['wezterm', 'imgcat', '--width', str(width), '--height', str(height), path])
|