diff options
author | Nick Vella <nick@nxk.io> | 2021-02-15 20:49:37 +1100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-02-17 23:06:19 +0100 |
commit | bafb8b0be69c0452b7974aa4616976d572db0f21 (patch) | |
tree | ca74a9fbabfb8e92dbd166940876db43357dd5c1 | |
parent | 05914d2e9a81243a82369862e28c8ae59d6a38cd (diff) | |
download | serenity-bafb8b0be69c0452b7974aa4616976d572db0f21.zip |
Run: Store and present recent Run command history in a ComboBox.
We now store the last 25 inputs ran in Run in a simple text file under
.config (~/.config/RunHistory.txt)
-rw-r--r-- | Userland/Applications/Run/Run.gml | 2 | ||||
-rw-r--r-- | Userland/Applications/Run/RunWindow.cpp | 52 | ||||
-rw-r--r-- | Userland/Applications/Run/RunWindow.h | 12 | ||||
-rw-r--r-- | Userland/Applications/Run/main.cpp | 4 |
4 files changed, 61 insertions, 9 deletions
diff --git a/Userland/Applications/Run/Run.gml b/Userland/Applications/Run/Run.gml index 0856c9713e..650dfe570e 100644 --- a/Userland/Applications/Run/Run.gml +++ b/Userland/Applications/Run/Run.gml @@ -31,7 +31,7 @@ text_alignment: "CenterLeft" } - @GUI::TextBox { + @GUI::ComboBox { name: "path" } } diff --git a/Userland/Applications/Run/RunWindow.cpp b/Userland/Applications/Run/RunWindow.cpp index 8980f304e7..3050774a6a 100644 --- a/Userland/Applications/Run/RunWindow.cpp +++ b/Userland/Applications/Run/RunWindow.cpp @@ -25,10 +25,12 @@ */ #include "RunWindow.h" +#include <AK/LexicalPath.h> #include <AK/URL.h> #include <AK/URLParser.h> #include <Applications/Run/RunGML.h> #include <LibCore/File.h> +#include <LibCore/StandardPaths.h> #include <LibDesktop/Launcher.h> #include <LibGUI/Button.h> #include <LibGUI/Event.h> @@ -44,7 +46,11 @@ #include <unistd.h> RunWindow::RunWindow() + : m_path_history() + , m_path_history_model(GUI::ItemListModel<String>::create(m_path_history)) { + load_history(); + auto app_icon = GUI::Icon::default_icon("app-run"); set_title("Run"); @@ -59,8 +65,9 @@ RunWindow::RunWindow() m_icon_image_widget = *main_widget.find_descendant_of_type_named<GUI::ImageWidget>("icon"); m_icon_image_widget->set_bitmap(app_icon.bitmap_for_size(32)); - m_path_text_box = *main_widget.find_descendant_of_type_named<GUI::TextBox>("path"); - m_path_text_box->on_return_pressed = [this] { + m_path_combo_box = *main_widget.find_descendant_of_type_named<GUI::ComboBox>("path"); + m_path_combo_box->set_model(m_path_history_model); + m_path_combo_box->on_return_pressed = [this] { m_ok_button->click(); }; @@ -78,7 +85,7 @@ RunWindow::RunWindow() m_browse_button->on_click = [this](auto) { Optional<String> path = GUI::FilePicker::get_open_filepath(this); if (path.has_value()) - m_path_text_box->set_text(path.value().view()); + m_path_combo_box->set_text(path.value().view()); }; } @@ -102,11 +109,16 @@ void RunWindow::event(Core::Event& event) void RunWindow::do_run() { - auto run_input = m_path_text_box->text(); + auto run_input = m_path_combo_box->text().trim_whitespace(); hide(); if (run_via_launch(run_input) || run_as_command(run_input)) { + // Remove any existing history entry, prepend the successful run string to history and save. + m_path_history.remove_all_matching([&](String v) { return v == run_input; }); + m_path_history.prepend(run_input); + save_history(); + close(); return; } @@ -168,3 +180,35 @@ bool RunWindow::run_via_launch(const String& run_input) return true; } + +String RunWindow::history_file_path() +{ + return LexicalPath::canonicalized_path(String::formatted("{}/{}", Core::StandardPaths::config_directory(), "RunHistory.txt")); +} + +void RunWindow::load_history() +{ + m_path_history.clear(); + auto file_or_error = Core::File::open(history_file_path(), Core::IODevice::ReadOnly); + if (file_or_error.is_error()) + return; + + auto file = file_or_error.release_value(); + while (!file->eof()) { + auto line = file->read_line(); + if (!line.is_empty() && !line.is_whitespace()) + m_path_history.append(line); + } +} + +void RunWindow::save_history() +{ + auto file_or_error = Core::File::open(history_file_path(), Core::IODevice::WriteOnly); + if (file_or_error.is_error()) + return; + + auto file = file_or_error.release_value(); + // Write the first 25 items of history + for (int i = 0; i < min(static_cast<int>(m_path_history.size()), 25); i++) + file->write(String::formatted("{}\n", m_path_history[i])); +} diff --git a/Userland/Applications/Run/RunWindow.h b/Userland/Applications/Run/RunWindow.h index f8cd19ea61..5a99a7731b 100644 --- a/Userland/Applications/Run/RunWindow.h +++ b/Userland/Applications/Run/RunWindow.h @@ -27,8 +27,9 @@ #pragma once #include <LibGUI/Button.h> +#include <LibGUI/ComboBox.h> #include <LibGUI/ImageWidget.h> -#include <LibGUI/TextBox.h> +#include <LibGUI/ItemListModel.h> #include <LibGUI/Window.h> class RunWindow final : public GUI::Window { @@ -45,9 +46,16 @@ private: bool run_as_command(const String& run_input); bool run_via_launch(const String& run_input); + String history_file_path(); + void load_history(); + void save_history(); + + Vector<String> m_path_history; + NonnullRefPtr<GUI::ItemListModel<String>> m_path_history_model; + RefPtr<GUI::ImageWidget> m_icon_image_widget; RefPtr<GUI::Button> m_ok_button; RefPtr<GUI::Button> m_cancel_button; RefPtr<GUI::Button> m_browse_button; - RefPtr<GUI::TextBox> m_path_text_box; + RefPtr<GUI::ComboBox> m_path_combo_box; }; diff --git a/Userland/Applications/Run/main.cpp b/Userland/Applications/Run/main.cpp index fe83a13b2c..706215a49c 100644 --- a/Userland/Applications/Run/main.cpp +++ b/Userland/Applications/Run/main.cpp @@ -35,14 +35,14 @@ int main(int argc, char** argv) { - if (pledge("stdio recvfd sendfd thread accept cpath rpath unix fattr proc exec", nullptr) < 0) { + if (pledge("stdio recvfd sendfd thread accept cpath rpath wpath unix fattr proc exec", nullptr) < 0) { perror("pledge"); return 1; } auto app = GUI::Application::construct(argc, argv); - if (pledge("stdio recvfd sendfd thread accept rpath unix proc exec", nullptr) < 0) { + if (pledge("stdio recvfd sendfd thread accept cpath rpath wpath unix proc exec", nullptr) < 0) { perror("pledge"); return 1; } |