Skip to content

Commit

Permalink
HexEditor: Add an option to load an annotations file on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
AtkinsSJ committed Jan 31, 2024
1 parent 4cef57a commit d930ea1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Base/usr/share/man/man1/Applications/HexEditor.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
## Synopsis

```**sh
$ HexEditor
$ HexEditor file
$ HexEditor [--annotations <path>] [file]
```

## Description
Expand All @@ -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 changes: 14 additions & 0 deletions Userland/Applications/HexEditor/HexEditorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions Userland/Applications/HexEditor/HexEditorWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
5 changes: 5 additions & 0 deletions Userland/Applications/HexEditor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
}

0 comments on commit d930ea1

Please sign in to comment.