add status line

This commit is contained in:
alice pellerin
2026-03-17 03:51:36 -05:00
parent a8e0a4fdb0
commit 86f6c43651
5 changed files with 94 additions and 38 deletions
+29 -6
View File
@@ -1,5 +1,6 @@
use std::{cmp::min, env, fs::File, io::Read, process::exit};
use std::{cmp::min, env, fs::File, io::Read, path::PathBuf, process::exit};
use crossterm::{event::{self, Event, KeyCode, KeyModifiers}, terminal::window_size};
use ratatui::style::Color;
use crate::{BYTES_PER_LINE, cursor::Cursor};
@@ -7,6 +8,7 @@ mod widget;
#[derive(Debug)]
pub struct App {
pub file_name: String,
pub contents: Vec<u8>,
pub window_rows: usize,
pub scroll_position: usize,
@@ -19,7 +21,7 @@ pub struct App {
#[derive(Debug)]
pub enum Mode {
Normal, Visual, Insert
Normal, Select, Insert
}
#[derive(Debug)]
@@ -27,6 +29,24 @@ pub enum PartialShortcut {
Goto, Zview
}
impl Mode {
pub const fn label(&self) -> &'static str {
match self {
Self::Normal => " NORMAL ",
Self::Select => " SELECT ",
Self::Insert => " INSERT ",
}
}
pub const fn color(&self) -> Color {
match self {
Self::Normal => Color::Blue,
Self::Select => Color::Yellow,
Self::Insert => Color::Green,
}
}
}
impl App {
pub fn init() -> Self {
let input_files: Vec<_> = env::args().skip(1).collect();
@@ -38,15 +58,17 @@ impl App {
assert!(input_files.len() == 1);
let file_name = input_files.first().unwrap();
let file_path: PathBuf = input_files.first().unwrap().into();
let file = File::open(file_name);
let file = File::open(&file_path);
let mut contents = Vec::new();
file.unwrap().read_to_end(&mut contents).unwrap();
Self {
file_name: file_path.file_name().unwrap().to_str().unwrap().to_owned(),
contents,
window_rows: window_size().unwrap().rows as usize,
// -1 because of the status line
window_rows: window_size().unwrap().rows as usize - 1,
scroll_position: 0,
cursor: Cursor::default(),
should_quit: false,
@@ -66,7 +88,8 @@ impl App {
#[allow(clippy::collapsible_match)]
match (&self.mode, event::read().unwrap(), &self.partial_shortcut) {
(Mode::Normal, Event::Resize(_, height), _) => {
self.window_rows = height as usize;
// -1 because of the status line
self.window_rows = height as usize - 1;
}
(Mode::Normal, Event::Key(key_event), None)
+46 -20
View File
@@ -1,14 +1,13 @@
use std::{cmp::min, iter};
use ratatui::{text::{Line, Span, Text}, widgets::Widget};
use ratatui::{buffer::Buffer, layout::Rect, text::{Line, Text}, widgets::Widget};
use crate::{BYTES_PER_LINE, app::App};
impl Widget for &App {
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
let screen_end = self.scroll_position + BYTES_PER_LINE * (area.height as usize);
fn render(self, area: Rect, buf: &mut Buffer) {
let screen_end = self.scroll_position + BYTES_PER_LINE * (area.height as usize - 1);
let bytes_end = min(screen_end, self.contents.len());
// TODO: bounds check this
let bytes_to_render = &self.contents[self.scroll_position..bytes_end];
let (chunks, remainder) = bytes_to_render
@@ -16,28 +15,28 @@ impl Widget for &App {
assert!(remainder.is_empty());
let lines: Vec<_> = chunks
let hex_lines = chunks
.iter()
.zip((self.scroll_position..).step_by(BYTES_PER_LINE))
.map(|(bytes, address)| self.render_line(address, bytes))
.collect();
.map(|(bytes, address)| self.render_line(address, bytes));
let text = Text::from(lines);
let hex_area_text: Text = hex_lines.collect();
let hex_area = Rect::new(area.x, area.y, area.width, area.height - 1);
hex_area_text.render(hex_area, buf);
text.render(area, buf);
let status_line_area = Rect::new(area.x, area.bottom() - 1, area.width, 1);
self.render_status_line().render(status_line_area, buf);
}
}
impl App {
#[allow(mismatched_lifetime_syntaxes)]
fn render_line(&self, address: usize, bytes: &[u8; BYTES_PER_LINE]) -> Line {
let spans: Vec<Span<'_>> = iter::once(address::render_address(address))
iter::once(address::render_address(address))
.chain(self.render_chunks(address, bytes))
.chain(iter::once(" ".into()))
.chain(character_panel::render_character_panel(bytes))
.collect();
Line::from(spans)
.collect()
}
}
@@ -57,14 +56,14 @@ mod hex {
use itertools::Itertools;
use ratatui::{style::{Color, Style, Stylize}, text::Span};
use crate::{BYTES_PER_CHUNK, BYTES_PER_LINE, CHUNKS_PER_LINE, app::App, cardinality::HasCardinality, cursor::InCursor, empty_span::empty_span, select_grey::SelectGrey};
use crate::{BYTES_PER_CHUNK, BYTES_PER_LINE, CHUNKS_PER_LINE, app::App, cardinality::HasCardinality, cursor::InCursor, empty_span::empty_span, custom_greys::CustomGreys};
impl App {
pub fn render_chunks(
&self,
address: usize,
bytes: &[u8; BYTES_PER_LINE]
) -> impl IntoIterator<Item=Span<'static>> {
) -> impl Iterator<Item=Span<'static>> {
let (chunks, remainder) = bytes.as_chunks::<BYTES_PER_CHUNK>();
assert!(remainder.is_empty());
@@ -74,7 +73,7 @@ mod hex {
.iter()
.copied()
.zip((address..).step_by(BYTES_PER_CHUNK))
.map(|(chunk, address)| self.render_chunk(address, chunk))
.map(|(chunk, address)| self.render_chunk(address, &chunk).collect())
.interleave(
(address..)
.step_by(BYTES_PER_CHUNK)
@@ -88,8 +87,8 @@ mod hex {
fn render_chunk(
&self,
address: usize,
bytes: [u8; BYTES_PER_CHUNK]
) -> Vec<Span<'static>> {
bytes: &[u8; BYTES_PER_CHUNK]
) -> impl Iterator<Item=Span<'static>> {
#[allow(unstable_name_collisions)]
bytes
.iter()
@@ -102,7 +101,6 @@ mod hex {
.skip(1)
.map(|address| self.render_space_before(address))
)
.collect()
}
fn render_byte_at(
@@ -203,7 +201,7 @@ mod character_panel {
pub fn render_character_panel(
bytes: &[u8; BYTES_PER_LINE]
) -> impl IntoIterator<Item=Span<'static>> {
) -> impl Iterator<Item=Span<'static>> {
bytes
.iter()
.copied()
@@ -257,3 +255,31 @@ mod character_panel {
LOOK_UP_TABLE[byte as usize]
}
}
mod status_line {
use crate::{app::App, custom_greys::CustomGreys};
use ratatui::{style::{Color, Stylize}, text::{Line, Span, Text}};
impl App {
pub fn render_status_line(&self) -> Text<'_> {
Text::from(
Line::from_iter([
self.render_mode(),
" ".into(),
self.render_file_name()
])
)
.bg(Color::ui_grey())
}
fn render_mode(&self) -> Span<'static> {
Span::from(self.mode.label())
.fg(Color::Black)
.bg(self.mode.color())
}
fn render_file_name(&self) -> Span<'_> {
Span::from(&self.file_name)
}
}
}