From 190c26cd76bd535ac88231ed61d157dea33aacbe Mon Sep 17 00:00:00 2001 From: Jeevitha Kannan K S Date: Sun, 10 Nov 2024 16:52:03 +0530 Subject: [PATCH] Fix scroll beyond list, color bleeding and refact in confirmation.rs Remove unnecessary usage of pub in ConfirmPropmt struct fields, simplify numbering, prevent scrolling beyond list, fix color bleeding --- tui/src/confirmation.rs | 50 +++++++++++++++++++---------------------- tui/src/state.rs | 4 ++-- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/tui/src/confirmation.rs b/tui/src/confirmation.rs index 6e130e5a..64b160e0 100644 --- a/tui/src/confirmation.rs +++ b/tui/src/confirmation.rs @@ -14,35 +14,30 @@ pub enum ConfirmStatus { } pub struct ConfirmPrompt { - pub names: Box<[String]>, - pub status: ConfirmStatus, + inner_area_height: usize, + names: Box<[String]>, scroll: usize, + pub status: ConfirmStatus, } impl ConfirmPrompt { - pub fn new(names: &[&str]) -> Self { - let max_count_str = format!("{}", names.len()); + pub fn new(names: Vec<&str>) -> Self { let names = names .iter() .zip(1..) - .map(|(name, n)| { - let count_str = format!("{n}"); - let space_str = (0..(max_count_str.len() - count_str.len())) - .map(|_| ' ') - .collect::(); - format!("{space_str}{n}. {name}") - }) + .map(|(name, n)| format!(" {n}. {name}")) .collect(); Self { + inner_area_height: 0, names, - status: ConfirmStatus::None, scroll: 0, + status: ConfirmStatus::None, } } pub fn scroll_down(&mut self) { - if self.scroll < self.names.len() - 1 { + if self.scroll + self.inner_area_height < self.names.len() - 1 { self.scroll += 1; } } @@ -72,9 +67,11 @@ impl FloatContent for ConfirmPrompt { .title_style(Style::default().bold()) .style(Style::default()); - frame.render_widget(block.clone(), area); - let inner_area = block.inner(area); + self.inner_area_height = inner_area.height as usize; + + frame.render_widget(Clear, area); + frame.render_widget(block, area); let paths_text = self .names @@ -86,26 +83,25 @@ impl FloatContent for ConfirmPrompt { }) .collect::(); - frame.render_widget(Clear, inner_area); frame.render_widget(List::new(paths_text), inner_area); } fn handle_key_event(&mut self, key: &KeyEvent) -> bool { - use KeyCode::*; + use ConfirmStatus::*; + use KeyCode::{Char, Down, Esc, Up}; self.status = match key.code { - Char('y') | Char('Y') => ConfirmStatus::Confirm, - Char('n') | Char('N') | Esc | Char('q') => ConfirmStatus::Abort, - Char('j') => { + Char('y') | Char('Y') => Confirm, + Char('n') | Char('N') | Esc | Char('q') => Abort, + Char('j') | Char('J') | Down => { self.scroll_down(); - ConfirmStatus::None + None } - Char('k') => { + Char('k') | Char('K') | Up => { self.scroll_up(); - ConfirmStatus::None + None } - _ => ConfirmStatus::None, + _ => None, }; - false } @@ -123,8 +119,8 @@ impl FloatContent for ConfirmPrompt { Box::new([ Shortcut::new("Continue", ["Y", "y"]), Shortcut::new("Abort", ["N", "n", "q", "Esc"]), - Shortcut::new("Scroll up", ["k"]), - Shortcut::new("Scroll down", ["j"]), + Shortcut::new("Scroll up", ["k", "Up"]), + Shortcut::new("Scroll down", ["j", "Down"]), Shortcut::new("Close linutil", ["CTRL-c"]), ]), ) diff --git a/tui/src/state.rs b/tui/src/state.rs index 7b31deca..ed320a2d 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -137,7 +137,7 @@ impl AppState { .map(|node| node.name.as_str()) .collect(); - let prompt = ConfirmPrompt::new(&cmd_names); + let prompt = ConfirmPrompt::new(cmd_names); self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40)); } } @@ -798,7 +798,7 @@ impl AppState { .map(|node| node.name.as_str()) .collect::>(); - let prompt = ConfirmPrompt::new(&cmd_names[..]); + let prompt = ConfirmPrompt::new(cmd_names); self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40)); } }