Permalink
Browse files

Improve search box behavior.

  • Loading branch information...
1 parent 874e85c commit 6d0e846936286fe9da8edd1503e1ef658958f7c0 @inducer committed Jul 24, 2012
Showing with 99 additions and 74 deletions.
  1. +10 −31 pudb/debugger.py
  2. +89 −43 pudb/ui_tools.py
View
@@ -301,8 +301,8 @@ def _runscript(self, filename):
# UI stuff --------------------------------------------------------------------
from pudb.ui_tools import make_hotkey_markup, labelled_value, \
- SelectableText, SignalWrap, StackFrame, BreakpointFrame, \
- SearchBox
+ SelectableText, SignalWrap, StackFrame, BreakpointFrame
+
from pudb.var_view import FrameVarInfoKeeper
@@ -335,7 +335,9 @@ def __init__(self, dbg):
self.debugger = dbg
Attr = urwid.AttrMap
- self.search_box = None
+ from pudb.ui_tools import SearchController
+ self.search_controller = SearchController(self)
+
self.last_module_filter = ""
# {{{ build ui
@@ -764,8 +766,6 @@ def page_down(w, size, key):
def page_up(w, size, key):
w.keypress(size, "page up")
- def move_up(w, size, key):
- w.keypress(size, "up")
def scroll_left(w, size, key):
self.source_hscroll_start = max(
0,
@@ -779,35 +779,13 @@ def scroll_right(w, size, key):
sl._invalidate()
def search(w, size, key):
- if self.search_box is None:
- _, search_start = self.source.get_focus()
-
- self.search_box = SearchBox(self)
- self.search_AttrMap = urwid.AttrMap(
- self.search_box, "search box")
-
- self.lhs_col.item_types.insert(
- 0, ("flow", None))
- self.lhs_col.widget_list.insert( 0, self.search_AttrMap)
-
- self.columns.set_focus(self.lhs_col)
- self.lhs_col.set_focus(self.search_AttrMap)
- else:
- self.columns.set_focus(self.lhs_col)
- self.lhs_col.set_focus(self.search_AttrMap)
- self.search_box.restart_search()
+ self.search_controller.open_search_ui()
def search_next(w, size, key):
- if self.search_box is not None:
- self.search_box.do_search(1)
- else:
- self.message("No previous search term.")
+ self.search_controller.perform_search(dir=1, update_search_start=True)
def search_previous(w, size, key):
- if self.search_box is not None:
- self.search_box.do_search(-1)
- else:
- self.message("No previous search term.")
+ self.search_controller.perform_search(dir=-1, update_search_start=True)
def toggle_breakpoint(w, size, key):
if self.shown_file:
@@ -1293,7 +1271,8 @@ def event_loop(self, toplevel=None):
"If you're new here, welcome! The help screen (invoked by hitting "
"'?' after this message) should get you on your way.\n"
"\nChanges in version 2012.3:\n\n"
- "- Python 3 support (contributed by Brad Froehle).\n"
+ "- Python 3 support (contributed by Brad Froehle)\n"
+ "- Better search box behavior (suggested by Ram Rachum)\n"
"\nChanges in version 2012.2.1:\n\n"
"- Don't touch config files during install.\n"
"\nChanges in version 2012.2:\n\n"
View
@@ -172,58 +172,67 @@ def keypress(self, size, key):
-class SearchBox(urwid.Edit):
+class SearchController(object):
def __init__(self, ui):
self.ui = ui
- urwid.Edit.__init__(self, [("label", "Search: ") ], "")
self.highlight_line = None
- _, self.search_start = self.ui.source.get_focus()
+ self.search_box = None
+ self.last_search_string = None
- from time import time
- self.search_start_time = time()
+ def cancel_highlight(self):
+ if self.highlight_line is not None:
+ self.highlight_line.set_highlight(False)
+ self.highlight_line = None
- def restart_search(self):
- from time import time
- now = time()
- if self.search_start_time > 5:
- self.set_edit_text("")
+ def cancel_search(self):
+ self.cancel_highlight()
+ self.hide_search_ui()
- self.search_time = now
+ def hide_search_ui(self):
+ self.search_box = None
+ del self.ui.lhs_col.item_types[0]
+ del self.ui.lhs_col.widget_list[0]
+ self.ui.lhs_col.set_focus(self.ui.lhs_col.widget_list[0])
- def keypress(self, size, key):
- result = urwid.Edit.keypress(self, size, key)
+ def open_search_ui(self):
+ lhs_col = self.ui.lhs_col
- if result is not None:
- if key == "esc":
- self.cancel_search()
- return None
- elif key == "enter":
- if self.get_edit_text():
- self.ui.lhs_col.set_focus(self.ui.lhs_col.widget_list[1])
- else:
- self.cancel_search()
- return None
- else:
- if self.do_search(1, self.search_start):
- self.ui.search_AttrMap.set_attr_map({None: "search box"})
- else:
- self.ui.search_AttrMap.set_attr_map({None: "search not found"})
+ if self.search_box is None:
+ _, self.search_start = self.ui.source.get_focus()
- return result
+ self.search_box = SearchBox(self)
+ self.search_AttrMap = urwid.AttrMap(
+ self.search_box, "search box")
- def cancel_highlight(self):
- if self.highlight_line is not None:
- self.highlight_line.set_highlight(False)
- self.highlight_line = None
+ lhs_col.item_types.insert(
+ 0, ("flow", None))
+ lhs_col.widget_list.insert( 0, self.search_AttrMap)
- def do_search(self, dir, start=None):
+ self.ui.columns.set_focus(lhs_col)
+ lhs_col.set_focus(self.search_AttrMap)
+ else:
+ self.ui.columns.set_focus(lhs_col)
+ lhs_col.set_focus(self.search_AttrMap)
+ #self.search_box.restart_search()
+
+ def perform_search(self, dir, s=None, start=None, update_search_start=False):
self.cancel_highlight()
+ # self.ui.lhs_col.set_focus(self.ui.lhs_col.widget_list[1])
+
+ if s is None:
+ s = self.last_search_string
+
+ if s is None:
+ self.ui.message("No previous search term.")
+ else:
+ self.last_search_string = s
+
+
if start is None:
- _, start = self.ui.source.get_focus()
- s = self.ui.search_box.get_edit_text()
+ start = self.search_start
case_insensitive = s.lower() == s
@@ -245,17 +254,54 @@ def do_search(self, dir, start=None):
sl.set_highlight(True)
self.highlight_line = sl
self.ui.source.set_focus(i)
+
+ if update_search_start:
+ self.search_start = i
+
return True
- last_i = i
i = (i+dir) % len(self.ui.source)
return False
- def cancel_search(self):
- self.cancel_highlight()
- self.ui.search_box = None
- del self.ui.lhs_col.item_types[0]
- del self.ui.lhs_col.widget_list[0]
- self.ui.lhs_col.set_focus(self.ui.lhs_col.widget_list[0])
+
+
+class SearchBox(urwid.Edit):
+ def __init__(self, controller):
+ urwid.Edit.__init__(self, [("label", "Search: ") ], "")
+ self.controller = controller
+
+ def restart_search(self):
+ from time import time
+ now = time()
+
+ if self.search_start_time > 5:
+ self.set_edit_text("")
+
+ self.search_time = now
+
+ def keypress(self, size, key):
+ result = urwid.Edit.keypress(self, size, key)
+ txt = self.get_edit_text()
+
+ if result is not None:
+ if key == "esc":
+ self.controller.cancel_search()
+ return None
+ elif key == "enter":
+ if txt:
+ self.controller.hide_search_ui()
+ self.controller.perform_search(dir=1, s=txt,
+ update_search_start=True)
+ else:
+ self.controller.cancel_search()
+ return None
+ else:
+ if self.controller.perform_search(dir=1, s=txt):
+ self.controller.search_AttrMap.set_attr_map({None: "search box"})
+ else:
+ self.controller.search_AttrMap.set_attr_map({None: "search not found"})
+
+ return result
+

0 comments on commit 6d0e846

Please sign in to comment.