From bafb8b0be69c0452b7974aa4616976d572db0f21 Mon Sep 17 00:00:00 2001 From: Nick Vella Date: Mon, 15 Feb 2021 20:49:37 +1100 Subject: 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) --- Userland/Applications/Run/Run.gml | 2 +- Userland/Applications/Run/RunWindow.cpp | 52 ++++++++++++++++++++++++++++++--- Userland/Applications/Run/RunWindow.h | 12 ++++++-- 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 #include #include #include #include +#include #include #include #include @@ -44,7 +46,11 @@ #include RunWindow::RunWindow() + : m_path_history() + , m_path_history_model(GUI::ItemListModel::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("icon"); m_icon_image_widget->set_bitmap(app_icon.bitmap_for_size(32)); - m_path_text_box = *main_widget.find_descendant_of_type_named("path"); - m_path_text_box->on_return_pressed = [this] { + m_path_combo_box = *main_widget.find_descendant_of_type_named("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 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(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 +#include #include -#include +#include #include 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 m_path_history; + NonnullRefPtr> m_path_history_model; + RefPtr m_icon_image_widget; RefPtr m_ok_button; RefPtr m_cancel_button; RefPtr m_browse_button; - RefPtr m_path_text_box; + RefPtr 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; } -- cgit v1.2.3