1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 12:05:07 +00:00

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.
This commit is contained in:
Linus Groh 2020-10-25 23:25:41 +00:00 committed by Andreas Kling
parent af05671843
commit b2e4fe1299
6 changed files with 36 additions and 31 deletions

View file

@ -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)