Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

add method to autocomplete from command history

Geo13c36a0482c1
diff --git a/src/Managers/CommandHistoryManager.cpp b/src/Managers/CommandHistoryManager.cpp
index fe94ec61..278ba9de 100644
--- a/src/Managers/CommandHistoryManager.cpp
+++ b/src/Managers/CommandHistoryManager.cpp
@@ -39,6 +39,31 @@ const std::string& CommandHistoryManager::down() {
return currentLine;
}
+/*
+Auto complete
+*/
+const std::string& CommandHistoryManager::autocomplete(const std::string& prefix) {
+ currentLine.clear();
+
+ if (prefix.empty() || history.empty()) {
+ return currentLine;
+ }
+
+ // newest -> oldest
+ for (size_t i = history.size(); i-- > 0;) {
+ const std::string& h = history[i];
+
+ if (h.size() >= prefix.size() && h.compare(0, prefix.size(), prefix) == 0) {
+ currentLine = h;
+ index = i;
+ return currentLine;
+ }
+ }
+
+ // no match
+ return currentLine;
+}
+
/*
Reset History
*/
diff --git a/src/Managers/CommandHistoryManager.h b/src/Managers/CommandHistoryManager.h
index 38984d99..18041bfc 100644
--- a/src/Managers/CommandHistoryManager.h
+++ b/src/Managers/CommandHistoryManager.h
@@ -14,6 +14,9 @@ public:
// Moves down in history and returns the next command
const std::string& down();
+ // Attempts to autocomplete based on the given prefix
+ const std::string& autocomplete(const std::string& prefix);
+
// Resets the navigation index to the end of the history
void reset();