aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-09 07:22:42 +0000
committer_ov4 <ov4@ov4.org>2026-10-09 07:22:42 +0000
commitbeadcf1f0d9cf8b9c0b14683bf40d27c142d33c5 (patch)
treec996d716cad78bb24870996d751715e8387d3024
parent091db10e0f5e8e95095f30cef34d51e23fc01abb (diff)
add history load/save
-rw-r--r--include/history.h7
-rw-r--r--include/path.h4
-rw-r--r--module-test/line_test.cc7
-rw-r--r--src/history.cc59
-rw-r--r--src/path.cc12
5 files changed, 87 insertions, 2 deletions
diff --git a/include/history.h b/include/history.h
index a4966e2..8b7a4bb 100644
--- a/include/history.h
+++ b/include/history.h
@@ -3,6 +3,8 @@
#include <string>
#include <vector>
+#include "path.h"
+
namespace ov4
{
@@ -15,4 +17,9 @@ void clear_history();
std::size_t get_last_index();
+std::string get_default_history_path();
+
+void load_history(const std::string &path = get_default_history_path());
+void save_history(const std::string &path = get_default_history_path());
+
}
diff --git a/include/path.h b/include/path.h
index c5f0d2b..d168581 100644
--- a/include/path.h
+++ b/include/path.h
@@ -7,8 +7,6 @@
#include <iterator>
#include <iostream>
-#include "all.h"
-
namespace ov4
{
@@ -19,4 +17,6 @@ std::string path_unifier(const std::string &s, bool is_cmd = false);
std::string get_current_dir();
int set_current_dir(const std::string &s);
+std::string get_home_dir();
+
}
diff --git a/module-test/line_test.cc b/module-test/line_test.cc
index 2ad6f32..a54085c 100644
--- a/module-test/line_test.cc
+++ b/module-test/line_test.cc
@@ -6,6 +6,8 @@
src/line/cursor.cc \
src/line/getter.cc \
src/history.cc \
+ src/path.cc \
+ src/util/string.cc \
-std=c++26 -Iinclude -Ithird_party/magic_enum/include \
-O0 -ggdb3 -fno-omit-frame-pointer -fno-inline -D_GLIBCXX_ASSERTIONS \
-o module-test/line_test.out
@@ -21,12 +23,15 @@
#include "line/cursor.h"
#include "line/getter.h"
#include "line/type.h"
+#include "history.h"
+#include "path.h"
using namespace std;
using namespace ov4;
int main()
{
+ load_history();
while (true)
{
print("shell $ ");
@@ -39,6 +44,8 @@ int main()
disable_raw();
cout << "\n\n\n\n" << endl;
cout << s << endl;
+ if (s == "quit") break;
}
+ save_history();
return 0;
}
diff --git a/src/history.cc b/src/history.cc
index 3e976ce..4b3eba1 100644
--- a/src/history.cc
+++ b/src/history.cc
@@ -1,5 +1,15 @@
#include "history.h"
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <print>
+#include <cstdio>
+#include <unistd.h>
+#include <fstream>
+#include <iostream>
+
+#include "util/io.h"
+
using namespace std;
namespace ov4
@@ -19,5 +29,54 @@ size_t get_last_index()
{
return history_vec.size() - 1;
}
+
+string get_default_history_path()
+{
+ return get_home_dir() + '/' + ".ccshell_history";
+}
+
+void load_history(const string &path)
+{
+ history_vec.clear();
+ ifstream ifs(path);
+ if (!ifs.is_open())
+ {
+ loggerln("load_history: fail to open history");
+ return;
+ }
+ string tmp;
+ while (getline(ifs, tmp))
+ {
+ history_vec.push_back(tmp);
+ }
+ return;
+}
+
+void save_history(const string &path)
+{
+ int fd = open(path.c_str(), O_WRONLY | O_CREAT);
+ if (fd == -1)
+ {
+ loggerln("save_history: fail to open() history");
+ return;
+ }
+ char f_mode = 'w';
+ FILE *f = fdopen(fd, &f_mode);
+ if (f == nullptr)
+ {
+ loggerln("save_history: fail to fdopen() history");
+ close(fd);
+ return;
+ }
+
+ for (auto &c : history_vec)
+ {
+ println(f, "{}", c);
+ }
+
+ fflush(f);
+ fsync(fd);
+ fclose(f);
+}
}
diff --git a/src/path.cc b/src/path.cc
index 103e2c5..30e81cc 100644
--- a/src/path.cc
+++ b/src/path.cc
@@ -1,5 +1,12 @@
#include "path.h"
+#include <pwd.h>
+#include <unistd.h>
+
+#include "global.h"
+#include "util/string.h"
+#include "util/io.h"
+
namespace ov4
{
@@ -67,5 +74,10 @@ string get_current_dir()
return safe_str(getcwd(buf, MAXLINE));
}
+string get_home_dir()
+{
+ return getpwuid(getuid())->pw_dir;
+}
+
}