diff options
author | Silvio Rhatto <rhatto@riseup.net> | 2025-01-04 22:58:42 -0300 |
---|---|---|
committer | Silvio Rhatto <rhatto@riseup.net> | 2025-01-04 22:58:42 -0300 |
commit | c872b2b5bc6a6e7af28d6443c7ed73eaa5166a31 (patch) | |
tree | bf47dbffa59a54c4f3629cac891d386cb0ec6970 /packages | |
parent | dcae1cb169e10b7408e89230003e1000c45467dd (diff) | |
download | finder-c872b2b5bc6a6e7af28d6443c7ed73eaa5166a31.tar.gz finder-c872b2b5bc6a6e7af28d6443c7ed73eaa5166a31.tar.bz2 |
Feat: plugin: opener: rifle
Diffstat (limited to 'packages')
-rw-r--r-- | packages/finder/main.py | 61 | ||||
-rw-r--r-- | packages/finder/plugin/opener/file/rifle.py | 66 | ||||
-rw-r--r-- | packages/finder/plugin/opener/file/xdg.py | 7 |
3 files changed, 123 insertions, 11 deletions
diff --git a/packages/finder/main.py b/packages/finder/main.py index 0d99689..9939254 100644 --- a/packages/finder/main.py +++ b/packages/finder/main.py @@ -47,6 +47,7 @@ except ImportError: from .plugin.searcher.file.name import FinderPluginFileNameSearcher from .plugin.searcher.file.line import FinderPluginFileLineSearcher from .plugin.opener.file.xdg import FinderPluginFileXdgOpener +from .plugin.opener.file.rifle import FinderPluginFileRifleOpener class FinderMain(): def accept(self, buff): @@ -75,19 +76,26 @@ class FinderMain(): # Update the toolbar self.state['loading'] = False - def open(self): + def options(self): + path = self.path if os.path.isdir(self.path) else os.path.dirname(self.path) + item = self.list_buffer.document.current_line + + return self.opener.options(os.path.join(path, item)) + + def open(self, option = 1): # Heuristics: if path is a file, folder is the dirname of that file # so right now file lists need to be put in the same folder of the # files it lists. path = self.path if os.path.isdir(self.path) else os.path.dirname(self.path) item = self.list_buffer.document.current_line - self.opener.open(os.path.join(path, item)) + self.opener.open(os.path.join(path, item), option) - def pager(self): - "Currently an alias to open()" + def view(self): + path = self.path if os.path.isdir(self.path) else os.path.dirname(self.path) + item = self.list_buffer.document.current_line - return self.open() + self.viewer.open(os.path.join(path, item)) def clear_list_buffer(self): self.list_buffer.set_document(Document(''), bypass_readonly=True) @@ -124,15 +132,16 @@ class FinderMain(): async def _(event): self.state['loading'] = True + self.app.invalidate() refresh = asyncio.create_task(self.refresh()) @self.keybindings.add("enter", filter = has_focus(self.list_buffer)) def _(event): - self.open() + self.view() @self.keybindings.add("right", filter = has_focus(self.list_buffer)) def _(event): - self.pager() + self.view() @self.keybindings.add("q", filter = has_focus(self.list_buffer)) def _(event): @@ -140,6 +149,30 @@ class FinderMain(): self.finish() + @self.keybindings.add("f1") + def _(event): + "Open with option 1" + + self.open(0) + + @self.keybindings.add("f2") + def _(event): + "Open with option 2" + + self.open(1) + + @self.keybindings.add("f3") + def _(event): + "Open with option 3" + + self.open(2) + + @self.keybindings.add("f4") + def _(event): + "Open with option 4" + + self.open(3) + def theme(self): self.style = Style.from_dict({ "output-field" : "#888888", @@ -152,9 +185,16 @@ class FinderMain(): if self.state['loading']: return "Loading...." - lines = self.list_buffer.document.line_count + #selected = self.list_buffer.document.current_line + lines = self.list_buffer.document.line_count + options = self.options() + options_status = "" + + for option_list in options: + options_status += '[F' + str(option_list[1]+1) + ': ' + str(option_list[0]) + '] ' - return f"Results: {lines}" + #return f"Results: {lines}; selected: {selected}" + return f"Results: {lines} | Open selected with: {options_status}[ENTER: default]" def get_statusbar_right(self): #return "[Tab] change focus [Enter] Select" @@ -230,7 +270,8 @@ class FinderMain(): else: pass - self.opener = FinderPluginFileXdgOpener() + self.viewer = FinderPluginFileXdgOpener() + self.opener = FinderPluginFileRifleOpener() async def main(self): # UI diff --git a/packages/finder/plugin/opener/file/rifle.py b/packages/finder/plugin/opener/file/rifle.py new file mode 100644 index 0000000..71c0144 --- /dev/null +++ b/packages/finder/plugin/opener/file/rifle.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Finder searcher and browser REPL. +# +# Copyright (C) 2025 Silvio Rhatto <rhatto@riseup.net> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published +# by the Free Software Foundation, either version 3 of the License, +# or any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import subprocess +import os + +try: + from ranger.ext.rifle import Rifle +except ImportError: + print("Please install ranger first!") + raise ImportError + +try: + from platformdirs import user_config_dir +except ImportError: + print("Please install platformdirs first!") + raise ImportError + +from prompt_toolkit.application.current import get_app + +class FinderPluginFileRifleOpener(): + def __init__(self): + config_dir = user_config_dir('ranger') + self.config = os.path.join(config_dir, 'rifle.conf') + self.rifle = Rifle(self.config) + + self.rifle.reload_config() + + def options(self, file): + commands = self.rifle.list_commands([file]) + options = [ [label if label is not None else command, count] for count, command, label, flags in commands ] + + return options + + def open(self, file, option = 1): + # Subprocess version + #process = subprocess.Popen( + # ['rifle', file], + # stdout=subprocess.PIPE, + # stderr=subprocess.PIPE, + #) + + # API version + self.rifle.execute([file], option) + + # Make sure to "invalidate" the app after Rifle returned, to ensure the + # UI is refreshed + app = get_app() + app.invalidate() diff --git a/packages/finder/plugin/opener/file/xdg.py b/packages/finder/plugin/opener/file/xdg.py index 9c4f2a5..a5a89b8 100644 --- a/packages/finder/plugin/opener/file/xdg.py +++ b/packages/finder/plugin/opener/file/xdg.py @@ -24,7 +24,12 @@ class FinderPluginFileXdgOpener(): def __init__(self): pass - def open(self, file): + def options(self, file): + "This plugin currently not support options" + + return [ ] + + def open(self, file, option = 1): process = subprocess.Popen( ['xdg-open', file], stdout=subprocess.PIPE, |