about summary refs log tree commit diff stats
path: root/100trace_browser.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2017-03-09 23:19:20 -0800
committerKartik K. Agaram <vc@akkartik.com>2017-03-09 23:24:08 -0800
commit4b487ef524d5c9d5756a14c45e7a1afd21b094cc (patch)
tree2d0986c81a978c08f6057435fb935509bea6f8a0 /100trace_browser.cc
parent9aa2fd345df1019443fceb041a872421d0c74650 (diff)
downloadmu-4b487ef524d5c9d5756a14c45e7a1afd21b094cc.tar.gz
3778
Support backspace in trace-browser.
Diffstat (limited to '100trace_browser.cc')
-rw-r--r--100trace_browser.cc40
1 files changed, 31 insertions, 9 deletions
diff --git a/100trace_browser.cc b/100trace_browser.cc
index 46190502..e2ed54ca 100644
--- a/100trace_browser.cc
+++ b/100trace_browser.cc
@@ -47,7 +47,14 @@
 //:
 //:   `/`: Search for a pattern.
 //:   `n`: Jump to next instance of search pattern.
-//:   `p`: Jump to previous instance of search pattern.
+//:   `N`: Jump to previous instance of search pattern.
+//:
+//:   After hitting `/`, a small editor on the bottom-most line supports the
+//:   following hotkeys:
+//:     ascii characters: add the key to the pattern
+//:     `Enter`: search for the pattern
+//:     `Esc` or `ctrl-c`: cancel the current search, setting the screen back
+//:       to its state before the search
 
 :(before "End Primitive Recipe Declarations")
 _BROWSE_TRACE,
@@ -230,20 +237,22 @@ void start_trace_browser() {
       refresh_screen_rows();
     }
     else if (key == '/') {
-      start_search_editor();
-      search_next(Current_search_pattern);
+      if (start_search_editor())
+        search_next(Current_search_pattern);
     }
     else if (key == 'n') {
-      search_next(Current_search_pattern);
+      if (!Current_search_pattern.empty())
+        search_next(Current_search_pattern);
     }
     else if (key == 'N') {
-      search_previous(Current_search_pattern);
+      if (!Current_search_pattern.empty())
+        search_previous(Current_search_pattern);
     }
   }
   tb_shutdown();
 }
 
-void start_search_editor() {
+bool start_search_editor() {
   const int bottom_screen_line = tb_height()-1;
   // run a little editor just in the last line of the screen
   clear_line(bottom_screen_line);
@@ -257,10 +266,23 @@ void start_search_editor() {
   while (true) {
     int key = read_key();
     if (key == TB_KEY_ENTER) {
-      Current_search_pattern = pattern;
-      break;
+      if (!pattern.empty())
+        Current_search_pattern = pattern;
+      return true;
+    }
+    else if (key == TB_KEY_ESC || key == TB_KEY_CTRL_C) {
+      return false;
+    }
+    else if (key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2) {
+      if (col > 0) {
+        --col;
+        tb_change_cell(col, bottom_screen_line, ' ', TB_WHITE, TB_BLACK);
+        tb_set_cursor(col, bottom_screen_line);
+        tb_present();
+        pattern.erase(--pattern.end());
+      }
     }
-    else {
+    else if (key < 128) {  // ascii only
       char c = static_cast<char>(key);
       tb_change_cell(col, bottom_screen_line, c, TB_WHITE, TB_BLACK);
       ++col;