vsm: persistant history of entered commands in interactive mode 22/46322/4
authorKrzysztof Dynowski <k.dynowski@samsung.com>
Fri, 7 Aug 2015 09:05:14 +0000 (11:05 +0200)
committerKrzysztof Dynowski <k.dynowski@samsung.com>
Wed, 19 Aug 2015 11:50:40 +0000 (13:50 +0200)
[Feature]       Keep history of entered commands (in ~/.vsm_history file)
[Cause]         No history after starting vsm
[Solution]      use read_history/write_history from readline library
[Verification]  Build, install, use vsm command

Change-Id: I107ba49ac01d52825312416a11fbb094735401af

cli/main.cpp

index 9bec3e7..48a71f2 100644 (file)
 #include <iterator>
 #include <iomanip>
 #include <boost/algorithm/string/predicate.hpp>
+#include <boost/filesystem.hpp>
 
 #include <readline/readline.h>
 #include <readline/history.h>
 
 using namespace vasum::cli;
 
+namespace fs =  boost::filesystem;
+
 namespace {
 
 static int interactiveMode = 0;
@@ -480,6 +483,12 @@ int cliMode(const int argc, const char** argv)
     return rc;
 }
 
+fs::path getHomePath() {
+    const char *h = ::getenv("HOME");
+    return fs::path(h ? h : "");
+}
+
+
 } // namespace
 
 
@@ -508,13 +517,24 @@ int main(const int argc, const char *argv[])
         }
 
     } else {
+        fs::path historyfile(".vsm_history");
 
         if (isatty(0) == 1) {
+            fs::path home = getHomePath();
+            if (!home.empty()) {
+                historyfile = home / historyfile;
+            }
+            ::read_history(historyfile.c_str());
+
             interactiveMode = 1;
             ::rl_attempted_completion_function = completion;
         }
 
         rc = processStream(std::cin);
+
+        if (interactiveMode) {
+            ::write_history(historyfile.c_str());
+        }
     }
 
     disconnect();