From ecbe17fb11e587341404a3bc2cc0b3db799374f1 Mon Sep 17 00:00:00 2001 From: sin-ack Date: Tue, 11 May 2021 15:06:03 +0000 Subject: [PATCH] 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. --- Userland/Libraries/LibLine/Editor.cpp | 2 ++ Userland/Libraries/LibLine/Editor.h | 2 ++ Userland/Shell/Shell.cpp | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibLine/Editor.cpp b/Userland/Libraries/LibLine/Editor.cpp index 9a59d611cc..d516a3ac98 100644 --- a/Userland/Libraries/LibLine/Editor.cpp +++ b/Userland/Libraries/LibLine/Editor.cpp @@ -214,6 +214,7 @@ void Editor::add_to_history(const String& line) struct timeval tv; gettimeofday(&tv, nullptr); m_history.append({ line, tv.tv_sec }); + m_history_dirty = true; } bool Editor::load_history(const String& path) @@ -301,6 +302,7 @@ bool Editor::save_history(const String& path) for (const auto& entry : final_history) file->write(String::formatted("{}::{}\n\n", entry.timestamp, entry.entry)); + m_history_dirty = false; return true; } diff --git a/Userland/Libraries/LibLine/Editor.h b/Userland/Libraries/LibLine/Editor.h index d0dc995f0c..62e9e0cd48 100644 --- a/Userland/Libraries/LibLine/Editor.h +++ b/Userland/Libraries/LibLine/Editor.h @@ -142,6 +142,7 @@ public: bool load_history(const String& path); bool save_history(const String& path); 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(Vector keys, Function callback) { m_callback_machine.register_key_input_callback(move(keys), move(callback)); } @@ -442,6 +443,7 @@ private: Vector m_history; size_t m_history_cursor { 0 }; size_t m_history_capacity { 1024 }; + bool m_history_dirty { false }; enum class InputState { Free, diff --git a/Userland/Shell/Shell.cpp b/Userland/Shell/Shell.cpp index 8737d8c3c7..f64a2708d7 100644 --- a/Userland/Shell/Shell.cpp +++ b/Userland/Shell/Shell.cpp @@ -2084,7 +2084,7 @@ void Shell::timer_event(Core::TimerEvent& event) if (!m_history_autosave_time.has_value()) return; - if (m_editor) + if (m_editor && m_editor->is_history_dirty()) m_editor->save_history(get_history_path()); }