Browse Source

locate select: show common prefix of results

Fabian Peter Hammerle 8 years ago
parent
commit
ab0a8de9c2
1 changed files with 26 additions and 7 deletions
  1. 26 7
      osex/locate.py

+ 26 - 7
osex/locate.py

@@ -68,8 +68,14 @@ class LocateRootNode(ioex.selector.Node):
     def get_window_width(self):
         return self._window.getmaxyx()[1]
 
+    def get_patterns_height(self):
+        return self._patterns_height
+
+    def get_common_prefix_display_height(self):
+        return self._common_prefix_display_height
+
     def get_header_height(self):
-        return self._header_height
+        return self.get_patterns_height() + self.get_common_prefix_display_height()
 
     def show_patterns(self):
         label = 'patterns: '
@@ -87,26 +93,37 @@ class LocateRootNode(ioex.selector.Node):
             self._window.addstr(pattern_wrapped, curses.A_REVERSE)
             self._window.addstr(' ')
         self._window.refresh()
-        self._header_height = patterns_text.count('\n') + 1
+        self._patterns_height = patterns_text.count('\n') + 1
         # remove A_REVERSE attribute from ident blocks
-        for row_index in range(1, self._header_height):
+        for row_index in range(1, self._patterns_height):
             self._window.addstr(row_index, 0, ' ' * len(label))
 
+    def show_common_prefix(self):
+        if len(self.get_children()) > 1 and len(self.common_prefix) > len('/'):
+            self._window.addstr(self.get_patterns_height(), 0, 'common prefix: ', curses.A_BOLD)
+            self._window.addstr(self.common_prefix)
+            self._window.clrtoeol()
+            self._window.refresh()
+            self._common_prefix_display_height = 1
+        else:
+            self._common_prefix_display_height = 0
+
     def show_header(self):
         self.show_patterns()
+        self.show_common_prefix()
 
     def refresh_children(self):
         self._window.clear()
-        self.show_header()
+        self.show_patterns()
         self._clear_children()
         if self._update_database:
-            self._window.addstr(self.get_header_height(), 0, 'updating database... ')
+            self._window.addstr(self.get_patterns_height(), 0, 'updating database... ')
             self._window.refresh()
             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.addstr(self.get_patterns_height(), 0, 'searching database... ')
         self._window.clrtoeol()
         self._window.refresh()
         paths = locate(
@@ -116,12 +133,14 @@ class LocateRootNode(ioex.selector.Node):
                     database_path = self._database_path
                     )
         if len(paths) == 0:
-            self._window.addstr(self.get_header_height(), 0, 'nothing found')
+            self._window.addstr(self.get_patterns_height(), 0, 'nothing found')
             self._window.clrtoeol()
             self._window.refresh()
         else:
             for path in paths:
                 self._append_child(ioex.selector.StaticNode(path))
+            self.common_prefix = os.path.dirname(os.path.commonprefix(paths))
+            self.show_common_prefix()
 
 def locate_select(
         stdscr,