diff --git a/src/action.rs b/src/action.rs index 79a3caa..b8e62a3 100644 --- a/src/action.rs +++ b/src/action.rs @@ -1,5 +1,5 @@ use std::{cmp::min, fs::File, io::Write, mem::{replace, swap}}; - +use ratatui::{style::Stylize, text::Span}; use crate::{BYTES_PER_LINE, app::{App, Mode, PartialAction}, edit_action::EditAction}; #[derive(Clone, Copy)] @@ -120,11 +120,13 @@ impl App { } } - const fn quit_if_saved(&mut self) { + fn quit_if_saved(&mut self) { if self.all_changes_saved() { self.quit(); } else { - // TODO: show alert of some kind + self.alert_message = Span::from( + "there are unsaved changes, use Q to override" + ).red(); } } @@ -227,8 +229,11 @@ impl App { self.cursor.collapse(); } - const fn goto_line_end(&mut self) { - self.cursor.head += BYTES_PER_LINE - 1 - (self.cursor.head % BYTES_PER_LINE); + fn goto_line_end(&mut self) { + self.cursor.head = min( + self.cursor.head + BYTES_PER_LINE - 1 - (self.cursor.head % BYTES_PER_LINE), + self.max_contents_index() + ); self.cursor.collapse(); } diff --git a/src/app/mod.rs b/src/app/mod.rs index e55777c..7ba2b8e 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,6 +1,6 @@ use std::{env, fs::File, io::Read, path::PathBuf, process::exit}; use crossterm::{event::{self, Event, KeyEvent}, terminal::window_size}; -use ratatui::style::Color; +use ratatui::{style::Color, text::Span}; use crate::{config::Config, cursor::Cursor, edit_action::EditAction}; mod widget; @@ -29,6 +29,8 @@ pub struct App { // the index *after* the last saved edit action pub last_saved_at: Option, + pub alert_message: Span<'static>, + pub logs: Vec, } @@ -111,6 +113,8 @@ impl App { time_traveling: None, last_saved_at: Some(0), + alert_message: "".into(), + logs: Vec::new(), } } @@ -132,6 +136,8 @@ impl App { } fn handle_key(&mut self, event: KeyEvent) { + self.alert_message = "".into(); + if self.partial_action == Some(PartialAction::Replace) { if let Some(hex_character) = event.code.as_char() && let Some(nybble) = nybble_from_hex(hex_character) diff --git a/src/app/widget.rs b/src/app/widget.rs index a4304d6..98c14b7 100644 --- a/src/app/widget.rs +++ b/src/app/widget.rs @@ -200,7 +200,7 @@ mod hex { let replaced_byte = self.partial_replace.unwrap_or(0) << 4; self.render_byte_without_replace_preview(address, replaced_byte) - .fg(Color::Black) + .black() } else { self.render_byte_without_replace_preview(address, byte) } @@ -330,7 +330,7 @@ mod character_panel { match self.cursor.contains(address) { Some(InCursor::Head) => span.bg(Color::select_grey()), - Some(InCursor::Rest) => span.bg(Color::DarkGray), + Some(InCursor::Rest) => span.on_dark_gray(), None => span, } } @@ -388,7 +388,9 @@ mod status_line { self.render_mode(), " ".into(), self.render_file_name(), - self.modified_indicator() + self.modified_indicator(), + " ".into(), + self.alert_message.clone() ]) ) .bg(Color::ui_grey()) @@ -396,7 +398,7 @@ mod status_line { fn render_mode(&self) -> Span<'static> { Span::from(self.mode.label()) - .fg(Color::Black) + .black() .bg(self.mode.color()) } diff --git a/src/main.rs b/src/main.rs index f270f57..a602836 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,8 +50,6 @@ const CHUNKS_PER_LINE: usize = BYTES_PER_LINE / BYTES_PER_CHUNK; // - utf8? // - diffing -// TODO: quit should confirm if unsaved changes - // when AsciiChar is stabilized, use it instead of char everywhere fn main() {