From c3fbc6abf17d09760a086e9c95d52abe93590454 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sat, 22 Mar 2025 20:50:55 +0200 Subject: [PATCH] edit the correct column --- src/model.rs | 8 +++---- src/view/table.rs | 61 ++++++++++++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/model.rs b/src/model.rs index 99bdb13..fd93e6a 100644 --- a/src/model.rs +++ b/src/model.rs @@ -86,15 +86,15 @@ impl Taggart { self.editing = None; } pub fn edit_insert (&mut self, c: char) { - if let Some((index, value)) = &mut self.editing { - self.editing = Some((*index, format!("{value}{c}"))); + if let Some((edit_index, value)) = &mut self.editing { + self.editing = Some((*edit_index, format!("{value}{c}"))); } } pub fn edit_backspace (&mut self) { - if let Some((index, value)) = &mut self.editing { + if let Some((edit_index, value)) = &mut self.editing { let mut chars = value.chars(); chars.next_back(); - self.editing = Some((*index, chars.as_str().into())); + self.editing = Some((*edit_index, chars.as_str().into())); } } pub fn edit_delete (&mut self) { diff --git a/src/view/table.rs b/src/view/table.rs index b7d02ce..51d86eb 100644 --- a/src/view/table.rs +++ b/src/view/table.rs @@ -4,50 +4,69 @@ pub struct TreeTable<'a>(pub(crate) &'a Taggart); impl<'a> Content for TreeTable<'a> { fn render (&self, to: &mut TuiOut) { - let Taggart { offset, paths, cursor, columns, column, .. } = self.0; - let (active_x, active_w) = columns.xw(*column); + let (active_x, active_w) = self.0.columns.xw(self.0.column); let active_w = active_w.saturating_sub(1); let area = to.area(); - to.fill_bg([area.x() + active_x, area.y(), active_w, area.h()], Color::Rgb(0, 0, 0)); + Self::column_cursor(to, area, active_x, active_w); + self.rows(to, area, active_x, active_w); + to.area = area; + } +} + +impl<'a> TreeTable<'a> { + const BG_COLUMN: Color = Color::Rgb(0, 0, 0); + const FG_ROW: Color = Color::Rgb(0, 0, 0); + const BG_ROW: Color = Color::Rgb(192, 128, 0); + const BG_CELL: Color = Color::Rgb(224, 192, 0); + const BG_EDIT: Color = Color::Rgb(48, 96, 0); + const FG_EDIT: Color = Color::Rgb(255, 255, 255); + fn rows (&self, to: &mut TuiOut, area: [u16;4], active_x: u16, active_w: u16) { for (row_index, row_y) in area.iter_y().enumerate() { - let row_index_scrolled = row_index + offset; - let selected = *cursor == row_index_scrolled; + let row_index_scrolled = row_index + self.0.offset; + let selected = self.0.cursor == row_index_scrolled; let mut column_x = area.x(); - if let Some(entry) = paths.get(row_index_scrolled) { + if let Some(entry) = self.0.paths.get(row_index_scrolled) { for (index, _fragment) in entry.path.iter().enumerate() { if index == entry.depth - 1 { let _cursor = if selected { ">" } else { " " }; to.area[1] = row_y; if selected { Self::row_cursor(to, area.x(), row_y, area.w()); + self.cell_cursor(to, area.x(), active_x, column_x, row_y, active_w); } self.row_data(to, entry, row_index_scrolled, &mut column_x); - if selected { - self.col_cursor(to, area.x(), active_x, column_x, row_y, active_w); - } } } } else { break } } - to.area = area; } -} - -impl<'a> TreeTable<'a> { + fn column_cursor (to: &mut TuiOut, area: [u16;4], active_x: u16, active_w: u16) { + to.fill_bg([area.x() + active_x, area.y(), active_w, area.h()], Self::BG_COLUMN); + } fn row_cursor (to: &mut TuiOut, x: u16, y: u16, w: u16) { let fill = [x, y, w, 1]; - to.fill_fg(fill, Color::Rgb(0, 0, 0)); - to.fill_bg(fill, Color::Rgb(192, 128, 0)); + to.fill_fg(fill, Self::FG_ROW); + to.fill_bg(fill, Self::BG_ROW); + } + fn cell_cursor (&self, to: &mut TuiOut, xa: u16, x0: u16, x: u16, y: u16, w: u16) { + let fill = [xa + x0, y, w, 1]; + to.fill_bg(fill, Self::BG_CELL); + //if let Some((_index, value)) = &self.0.editing { + //let x = xa + if x > 0 { x + 1 } else { x } as u16; + //to.blit(&value, x, y, None) + //} } fn row_data (&self, to: &mut TuiOut, entry: &Entry, row: usize, x: &mut u16) { for (column_index, Column { width, value, .. }) in self.0.columns.0.iter().enumerate() { to.area[0] = *x; if let Some((edit_index, value)) = self.0.editing.as_ref() - && *edit_index == column_index - && row == self.0.cursor + && self.0.column == column_index + && self.0.cursor == row { + to.fill_bg([*x, to.area().y(), *width as u16, 1], Self::BG_EDIT); + to.fill_fg([*x, to.area().y(), *width as u16, 1], Self::FG_EDIT); Content::render(&TrimStringRef(*width as u16, &value), to); } else if let Some(value) = value(entry) { Content::render(&TrimStringRef(*width as u16, &value), to); @@ -55,14 +74,6 @@ impl<'a> TreeTable<'a> { *x += *width as u16 + 1; } } - fn col_cursor (&self, to: &mut TuiOut, xa: u16, x0: u16, x: u16, y: u16, w: u16) { - let fill = [xa + x0, y, w, 1]; - to.fill_bg(fill, Color::Rgb(224, 192, 0)); - if let Some((_index, value)) = &self.0.editing { - let x = xa + if x > 0 { x + 1 } else { x } as u16; - to.blit(&value, x, y, None) - } - } } pub struct Column {