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

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)
This commit is contained in:
Nick Vella 2021-02-15 20:49:37 +11:00 committed by Andreas Kling
parent 05914d2e9a
commit bafb8b0be6
4 changed files with 61 additions and 9 deletions

View file

@ -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]));
}