HexEditor: Use new format functions.
I changed the formatting for some numbers when it made sense, because I believe '0x00000020' to be more expressive than ' 20' for example.
This commit is contained in:
parent
c776d76b71
commit
aea5fff0d1
Notes:
sideshowbarker
2024-07-19 02:01:19 +09:00
Author: https://github.com/asynts Commit: https://github.com/SerenityOS/serenity/commit/aea5fff0d17 Pull-request: https://github.com/SerenityOS/serenity/pull/3696 Reviewed-by: https://github.com/awesomekling Reviewed-by: https://github.com/emanuele6
2 changed files with 19 additions and 21 deletions
|
@ -135,9 +135,8 @@ bool HexEditor::copy_selected_hex_to_clipboard()
|
|||
return false;
|
||||
|
||||
StringBuilder output_string_builder;
|
||||
for (int i = m_selection_start; i <= m_selection_end; i++) {
|
||||
output_string_builder.appendf("%02X ", m_buffer.data()[i]);
|
||||
}
|
||||
for (int i = m_selection_start; i <= m_selection_end; i++)
|
||||
output_string_builder.appendff("{:02X} ", m_buffer.data()[i]);
|
||||
|
||||
GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
|
||||
return true;
|
||||
|
@ -149,9 +148,8 @@ bool HexEditor::copy_selected_text_to_clipboard()
|
|||
return false;
|
||||
|
||||
StringBuilder output_string_builder;
|
||||
for (int i = m_selection_start; i <= m_selection_end; i++) {
|
||||
output_string_builder.appendf("%c", isprint(m_buffer.data()[i]) ? m_buffer[i] : '.');
|
||||
}
|
||||
for (int i = m_selection_start; i <= m_selection_end; i++)
|
||||
output_string_builder.append(isprint(m_buffer.data()[i]) ? m_buffer[i] : '.');
|
||||
|
||||
GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
|
||||
return true;
|
||||
|
@ -163,10 +161,10 @@ bool HexEditor::copy_selected_hex_to_clipboard_as_c_code()
|
|||
return false;
|
||||
|
||||
StringBuilder output_string_builder;
|
||||
output_string_builder.appendf("unsigned char raw_data[%d] = {\n", (m_selection_end - m_selection_start) + 1);
|
||||
output_string_builder.appendff("unsigned char raw_data[{}] = {{\n", (m_selection_end - m_selection_start) + 1);
|
||||
output_string_builder.append(" ");
|
||||
for (int i = m_selection_start, j = 1; i <= m_selection_end; i++, j++) {
|
||||
output_string_builder.appendf("0x%02X", m_buffer.data()[i]);
|
||||
output_string_builder.appendff("{:#04X}", m_buffer.data()[i]);
|
||||
if (i != m_selection_end)
|
||||
output_string_builder.append(", ");
|
||||
if ((j % 12) == 0) {
|
||||
|
@ -223,7 +221,7 @@ void HexEditor::mousedown_event(GUI::MouseEvent& event)
|
|||
return;
|
||||
|
||||
#ifdef HEX_DEBUG
|
||||
printf("HexEditor::mousedown_event(hex): offset=%d\n", offset);
|
||||
outln("HexEditor::mousedown_event(hex): offset={}", offset);
|
||||
#endif
|
||||
|
||||
m_edit_mode = EditMode::Hex;
|
||||
|
@ -245,7 +243,7 @@ void HexEditor::mousedown_event(GUI::MouseEvent& event)
|
|||
return;
|
||||
|
||||
#ifdef HEX_DEBUG
|
||||
printf("HexEditor::mousedown_event(text): offset=%d\n", offset);
|
||||
outln("HexEditor::mousedown_event(text): offset={}", offset);
|
||||
#endif
|
||||
|
||||
m_position = offset;
|
||||
|
@ -348,7 +346,7 @@ void HexEditor::scroll_position_into_view(int position)
|
|||
void HexEditor::keydown_event(GUI::KeyEvent& event)
|
||||
{
|
||||
#ifdef HEX_DEBUG
|
||||
printf("HexEditor::keydown_event key=%d\n", event.key());
|
||||
outln("HexEditor::keydown_event key={}", static_cast<u8>(event.key()));
|
||||
#endif
|
||||
|
||||
if (event.key() == KeyCode::Key_Up) {
|
||||
|
@ -518,7 +516,7 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
|
|||
};
|
||||
|
||||
bool is_current_line = (m_position / bytes_per_row()) == i;
|
||||
auto line = String::format("0x%08X", i * bytes_per_row());
|
||||
auto line = String::formatted("{:#010X}", i * bytes_per_row());
|
||||
painter.draw_text(
|
||||
side_offset_rect,
|
||||
line,
|
||||
|
@ -562,7 +560,7 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
|
|||
text_color = palette().inactive_selection_text();
|
||||
}
|
||||
|
||||
auto line = String::format("%02X", m_buffer[byte_position]);
|
||||
auto line = String::formatted("{:02X}", m_buffer[byte_position]);
|
||||
painter.draw_text(hex_display_rect, line, Gfx::TextAlignment::TopLeft, text_color);
|
||||
|
||||
Gfx::IntRect text_display_rect {
|
||||
|
@ -578,7 +576,7 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
|
|||
painter.fill_rect(text_display_rect, palette().inactive_selection());
|
||||
}
|
||||
|
||||
painter.draw_text(text_display_rect, String::format("%c", isprint(m_buffer[byte_position]) ? m_buffer[byte_position] : '.'), Gfx::TextAlignment::TopLeft, text_color);
|
||||
painter.draw_text(text_display_rect, String::formatted("{:c}", isprint(m_buffer[byte_position]) ? m_buffer[byte_position] : '.'), Gfx::TextAlignment::TopLeft, text_color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,11 +54,11 @@ HexEditorWidget::HexEditorWidget()
|
|||
m_editor = add<HexEditor>();
|
||||
|
||||
m_editor->on_status_change = [this](int position, HexEditor::EditMode edit_mode, int selection_start, int selection_end) {
|
||||
m_statusbar->set_text(0, String::format("Offset: %8X", position));
|
||||
m_statusbar->set_text(1, String::format("Edit Mode: %s", edit_mode == HexEditor::EditMode::Hex ? "Hex" : "Text"));
|
||||
m_statusbar->set_text(2, String::format("Selection Start: %d", selection_start));
|
||||
m_statusbar->set_text(3, String::format("Selection End: %d", selection_end));
|
||||
m_statusbar->set_text(4, String::format("Selected Bytes: %d", abs(selection_end - selection_start) + 1));
|
||||
m_statusbar->set_text(0, String::formatted("Offset: {:#010X}", position));
|
||||
m_statusbar->set_text(1, String::formatted("Edit Mode: {}", edit_mode == HexEditor::EditMode::Hex ? "Hex" : "Text"));
|
||||
m_statusbar->set_text(2, String::formatted("Selection Start: {}", selection_start));
|
||||
m_statusbar->set_text(3, String::formatted("Selection End: {}", selection_end));
|
||||
m_statusbar->set_text(4, String::formatted("Selected Bytes: {}", abs(selection_end - selection_start) + 1));
|
||||
};
|
||||
|
||||
m_editor->on_change = [this] {
|
||||
|
@ -126,7 +126,7 @@ HexEditorWidget::HexEditorWidget()
|
|||
|
||||
m_document_dirty = false;
|
||||
set_path(LexicalPath(save_path.value()));
|
||||
dbg() << "Wrote document to " << save_path.value();
|
||||
dbgln("Wrote document to {}", save_path.value());
|
||||
});
|
||||
|
||||
auto menubar = GUI::MenuBar::construct();
|
||||
|
@ -227,7 +227,7 @@ void HexEditorWidget::open_file(const String& path)
|
|||
{
|
||||
auto file = Core::File::construct(path);
|
||||
if (!file->open(Core::IODevice::ReadOnly)) {
|
||||
GUI::MessageBox::show(window(), String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);
|
||||
GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue