1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-02 23:02:06 +00:00

LibLine+Shell: Add dirty history flag and use it

This patch adds a new flag called history_dirty to Line::Editor that is
set when history is added to but written.  Applications can leverage
this flag to write history only when it changes.  This patch adds an
example usage of this functionality to Shell, which will now only save
the history when it is dirty.
This commit is contained in:
sin-ack 2021-05-11 15:06:03 +00:00 committed by Linus Groh
parent 227ccfc61a
commit ecbe17fb11
3 changed files with 5 additions and 1 deletions

View file

@ -214,6 +214,7 @@ void Editor::add_to_history(const String& line)
struct timeval tv; struct timeval tv;
gettimeofday(&tv, nullptr); gettimeofday(&tv, nullptr);
m_history.append({ line, tv.tv_sec }); m_history.append({ line, tv.tv_sec });
m_history_dirty = true;
} }
bool Editor::load_history(const String& path) bool Editor::load_history(const String& path)
@ -301,6 +302,7 @@ bool Editor::save_history(const String& path)
for (const auto& entry : final_history) for (const auto& entry : final_history)
file->write(String::formatted("{}::{}\n\n", entry.timestamp, entry.entry)); file->write(String::formatted("{}::{}\n\n", entry.timestamp, entry.entry));
m_history_dirty = false;
return true; return true;
} }

View file

@ -142,6 +142,7 @@ public:
bool load_history(const String& path); bool load_history(const String& path);
bool save_history(const String& path); bool save_history(const String& path);
const auto& history() const { return m_history; } const auto& history() const { return m_history; }
bool is_history_dirty() const { return m_history_dirty; }
void register_key_input_callback(const KeyBinding&); void register_key_input_callback(const KeyBinding&);
void register_key_input_callback(Vector<Key> keys, Function<bool(Editor&)> callback) { m_callback_machine.register_key_input_callback(move(keys), move(callback)); } void register_key_input_callback(Vector<Key> keys, Function<bool(Editor&)> callback) { m_callback_machine.register_key_input_callback(move(keys), move(callback)); }
@ -442,6 +443,7 @@ private:
Vector<HistoryEntry> m_history; Vector<HistoryEntry> m_history;
size_t m_history_cursor { 0 }; size_t m_history_cursor { 0 };
size_t m_history_capacity { 1024 }; size_t m_history_capacity { 1024 };
bool m_history_dirty { false };
enum class InputState { enum class InputState {
Free, Free,

View file

@ -2084,7 +2084,7 @@ void Shell::timer_event(Core::TimerEvent& event)
if (!m_history_autosave_time.has_value()) if (!m_history_autosave_time.has_value())
return; return;
if (m_editor) if (m_editor && m_editor->is_history_dirty())
m_editor->save_history(get_history_path()); m_editor->save_history(get_history_path());
} }