Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## dz6 v0.7.0

- Hex view:
- Replace mode:
- `t` now truncates the file (it was `T` before).
- `T` reverse truncates the file (deletes from offset 0 to current offset).
- Both commands above need confirmation from the user as they can't be undone.

## dz6 v0.6.0

- Hex view:
Expand Down
2 changes: 2 additions & 0 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ pub enum UIState {
DialogLog,
DialogNames,
DialogNamesRegex,
DialogReverseTruncate,
DialogSearch,
DialogStrings,
DialogStringsRegex,
DialogTruncate,
Error,
HexEditing,
HexSelection,
Expand Down
4 changes: 4 additions & 0 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ pub fn handle_events(app: &mut App) -> Result<bool> {
UIState::DialogCalculator => {
global::calculator::dialog_calculator_events(app, &event)?
}
UIState::DialogTruncate => hex::truncate::dialog_truncate_events(app, &event)?,
UIState::DialogReverseTruncate => {
hex::truncate::dialog_reverse_truncate_events(app, &event)?
}
};
}
Event::Resize(width, _height) => {
Expand Down
8 changes: 7 additions & 1 deletion src/global/status_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ pub fn status_bar_draw(app: &mut App, frame: &mut Frame, area: Rect) {
frame.render_widget(status_bar_info_left, area);

let selected = if app.state == UIState::HexSelection {
format!("{:X}", app.hex_view.selection.end.saturating_sub(app.hex_view.selection.start))
format!(
"{:X}",
app.hex_view
.selection
.end
.saturating_sub(app.hex_view.selection.start)
)
} else {
"".to_string()
};
Expand Down
16 changes: 10 additions & 6 deletions src/hex/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,16 @@ pub fn edit_events(app: &mut App, key: KeyEvent) -> Result<bool> {
fill_with(app, b.wrapping_sub(1), false);
}
} else if c == 'T' {
// truncate the file
if let Some(f) = &app.file_info.file {
f.set_len((app.hex_view.offset + 1) as u64)?;
app.reload_file();
app.state = UIState::Normal;
app.hex_view.editing_hex = true;
// set a new start (reverse truncate)
if app.hex_view.offset > 0 {
app.dialog_renderer = Some(super::truncate::dialog_reverse_truncate);
app.state = UIState::DialogReverseTruncate;
}
} else if c == 't' {
// set a new end (truncate)
if app.hex_view.offset < app.file_info.size.saturating_sub(1) {
app.dialog_renderer = Some(super::truncate::dialog_truncate);
app.state = UIState::DialogTruncate;
}
} else if c == '~' {
if let Some(b) = app.read_u8(app.hex_view.offset) {
Expand Down
1 change: 1 addition & 0 deletions src/hex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pub mod names;
pub mod search;
pub mod selection;
pub mod strings;
pub mod truncate;
68 changes: 68 additions & 0 deletions src/hex/truncate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crossterm::event::{Event, KeyCode};
use ratatui::Frame;
use std::io::{Result, Write};

use crate::{
app::App,
editor::UIState,
widgets::{Message, MessageType},
};

pub fn dialog_truncate(app: &mut App, frame: &mut Frame) {
let mut dialog = Message::from(&format!(
"Permanently delete from offset {:X} to the end of file? (y/N)",
app.hex_view.offset.saturating_add(1)
));
dialog.kind = MessageType::Error;
dialog.render(app, frame);
}

pub fn dialog_truncate_events(app: &mut App, event: &Event) -> Result<bool> {
if let Event::Key(key) = event {
match key.code {
KeyCode::Char('y') => {
if let Some(f) = &app.file_info.file {
f.set_len((app.hex_view.offset + 1) as u64)?;
app.reload_file();
}
}
_ => {}
}
app.dialog_renderer = None;
app.state = UIState::Normal;
app.hex_view.editing_hex = true;
}
Ok(false)
}

pub fn dialog_reverse_truncate(app: &mut App, frame: &mut Frame) {
let mut dialog = Message::from(&format!(
"Permanently delete from offset 0 to {:X}? (y/N)",
app.hex_view.offset.saturating_sub(1)
));
dialog.kind = MessageType::Error;
dialog.render(app, frame);
}

pub fn dialog_reverse_truncate_events(app: &mut App, event: &Event) -> Result<bool> {
if let Event::Key(key) = event {
match key.code {
KeyCode::Char('y') => {
let buff = &mut app.file_info.get_buffer().to_vec();
let new_buff = buff.drain(app.hex_view.offset..);

if let Some(f) = &mut app.file_info.file {
f.write_all(new_buff.as_slice())?;
f.set_len(new_buff.len() as u64)?;
app.reload_file();
app.goto(0);
}
}
_ => {}
}
app.dialog_renderer = None;
app.state = UIState::Normal;
app.hex_view.editing_hex = true;
}
Ok(false)
}
Loading