From f0ce6c36dee2c866de12e5886f8ba7d7404cfcc6 Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 27 Feb 2024 20:11:14 +0100 Subject: [PATCH] editor: Add basic loading and saving --- editor/EditorWidget.cpp | 35 +++++++++++++++++++++++++++++++++-- editor/EditorWidget.h | 4 ++++ editor/main.cpp | 13 +++++++++++-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/editor/EditorWidget.cpp b/editor/EditorWidget.cpp index 2481fe6a..094053db 100644 --- a/editor/EditorWidget.cpp +++ b/editor/EditorWidget.cpp @@ -31,6 +31,11 @@ Result EditorWidget::load_file(const os::Path& path) 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()); return {}; @@ -46,7 +51,9 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest if (request.code == moon::K_Esc) { 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) @@ -127,13 +134,37 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest } case 'i': { 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; } + 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; } } +Result 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 EditorWidget::draw(ui::Canvas& canvas) { int visible_lines = canvas.height / m_font->height(); diff --git a/editor/EditorWidget.h b/editor/EditorWidget.h index 7b0730f8..418c269e 100644 --- a/editor/EditorWidget.h +++ b/editor/EditorWidget.h @@ -19,6 +19,8 @@ class EditorWidget : public ui::Widget Result load_file(const os::Path& path); + Result save_file(); + Result handle_key_event(const ui::KeyEventRequest& request) override; Result draw(ui::Canvas& canvas) override; @@ -43,6 +45,8 @@ class EditorWidget : public ui::Widget bool m_insert = false; + os::Path m_path { AT_FDCWD }; + void tick_cursor(); Result recalculate_lines(); diff --git a/editor/main.cpp b/editor/main.cpp index fdea5393..57d0012e 100644 --- a/editor/main.cpp +++ b/editor/main.cpp @@ -8,10 +8,19 @@ */ #include "EditorWidget.h" +#include #include -Result luna_main(int, char**) +Result 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; TRY(app.init()); @@ -21,7 +30,7 @@ Result luna_main(int, char**) app.set_main_window(window); auto* editor = TRY(make(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->draw();