editor: Add basic loading and saving
This commit is contained in:
parent
d996e3f9f8
commit
aae5f21f7f
@ -31,6 +31,11 @@ Result<void> 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<ui::EventResult> 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<ui::EventResult> 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<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)
|
||||
{
|
||||
int visible_lines = canvas.height / m_font->height();
|
||||
|
@ -19,6 +19,8 @@ class EditorWidget : public ui::Widget
|
||||
|
||||
Result<void> load_file(const os::Path& path);
|
||||
|
||||
Result<void> save_file();
|
||||
|
||||
Result<ui::EventResult> handle_key_event(const ui::KeyEventRequest& request) override;
|
||||
|
||||
Result<void> 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<void> recalculate_lines();
|
||||
|
@ -8,10 +8,19 @@
|
||||
*/
|
||||
|
||||
#include "EditorWidget.h"
|
||||
#include <os/ArgumentParser.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;
|
||||
TRY(app.init());
|
||||
|
||||
@ -21,7 +30,7 @@ Result<int> luna_main(int, char**)
|
||||
app.set_main_window(window);
|
||||
|
||||
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->draw();
|
||||
|
Loading…
Reference in New Issue
Block a user