Browse Source

HexEditor: Add an option to load an annotations file on startup

Sam Atkins 1 year ago
parent
commit
d930ea1242

+ 9 - 2
Base/usr/share/man/man1/Applications/HexEditor.md

@@ -7,8 +7,7 @@
 ## Synopsis
 
 ```**sh
-$ HexEditor 
-$ HexEditor file
+$ HexEditor [--annotations <path>] [file]
 ```
 
 ## Description
@@ -35,3 +34,11 @@ An option to copy as hex value, as text, or as C-code is available and can extra
 ![](HexEditor_Copy_Hex_Text_C_Code.png)
 
 Hex Editor's simple and straight-forward interface offers search, export, byte pattern insertions and statistics. 
+
+## Options
+
+* `-a`, `--annotations`: Path to an annotations file to load on startup
+
+## Arguments
+
+* `file`: File to open on startup 

+ 14 - 0
Userland/Applications/HexEditor/HexEditorWidget.cpp

@@ -676,6 +676,20 @@ void HexEditorWidget::open_file(ByteString const& filename, NonnullOwnPtr<Core::
     GUI::Application::the()->set_most_recently_open_file(filename);
 }
 
+void HexEditorWidget::open_annotations_file(StringView filename)
+{
+    auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), filename);
+    if (response.is_error())
+        return;
+
+    auto result = m_editor->document().annotations().load_from_file(response.value().stream());
+    if (result.is_error()) {
+        GUI::MessageBox::show(window(), ByteString::formatted("Unable to load annotations: {}\n"sv, result.error()), "Error"sv, GUI::MessageBox::Type::Error);
+        return;
+    }
+    m_annotations_path = filename;
+}
+
 bool HexEditorWidget::request_close()
 {
     if (!window()->is_modified())

+ 1 - 0
Userland/Applications/HexEditor/HexEditorWidget.h

@@ -26,6 +26,7 @@ class HexEditorWidget final : public GUI::Widget {
 public:
     virtual ~HexEditorWidget() override = default;
     void open_file(ByteString const& filename, NonnullOwnPtr<Core::File>);
+    void open_annotations_file(StringView filename);
     ErrorOr<void> initialize_menubar(GUI::Window&);
     bool request_close();
 

+ 5 - 0
Userland/Applications/HexEditor/main.cpp

@@ -27,8 +27,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
     auto app = TRY(GUI::Application::create(arguments));
 
     StringView filename;
+    StringView annotations_filename;
 
     Core::ArgsParser args_parser;
+    args_parser.add_option(annotations_filename, "Annotations file to load", "annotations", 'a', "path");
     args_parser.add_positional_argument(filename, "File to open", "path", Core::ArgsParser::Required::No);
     args_parser.parse(arguments);
 
@@ -69,5 +71,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
             hex_editor_widget->open_file(response.value().filename(), response.value().release_stream());
     }
 
+    if (!annotations_filename.is_empty())
+        hex_editor_widget->open_annotations_file(annotations_filename);
+
     return app->exec();
 }