You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2020/03/12 11:03:27 UTC

[mynewt-core] branch master updated: sys/console: Add auto search to ram based history

This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 7b8bc8d  sys/console: Add auto search to ram based history
7b8bc8d is described below

commit 7b8bc8d56ffbc5ac04ace2f8b5278a2b2affc822
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Tue Mar 10 15:55:48 2020 +0100

    sys/console: Add auto search to ram based history
    
    Feature present in log based console history is now implemented
    to ram base history handler.
---
 sys/console/full/history_ram/src/history_ram.c | 53 +++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 6 deletions(-)

diff --git a/sys/console/full/history_ram/src/history_ram.c b/sys/console/full/history_ram/src/history_ram.c
index e08fda0..348599e 100644
--- a/sys/console/full/history_ram/src/history_ram.c
+++ b/sys/console/full/history_ram/src/history_ram.c
@@ -157,28 +157,69 @@ console_history_add(const char *line)
     return 1;
 }
 
+/**
+ * Function returns line from history
+ *
+ * @param line_num 1 - based line counter, 1 - last line in history
+ *
+ * @return line with given number or NULL if line_num is out of range
+ */
+static const char *
+console_history_line(uint8_t line_num)
+{
+    int head = console_hist.head;
+
+    if (line_num > console_hist.count) {
+        return NULL;
+    } else {
+        return console_hist.lines[(head + console_hist.count - line_num) % console_hist.count];
+    }
+}
+
 history_handle_t
 console_history_find(history_handle_t start, history_find_type_t search_type,
                      void *arg)
 {
-    int num;
+    const char *history_line;
+    const char *pattern;
+    size_t pattern_size;
+    int num = 0;
+    int direction;
 
     switch (search_type) {
     case HFT_PREV:
         num = start + (arg ? *(int *)arg : 1);
         if (num > console_hist.count) {
-            return 0;
+            num = 0;
         }
-        return num;
+        break;
     case HFT_NEXT:
         num = start - (arg ? *(int *)arg : 1);
         if (num <= 0) {
-            return 0;
+            num = 0;
         }
-        return num;
+        break;
+    case HFT_MATCH_NEXT:
+    case HFT_MATCH_PREV:
+        direction = (search_type == HFT_MATCH_NEXT) ? -1 : 1;
+        pattern = (const char *)arg;
+        pattern_size = strlen(pattern);
+        num = start;
+        while (1) {
+            num += direction;
+            history_line = console_history_line(num);
+            if (history_line == NULL) {
+                num = 0;
+            } else if (strncmp(history_line, pattern, pattern_size) != 0) {
+                continue;
+            }
+            break;
+        }
+        break;
     default:
-        return 0;
+        break;
     }
+    return num;
 }
 
 int