summaryrefslogtreecommitdiff
path: root/Userland/Applications/Run
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2022-04-06 17:36:46 +0100
committerTim Flynn <trflynn89@pm.me>2022-09-20 07:48:45 -0400
commit2dea636f3393b2a5a721d4f8e9d1092a2c17e895 (patch)
treed9a2eb0b4245975b3c576fd4a09654c7d113f440 /Userland/Applications/Run
parentaee52a458b333664635a36c78a4abc34e9ce589f (diff)
downloadserenity-2dea636f3393b2a5a721d4f8e9d1092a2c17e895.zip
Run: Use Core::Stream API for command history file
Diffstat (limited to 'Userland/Applications/Run')
-rw-r--r--Userland/Applications/Run/RunWindow.cpp32
-rw-r--r--Userland/Applications/Run/RunWindow.h4
2 files changed, 19 insertions, 17 deletions
diff --git a/Userland/Applications/Run/RunWindow.cpp b/Userland/Applications/Run/RunWindow.cpp
index 7f89868abc..e88eed6469 100644
--- a/Userland/Applications/Run/RunWindow.cpp
+++ b/Userland/Applications/Run/RunWindow.cpp
@@ -11,6 +11,7 @@
#include <Applications/Run/RunGML.h>
#include <LibCore/File.h>
#include <LibCore/StandardPaths.h>
+#include <LibCore/Stream.h>
#include <LibDesktop/Launcher.h>
#include <LibGUI/Button.h>
#include <LibGUI/Event.h>
@@ -29,7 +30,8 @@ RunWindow::RunWindow()
: m_path_history()
, m_path_history_model(GUI::ItemListModel<String>::create(m_path_history))
{
- load_history();
+ // FIXME: Handle failure to load history somehow.
+ (void)load_history();
auto app_icon = GUI::Icon::default_icon("app-run"sv);
@@ -95,7 +97,8 @@ void RunWindow::do_run()
// 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();
+ // FIXME: Handle failure to save history somehow.
+ (void)save_history();
close();
return;
@@ -164,29 +167,28 @@ String RunWindow::history_file_path()
return LexicalPath::canonicalized_path(String::formatted("{}/{}", Core::StandardPaths::config_directory(), "RunHistory.txt"));
}
-void RunWindow::load_history()
+ErrorOr<void> RunWindow::load_history()
{
m_path_history.clear();
- auto file_or_error = Core::File::open(history_file_path(), Core::OpenMode::ReadOnly);
- if (file_or_error.is_error())
- return;
+ auto file = TRY(Core::Stream::File::open(history_file_path(), Core::Stream::OpenMode::Read));
+ auto buffered_file = TRY(Core::Stream::BufferedFile::create(move(file)));
+ Array<u8, PAGE_SIZE> line_buffer;
- auto file = file_or_error.release_value();
- while (!file->eof()) {
- auto line = file->read_line();
+ while (!buffered_file->is_eof()) {
+ StringView line = TRY(buffered_file->read_line(line_buffer));
if (!line.is_empty() && !line.is_whitespace())
m_path_history.append(line);
}
+ return {};
}
-void RunWindow::save_history()
+ErrorOr<void> RunWindow::save_history()
{
- auto file_or_error = Core::File::open(history_file_path(), Core::OpenMode::WriteOnly);
- if (file_or_error.is_error())
- return;
+ auto file = TRY(Core::Stream::File::open(history_file_path(), Core::Stream::OpenMode::Write));
- 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]));
+ TRY(file->write(String::formatted("{}\n", m_path_history[i]).bytes()));
+
+ return {};
}
diff --git a/Userland/Applications/Run/RunWindow.h b/Userland/Applications/Run/RunWindow.h
index ca19e41350..5ae6b483e1 100644
--- a/Userland/Applications/Run/RunWindow.h
+++ b/Userland/Applications/Run/RunWindow.h
@@ -28,8 +28,8 @@ private:
bool run_via_launch(String const& run_input);
String history_file_path();
- void load_history();
- void save_history();
+ ErrorOr<void> load_history();
+ ErrorOr<void> save_history();
Vector<String> m_path_history;
NonnullRefPtr<GUI::ItemListModel<String>> m_path_history_model;