ranger: fzf integration

This commit is contained in:
Primrose 2025-02-20 08:44:44 +01:00
parent 4f0bd9c9e9
commit 645403156c
Signed by: primrose
GPG key ID: 4E887A4CA9714ADA
3 changed files with 45 additions and 4 deletions

View file

@ -0,0 +1,32 @@
# source: https://github.com/gotbletu/shownotes/blob/master/ranger_file_locate_fzf.md
from ranger.api.commands import Command
import os
class fzf_select(Command):
"""
:fzf_select
Find a file using fzf.
With a prefix argument select only directories.
See: https://github.com/junegunn/fzf
"""
def execute(self):
import subprocess
if self.quantifier:
# match only directories
command="fd . ~/Documents --type d | fzf"
else:
# match files and directories
command="fd . ~/Documents | fzf"
fzf = self.fm.execute_command(command, stdout=subprocess.PIPE)
stdout, stderr = fzf.communicate()
if fzf.returncode == 0:
fzf_file = os.path.abspath(stdout.decode('utf-8').rstrip('\n'))
if os.path.isdir(fzf_file):
self.fm.cd(fzf_file)
else:
self.fm.select_file(fzf_file)