editor: Add basic loading and saving
All checks were successful
Build and test / build (push) Successful in 1m54s

This commit is contained in:
apio 2024-02-27 20:11:14 +01:00
parent 2951c539b1
commit f0ce6c36de
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 48 additions and 4 deletions

View File

@ -31,6 +31,11 @@ Result<void> EditorWidget::load_file(const os::Path& path)
m_cursor = m_data.size(); m_cursor = m_data.size();
m_path = path;
String title = TRY(String::format("Text Editor - %s"_sv, m_path.name().chars()));
ui::App::the().main_window()->set_title(title.view());
TRY(recalculate_lines()); TRY(recalculate_lines());
return {}; return {};
@ -46,7 +51,9 @@ Result<ui::EventResult> EditorWidget::handle_key_event(const ui::KeyEventRequest
if (request.code == moon::K_Esc) if (request.code == moon::K_Esc)
{ {
m_insert = false; m_insert = false;
ui::App::the().main_window()->set_title("Text Editor"); String title = TRY(String::from_cstring("Text Editor"));
if (!m_path.is_empty_path()) title = TRY(String::format("Text Editor - %s"_sv, m_path.name().chars()));
ui::App::the().main_window()->set_title(title.view());
} }
if (request.code == moon::K_Backspace) if (request.code == moon::K_Backspace)
@ -127,13 +134,37 @@ Result<ui::EventResult> EditorWidget::handle_key_event(const ui::KeyEventRequest
} }
case 'i': { case 'i': {
m_insert = true; m_insert = true;
ui::App::the().main_window()->set_title("Text Editor (insert)"); String title = TRY(String::from_cstring("Text Editor (insert)"));
if (!m_path.is_empty_path()) title = TRY(String::format("Text Editor - %s (insert)"_sv, m_path.name().chars()));
ui::App::the().main_window()->set_title(title.view());
return ui::EventResult::DidHandle; return ui::EventResult::DidHandle;
} }
case 'f': {
auto result = save_file();
if (result.has_error())
{
os::eprintln("TextEditor: failed to save file: %s", result.error_string());
return ui::EventResult::DidNotHandle;
}
os::println("TextEditor: buffer saved to %s successfully", m_path.name().chars());
return ui::EventResult::DidNotHandle;
}
default: return ui::EventResult::DidNotHandle; default: return ui::EventResult::DidNotHandle;
} }
} }
Result<void> EditorWidget::save_file()
{
if (m_path.is_empty_path())
{
os::eprintln("TextEditor: no file to save buffer to!");
return err(ENOENT);
}
auto file = TRY(os::File::open(m_path, os::File::WriteOnly));
return file->write(m_data);
}
Result<void> EditorWidget::draw(ui::Canvas& canvas) Result<void> EditorWidget::draw(ui::Canvas& canvas)
{ {
int visible_lines = canvas.height / m_font->height(); int visible_lines = canvas.height / m_font->height();

View File

@ -19,6 +19,8 @@ class EditorWidget : public ui::Widget
Result<void> load_file(const os::Path& path); Result<void> load_file(const os::Path& path);
Result<void> save_file();
Result<ui::EventResult> handle_key_event(const ui::KeyEventRequest& request) override; Result<ui::EventResult> handle_key_event(const ui::KeyEventRequest& request) override;
Result<void> draw(ui::Canvas& canvas) override; Result<void> draw(ui::Canvas& canvas) override;
@ -43,6 +45,8 @@ class EditorWidget : public ui::Widget
bool m_insert = false; bool m_insert = false;
os::Path m_path { AT_FDCWD };
void tick_cursor(); void tick_cursor();
Result<void> recalculate_lines(); Result<void> recalculate_lines();

View File

@ -8,10 +8,19 @@
*/ */
#include "EditorWidget.h" #include "EditorWidget.h"
#include <os/ArgumentParser.h>
#include <ui/App.h> #include <ui/App.h>
Result<int> luna_main(int, char**) Result<int> luna_main(int argc, char** argv)
{ {
StringView path;
os::ArgumentParser parser;
parser.add_description("A graphical text editor"_sv);
parser.add_system_program_info("editor"_sv);
parser.add_positional_argument(path, "path", false);
parser.parse(argc, argv);
ui::App app; ui::App app;
TRY(app.init()); TRY(app.init());
@ -21,7 +30,7 @@ Result<int> luna_main(int, char**)
app.set_main_window(window); app.set_main_window(window);
auto* editor = TRY(make<EditorWidget>(ui::Font::default_font())); auto* editor = TRY(make<EditorWidget>(ui::Font::default_font()));
// TRY(editor->load_file("/etc/skel/welcome")); if (!path.is_empty()) TRY(editor->load_file(path));
window->set_main_widget(*editor); window->set_main_widget(*editor);
window->draw(); window->draw();