Browse Source

select-locate: show info while updating & searching database

Fabian Peter Hammerle 8 years ago
parent
commit
0eefdce24d
2 changed files with 92 additions and 23 deletions
  1. 91 22
      scripts/select-locate
  2. 1 1
      setup.py

+ 91 - 22
scripts/select-locate

@@ -8,30 +8,99 @@ import pprint
 import locale
 import argparse
 import argcomplete
-import ioex, ioex.selector
-
-def locate_select(patterns, match_all, ignore_case, update_database, multiple, database_path, update_require_visibility):
-
-    if update_database:
-        osex.update_locate_database(
-                database_path = database_path,
-                require_visibility = update_require_visibility
-                )
-
-    paths = osex.locate(
-                patterns, 
-                match_all = match_all, 
-                ignore_case = ignore_case,
-                database_path = database_path
-                )
-
-    selected_paths = ioex.curses_tty_wrapper(
-        ioex.selector.select_string,
-        paths,
+import ioex, ioex.cursesex, ioex.selector
+
+class LocateRootNode(ioex.selector.Node):
+
+    def __init__(
+            self,
+            window,
+            patterns,
+            match_all,
+            ignore_case,
+            database_path,
+            update_database,
+            update_require_visibility
+            ):
+        super(LocateRootNode, self).__init__()
+        self._window = window
+        self._patterns = [p for p in patterns if len(p) > 0]
+        self._match_all = match_all
+        self._ignore_case = ignore_case
+        self._database_path = database_path
+        self._update_database = update_database
+        self._update_require_visibility = update_require_visibility
+        self.refresh_children()
+
+    def get_window_height(self):
+        return self._window.getmaxyx()[0]
+
+    def get_window_width(self):
+        return self._window.getmaxyx()[1]
+
+    def get_header_height(self):
+        return self._header_height
+
+    def show_patterns(self):
+        self._window.addstr(0, 0, 'locate-select', curses.A_BOLD)
+        self._window.refresh()
+        self._header_height = 1
+
+    def show_header(self):
+        self.show_patterns()
+
+    def refresh_children(self):
+        self._window.clear()
+        self.show_header()
+        self._clear_children()
+        if self._update_database:
+            self._window.addstr(self.get_header_height(), 0, 'updating database... ')
+            self._window.refresh()
+            osex.update_locate_database(
+                    database_path = self._database_path,
+                    require_visibility = self._update_require_visibility
+                    )
+        self._window.addstr(self.get_header_height(), 0, 'searching database... ')
+        self._window.clrtoeol()
+        self._window.refresh()
+        paths = osex.locate(
+                    self._patterns,
+                    match_all = self._match_all,
+                    ignore_case = self._ignore_case,
+                    database_path = self._database_path
+                    )
+        if len(paths) == 0:
+            self._window.addstr(self.get_header_height(), 0, 'nothing found')
+            self._window.clrtoeol()
+            self._window.refresh()
+        else:
+            for path in paths:
+                self._append_child(ioex.selector.StaticNode(path))
+
+def locate_select(stdscr, patterns, match_all, ignore_case, update_database, multiple, database_path, update_require_visibility):
+
+    curses.curs_set(0)
+
+    root = LocateRootNode(
+            stdscr,
+            patterns,
+            match_all,
+            ignore_case,
+            database_path,
+            update_database,
+            update_require_visibility
+            )
+
+    selection = ioex.selector.select(
+        stdscr,
+        root,
         multiple = multiple
         )
 
-    return selected_paths
+    if selection is None:
+        return None
+    else:
+        return [n.get_label() for n in selection]
 
 def _init_argparser():
 
@@ -57,7 +126,7 @@ def main(argv):
     elif args.update_require_visibility == 'no':
         args.update_require_visibility = False
 
-    paths = locate_select(**vars(args))
+    paths = ioex.cursesex.tty_wrapper(locate_select, **vars(args))
     if paths is None:
         return 1
     else:

+ 1 - 1
setup.py

@@ -14,6 +14,6 @@ setup(
     keywords = [],
     classifiers = [],
     scripts = glob.glob('scripts/*'),
-    install_requires = ['ioex>=0.2.1'],
+    install_requires = ['ioex>0.2.1'],
     tests_require = ['pytest']
     )