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
This commit is contained in:
Jeevitha Kannan K S 2024-11-10 16:52:03 +05:30
parent 3b7e859af8
commit b8f0b82d5d
No known key found for this signature in database
GPG Key ID: 5904C34A2F7CE333
2 changed files with 23 additions and 27 deletions

View File

@ -14,35 +14,30 @@ pub enum ConfirmStatus {
} }
pub struct ConfirmPrompt { pub struct ConfirmPrompt {
pub names: Box<[String]>, inner_area_height: usize,
pub status: ConfirmStatus, names: Box<[String]>,
scroll: usize, scroll: usize,
pub status: ConfirmStatus,
} }
impl ConfirmPrompt { impl ConfirmPrompt {
pub fn new(names: &[&str]) -> Self { pub fn new(names: Vec<&str>) -> Self {
let max_count_str = format!("{}", names.len());
let names = names let names = names
.iter() .iter()
.zip(1..) .zip(1..)
.map(|(name, n)| { .map(|(name, n)| format!(" {n}. {name}"))
let count_str = format!("{n}");
let space_str = (0..(max_count_str.len() - count_str.len()))
.map(|_| ' ')
.collect::<String>();
format!("{space_str}{n}. {name}")
})
.collect(); .collect();
Self { Self {
inner_area_height: 0,
names, names,
status: ConfirmStatus::None,
scroll: 0, scroll: 0,
status: ConfirmStatus::None,
} }
} }
pub fn scroll_down(&mut self) { 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; self.scroll += 1;
} }
} }
@ -72,9 +67,11 @@ impl FloatContent for ConfirmPrompt {
.title_style(Style::default().bold()) .title_style(Style::default().bold())
.style(Style::default()); .style(Style::default());
frame.render_widget(block.clone(), area);
let inner_area = block.inner(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 let paths_text = self
.names .names
@ -86,26 +83,25 @@ impl FloatContent for ConfirmPrompt {
}) })
.collect::<Text>(); .collect::<Text>();
frame.render_widget(Clear, inner_area);
frame.render_widget(List::new(paths_text), inner_area); frame.render_widget(List::new(paths_text), inner_area);
} }
fn handle_key_event(&mut self, key: &KeyEvent) -> bool { fn handle_key_event(&mut self, key: &KeyEvent) -> bool {
use KeyCode::*; use ConfirmStatus::*;
use KeyCode::{Char, Down, Esc, Up};
self.status = match key.code { self.status = match key.code {
Char('y') | Char('Y') => ConfirmStatus::Confirm, Char('y') | Char('Y') => Confirm,
Char('n') | Char('N') | Esc | Char('q') => ConfirmStatus::Abort, Char('n') | Char('N') | Esc | Char('q') => Abort,
Char('j') => { Char('j') | Char('J') | Down => {
self.scroll_down(); self.scroll_down();
ConfirmStatus::None None
} }
Char('k') => { Char('k') | Char('K') | Up => {
self.scroll_up(); self.scroll_up();
ConfirmStatus::None None
} }
_ => ConfirmStatus::None, _ => None,
}; };
false false
} }

View File

@ -137,7 +137,7 @@ impl AppState {
.map(|node| node.name.as_str()) .map(|node| node.name.as_str())
.collect(); .collect();
let prompt = ConfirmPrompt::new(&cmd_names); let prompt = ConfirmPrompt::new(cmd_names);
self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40)); self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40));
} }
} }
@ -798,7 +798,7 @@ impl AppState {
.map(|node| node.name.as_str()) .map(|node| node.name.as_str())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let prompt = ConfirmPrompt::new(&cmd_names[..]); let prompt = ConfirmPrompt::new(cmd_names);
self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40)); self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40));
} }
} }