Add a basic GUI text editor #45

Merged
apio merged 9 commits from text-editor into main 2024-04-15 17:06:05 +00:00
Showing only changes of commit 0c8fe315db - Show all commits

View File

@ -11,6 +11,7 @@
#include <ctype.h>
#include <luna/Utf8.h>
#include <os/File.h>
#include <os/FileSystem.h>
#include <ui/App.h>
EditorWidget::EditorWidget(SharedPtr<ui::Font> font) : ui::Widget(), m_font(font)
@ -21,6 +22,15 @@ EditorWidget::EditorWidget(SharedPtr<ui::Font> font) : ui::Widget(), m_font(font
Result<void> EditorWidget::load_file(const os::Path& path)
{
struct stat st;
TRY(os::FileSystem::stat(path, st, true));
if (!S_ISREG(st.st_mode))
{
os::eprintln("editor: not loading %s as it is not a regular file", path.name().chars());
return {};
}
os::eprintln("Loading file: %s", path.name().chars());
auto file = TRY(os::File::open_or_create(path, os::File::ReadOnly));
@ -98,10 +108,10 @@ Result<ui::EventResult> EditorWidget::handle_key_event(const ui::KeyEventRequest
auto result = save_file();
if (result.has_error())
{
os::eprintln("TextEditor: failed to save file: %s", result.error_string());
os::eprintln("editor: failed to save file: %s", result.error_string());
return ui::EventResult::DidNotHandle;
}
os::println("TextEditor: buffer saved to %s successfully", m_path.name().chars());
os::println("editor: buffer saved to %s successfully", m_path.name().chars());
return ui::EventResult::DidNotHandle;
}
default: return ui::EventResult::DidNotHandle;
@ -150,7 +160,7 @@ Result<void> EditorWidget::save_file()
{
if (m_path.is_empty_path())
{
os::eprintln("TextEditor: no file to save buffer to!");
os::eprintln("editor: no file to save buffer to!");
return err(ENOENT);
}