From f2e345b01b8b7457bb03b7bee0c825664c95f82d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 11 Dec 2021 05:27:40 -0500 Subject: display the history index as well --- src/history.rs | 18 +++++++++++++++++- src/main.rs | 1 + src/readline.rs | 6 +++++- src/state.rs | 25 ++++++++++++++++++++++--- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/history.rs b/src/history.rs index 0ba85ee..3028ef4 100644 --- a/src/history.rs +++ b/src/history.rs @@ -45,7 +45,15 @@ impl History { (self.size.0 as usize - used_lines).try_into().unwrap(), 0, ); - entry.render(out, self.size.1, focused, scrolling, offset); + entry.render( + out, + idx, + self.entry_count(), + self.size.1, + focused, + scrolling, + offset, + ); if focused && !scrolling { cursor = Some(( out.screen().cursor_position(), @@ -273,6 +281,8 @@ impl Entry { fn render( &mut self, out: &mut textmode::Output, + idx: usize, + entry_count: usize, width: u16, focused: bool, scrolling: bool, @@ -293,6 +303,12 @@ impl Entry { } out.reset_attributes(); self.set_bgcolor(out, focused); + let entry_count_width = format!("{}", entry_count).len(); + let idx_str = format!("{}", idx + 1); + out.write_str(&" ".repeat(entry_count_width - idx_str.len())); + out.write_str("["); + out.write_str(&idx_str); + out.write_str("]"); out.write_str("$ "); if self.running() { out.set_bgcolor(textmode::Color::Rgb(16, 64, 16)); diff --git a/src/main.rs b/src/main.rs index f7687cd..6f7046b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![warn(clippy::pedantic)] #![warn(clippy::nursery)] #![allow(clippy::missing_const_for_fn)] +#![allow(clippy::too_many_arguments)] #![allow(clippy::too_many_lines)] #![allow(clippy::unused_self)] diff --git a/src/readline.rs b/src/readline.rs index 306fed9..0be2cad 100644 --- a/src/readline.rs +++ b/src/readline.rs @@ -59,6 +59,7 @@ impl Readline { pub async fn render( &self, out: &mut textmode::Output, + entry_count: usize, focus: bool, offset: time::UtcOffset, ) -> anyhow::Result<()> { @@ -90,13 +91,16 @@ impl Readline { out.move_to(self.size.0 - 1, 0); out.reset_attributes(); + let idx_str = format!("[{}]", entry_count + 1); + let idx_str_len: u16 = idx_str.len().try_into().unwrap(); + out.write_str(&idx_str); out.write_str(&prompt_char); out.write_str(" "); out.reset_attributes(); out.write(b"\x1b[K"); out.write_str(&self.input_line); out.reset_attributes(); - out.move_to(self.size.0 - 1, 2 + self.pos_width()); + out.move_to(self.size.0 - 1, idx_str_len + 2 + self.pos_width()); if focus { out.hide_cursor(false); } diff --git a/src/state.rs b/src/state.rs index 0b54815..6ecfa07 100644 --- a/src/state.rs +++ b/src/state.rs @@ -165,7 +165,14 @@ impl State { self.offset, ) .await?; - self.readline.render(out, true, self.offset).await?; + self.readline + .render( + out, + self.history.entry_count(), + true, + self.offset, + ) + .await?; } crate::action::Focus::History(idx) => { if self.hide_readline { @@ -183,7 +190,14 @@ impl State { ) .await?; let pos = out.screen().cursor_position(); - self.readline.render(out, false, self.offset).await?; + self.readline + .render( + out, + self.history.entry_count(), + false, + self.offset, + ) + .await?; out.move_to(pos.0, pos.1); } } @@ -198,7 +212,12 @@ impl State { ) .await?; self.readline - .render(out, idx.is_none(), self.offset) + .render( + out, + self.history.entry_count(), + idx.is_none(), + self.offset, + ) .await?; out.hide_cursor(true); } -- cgit v1.2.3-54-g00ecf