summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-10-25 23:25:41 +0000
committerAndreas Kling <kling@serenityos.org>2020-10-26 11:27:54 +0100
commitb2e4fe1299bd57b4ab4bb7ea0e0133038db52b26 (patch)
tree991fa54dd3333d8fc502d7cc9a6cfb55cbf18e52
parentaf056718437f23e895f45e9b6c473d37a3e5f699 (diff)
downloadserenity-b2e4fe1299bd57b4ab4bb7ea0e0133038db52b26.zip
Shell+LibLine: Move Shell::{load,save}_history() to Line::Editor
This allows us to easily re-use history loading and saving in other programs using Line::Editor, as well as implementing universally recognized HISTCONTROL.
-rw-r--r--Libraries/LibLine/Editor.cpp27
-rw-r--r--Libraries/LibLine/Editor.h4
-rw-r--r--Shell/Builtin.cpp2
-rw-r--r--Shell/Shell.cpp28
-rw-r--r--Shell/Shell.h4
-rw-r--r--Shell/main.cpp2
6 files changed, 36 insertions, 31 deletions
diff --git a/Libraries/LibLine/Editor.cpp b/Libraries/LibLine/Editor.cpp
index 28f66ce718..c103d5ccf9 100644
--- a/Libraries/LibLine/Editor.cpp
+++ b/Libraries/LibLine/Editor.cpp
@@ -34,6 +34,7 @@
#include <LibCore/ConfigFile.h>
#include <LibCore/Event.h>
#include <LibCore/EventLoop.h>
+#include <LibCore/File.h>
#include <LibCore/Notifier.h>
#include <ctype.h>
#include <signal.h>
@@ -212,6 +213,32 @@ void Editor::add_to_history(const String& line)
m_history.append(line);
}
+bool Editor::load_history(const String& path)
+{
+ auto history_file = Core::File::construct(path);
+ if (!history_file->open(Core::IODevice::ReadOnly))
+ return false;
+ while (history_file->can_read_line()) {
+ auto b = history_file->read_line(1024);
+ // skip the newline and terminating bytes
+ add_to_history(String(reinterpret_cast<const char*>(b.data()), b.size() - 2));
+ }
+ return true;
+}
+
+bool Editor::save_history(const String& path)
+{
+ auto file_or_error = Core::File::open(path, Core::IODevice::WriteOnly, 0600);
+ if (file_or_error.is_error())
+ return false;
+ auto& file = *file_or_error.value();
+ for (const auto& line : m_history) {
+ file.write(line);
+ file.write("\n");
+ }
+ return true;
+}
+
void Editor::clear_line()
{
for (size_t i = 0; i < m_cursor; ++i)
diff --git a/Libraries/LibLine/Editor.h b/Libraries/LibLine/Editor.h
index 076acdc830..f2513fbe6c 100644
--- a/Libraries/LibLine/Editor.h
+++ b/Libraries/LibLine/Editor.h
@@ -143,7 +143,9 @@ public:
void initialize();
- void add_to_history(const String&);
+ void add_to_history(const String& line);
+ bool load_history(const String& path);
+ bool save_history(const String& path);
const Vector<String>& history() const { return m_history; }
void register_key_input_callback(const KeyBinding&);
diff --git a/Shell/Builtin.cpp b/Shell/Builtin.cpp
index ee71a4e6db..c719abef32 100644
--- a/Shell/Builtin.cpp
+++ b/Shell/Builtin.cpp
@@ -267,7 +267,7 @@ int Shell::builtin_exit(int argc, const char** argv)
}
}
stop_all_jobs();
- save_history();
+ m_editor->save_history(get_history_path());
if (m_is_interactive)
printf("Good-bye!\n");
exit(exit_code);
diff --git a/Shell/Shell.cpp b/Shell/Shell.cpp
index 36899a30f4..05fe1f66a6 100644
--- a/Shell/Shell.cpp
+++ b/Shell/Shell.cpp
@@ -966,30 +966,6 @@ String Shell::get_history_path()
return builder.to_string();
}
-void Shell::load_history()
-{
- auto history_file = Core::File::construct(get_history_path());
- if (!history_file->open(Core::IODevice::ReadOnly))
- return;
- while (history_file->can_read_line()) {
- auto b = history_file->read_line(1024);
- // skip the newline and terminating bytes
- m_editor->add_to_history(String(reinterpret_cast<const char*>(b.data()), b.size() - 2));
- }
-}
-
-void Shell::save_history()
-{
- auto file_or_error = Core::File::open(get_history_path(), Core::IODevice::WriteOnly, 0600);
- if (file_or_error.is_error())
- return;
- auto& file = *file_or_error.value();
- for (const auto& line : m_editor->history()) {
- file.write(line);
- file.write("\n");
- }
-}
-
String Shell::escape_token(const String& token)
{
StringBuilder builder;
@@ -1559,7 +1535,7 @@ Shell::Shell(Line::Editor& editor)
}
directory_stack.append(cwd);
- load_history();
+ m_editor->load_history(get_history_path());
cache_path();
}
@@ -1569,7 +1545,7 @@ Shell::~Shell()
return;
stop_all_jobs();
- save_history();
+ m_editor->save_history(get_history_path());
}
void Shell::stop_all_jobs()
diff --git a/Shell/Shell.h b/Shell/Shell.h
index 4f61942fe8..d485979c73 100644
--- a/Shell/Shell.h
+++ b/Shell/Shell.h
@@ -109,6 +109,8 @@ public:
String format(const StringView&, ssize_t& cursor) const;
+ RefPtr<Line::Editor> editor() const { return m_editor; }
+
struct LocalFrame {
HashMap<String, RefPtr<AST::Value>> local_variables;
};
@@ -154,8 +156,6 @@ public:
void kill_job(const Job*, int sig);
String get_history_path();
- void load_history();
- void save_history();
void print_path(const String& path);
bool read_single_line();
diff --git a/Shell/main.cpp b/Shell/main.cpp
index 4ab4805a5b..cc2c08d308 100644
--- a/Shell/main.cpp
+++ b/Shell/main.cpp
@@ -57,7 +57,7 @@ int main(int argc, char** argv)
for (auto& it : s_shell->jobs)
s_shell->kill_job(it.value.ptr(), SIGHUP);
- s_shell->save_history();
+ s_shell->editor()->save_history(s_shell->get_history_path());
});
editor = Line::Editor::construct();