aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2024-08-02 20:01:47 -0300
committerSilvio Rhatto <rhatto@riseup.net>2024-08-02 20:01:47 -0300
commit5d9d47a3426d03dbed7306d803a6b40c04d9ea4b (patch)
treec4654a68bd273b4ee49cecd4458c0b329921bc11
parent2e574026c50e4479ca409ae79541fbcc84b20c95 (diff)
downloadfinder-5d9d47a3426d03dbed7306d803a6b40c04d9ea4b.tar.gz
finder-5d9d47a3426d03dbed7306d803a6b40c04d9ea4b.tar.bz2
Feat: add basic file line plugin
-rw-r--r--packages/finder/main.py26
-rw-r--r--packages/finder/plugin/searcher/file/line.py57
2 files changed, 78 insertions, 5 deletions
diff --git a/packages/finder/main.py b/packages/finder/main.py
index 4e04e5c..931f1d4 100644
--- a/packages/finder/main.py
+++ b/packages/finder/main.py
@@ -45,6 +45,7 @@ except ImportError:
raise ImportError
from .plugin.searcher.file.name import FinderPluginFileNameSearcher
+from .plugin.searcher.file.line import FinderPluginFileLineSearcher
from .plugin.opener.file.xdg import FinderPluginFileXdgOpener
class FinderMain():
@@ -75,9 +76,13 @@ class FinderMain():
self.state['loading'] = False
def open(self):
+ # 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(self.path, item))
+ self.opener.open(os.path.join(path, item))
def clear_list_buffer(self):
self.list_buffer.set_document(Document(''), bypass_readonly=True)
@@ -207,6 +212,18 @@ class FinderMain():
def run(self):
return asyncio.run(self.main())
+ def set_handlers(self):
+ # Searcher and opener objects
+ if os.path.isdir(self.path):
+ self.searcher = FinderPluginFileNameSearcher(self.path)
+ elif os.path.isfile(self.path):
+ self.searcher = FinderPluginFileLineSearcher(self.path)
+ else:
+ # TODO: exception
+ pass
+
+ self.opener = FinderPluginFileXdgOpener()
+
async def main(self):
# UI
self.ui()
@@ -235,9 +252,8 @@ class FinderMain():
#editing_mode = EditingMode.VI,
)
- # Searcher and opener objects
- self.searcher = FinderPluginFileNameSearcher(self.path)
- self.opener = FinderPluginFileXdgOpener()
+ # Set handlers for the current path
+ self.set_handlers()
async with asyncio.TaskGroup() as tg:
# Run the application
@@ -254,7 +270,7 @@ class FinderMain():
def format(self, items):
return '\n'.join(
sorted(
- [ item.removeprefix(self.path).removeprefix(os.sep) for item in items ]
+ [ item.removeprefix(self.path).removeprefix(os.sep).rstrip('\n') for item in items ]
))
def finish(self, result = True):
diff --git a/packages/finder/plugin/searcher/file/line.py b/packages/finder/plugin/searcher/file/line.py
new file mode 100644
index 0000000..bb90cc0
--- /dev/null
+++ b/packages/finder/plugin/searcher/file/line.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Finder searcher and browser REPL.
+#
+# Copyright (C) 2024 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 os
+import re
+
+class FinderPluginFileLineSearcher():
+ def __init__(self, path):
+ self.set_path(path)
+
+ def set_path(self, path):
+ # Initialize item list
+ if 'items' not in dir(self):
+ self.items = []
+
+ # Reset item list when changing paths
+ elif 'path' in dir(self) and self.path != path:
+ self.items = []
+
+ self.path = path
+
+ async def finder(self, cached = False):
+ # Check existing cache
+ if cached:
+ if len(self.items) > 0:
+ return self.items
+ else:
+ return await self.finder()
+
+ else:
+ with open(self.path, 'r') as f:
+ self.items = f.readlines()
+
+ return self.items
+
+ def filter(self, text = ''):
+ if text == '' or text is None:
+ return self.items
+
+ return filter(lambda s: re.search(re.escape(text), s, flags=re.IGNORECASE), self.items)