From c17b3e82bad7efb54c9c271c8f37232d780c0857 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Tue, 1 Nov 2022 12:00:38 +0100 Subject: [PATCH 01/22] Selecting an item in a cursive_table_view and rendering it appropriately --- src/lib.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 20edb91..399ac6a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,10 +13,13 @@ extern crate cursive_core as cursive; // STD Dependencies ----------------------------------------------------------- -use std::cmp::{self, Ordering}; use std::collections::HashMap; use std::hash::Hash; use std::rc::Rc; +use std::{ + cmp::{self, Ordering}, + collections::BTreeSet, +}; // External Dependencies ------------------------------------------------------ use cursive::{ @@ -127,6 +130,8 @@ pub struct TableView { items: Vec, rows_to_items: Vec, + selected_rows: BTreeSet, + on_sort: Option>, // TODO Pass drawing offsets into the handlers so a popup menu // can be created easily? @@ -197,7 +202,7 @@ where focus: 0, items: Vec::new(), rows_to_items: Vec::new(), - + selected_rows: BTreeSet::new(), on_sort: None, on_submit: None, on_select: None, @@ -826,7 +831,17 @@ where fn draw_content(&self, printer: &Printer) { for i in 0..self.rows_to_items.len() { let printer = printer.offset((0, i)); - let color = if i == self.focus && self.enabled { + let color = if self.selected_rows.contains(&i) && i != self.focus && self.enabled { + theme::ColorStyle::new( + theme::ColorType::Palette(theme::PaletteColor::TitleSecondary), + theme::ColorType::Palette(theme::PaletteColor::View), + ) + } else if self.selected_rows.contains(&i) && i == self.focus && self.enabled { + theme::ColorStyle::new( + theme::ColorType::Palette(theme::PaletteColor::TitleSecondary), + theme::ColorType::Palette(theme::PaletteColor::Highlight), + ) + } else if i == self.focus && self.enabled { if !self.column_select && self.enabled && printer.focused { theme::ColorStyle::highlight() } else { @@ -885,6 +900,14 @@ where fn on_inner_event(&mut self, event: Event) -> EventResult { let last_focus = self.focus; match event { + Event::Key(Key::Ins) => { + if self.selected_rows.contains(&last_focus) { + self.selected_rows.remove(&last_focus); + } else { + self.selected_rows.insert(last_focus); + } + self.focus_down(1); + } Event::Key(Key::Right) => { if self.column_select { if !self.column_next() { From 914e74a5c79eead0d612ef4f8cea561e16ab7792 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Tue, 1 Nov 2022 12:49:11 +0100 Subject: [PATCH 02/22] PgUp/Down moves real page up --- src/lib.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 399ac6a..8fa92ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,10 +13,11 @@ extern crate cursive_core as cursive; // STD Dependencies ----------------------------------------------------------- -use std::collections::HashMap; use std::hash::Hash; use std::rc::Rc; +use std::{borrow::BorrowMut, collections::HashMap}; use std::{ + cell::Cell, cmp::{self, Ordering}, collections::BTreeSet, }; @@ -131,7 +132,7 @@ pub struct TableView { rows_to_items: Vec, selected_rows: BTreeSet, - + visible_rows: Cell, on_sort: Option>, // TODO Pass drawing offsets into the handlers so a popup menu // can be created easily? @@ -203,6 +204,7 @@ where items: Vec::new(), rows_to_items: Vec::new(), selected_rows: BTreeSet::new(), + visible_rows: Cell::new(0), on_sort: None, on_submit: None, on_select: None, @@ -942,11 +944,11 @@ where } Event::Key(Key::PageUp) => { self.column_cancel(); - self.focus_up(10); + self.focus_up(self.visible_rows.get()); } Event::Key(Key::PageDown) => { self.column_cancel(); - self.focus_down(10); + self.focus_down(self.visible_rows.get()); } Event::Key(Key::Home) => { self.column_cancel(); @@ -1052,6 +1054,7 @@ where let printer = &printer.offset((0, 2)).focused(true); scroll::draw(self, printer, Self::draw_content); + self.visible_rows.set(printer.size.y); } fn layout(&mut self, size: Vec2) { From a24d41d76b257bcbc12cca54a471b802d8e40f5e Mon Sep 17 00:00:00 2001 From: smallB007 Date: Fri, 11 Nov 2022 10:49:52 +0100 Subject: [PATCH 03/22] Added last focus time to mark active table --- src/lib.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8fa92ad..0a85ed5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,13 +15,13 @@ extern crate cursive_core as cursive; // STD Dependencies ----------------------------------------------------------- use std::hash::Hash; use std::rc::Rc; +use std::time::{Duration, Instant}; use std::{borrow::BorrowMut, collections::HashMap}; use std::{ cell::Cell, cmp::{self, Ordering}, collections::BTreeSet, }; - // External Dependencies ------------------------------------------------------ use cursive::{ align::HAlign, @@ -118,6 +118,7 @@ type IndexCallback = Rc; /// .default_column(BasicColumn::Name); /// # } /// ``` + pub struct TableView { enabled: bool, scroll_core: scroll::Core, @@ -132,6 +133,7 @@ pub struct TableView { rows_to_items: Vec, selected_rows: BTreeSet, + last_focus_time: Instant, visible_rows: Cell, on_sort: Option>, // TODO Pass drawing offsets into the handlers so a popup menu @@ -186,6 +188,13 @@ where T: TableViewItem, H: Eq + Hash + Copy + Clone + 'static, { + //// + /// + /// + /// + pub fn get_last_focus_time(&self) -> Instant { + self.last_focus_time + } /// Creates a new empty `TableView` without any columns. /// /// A TableView should be accompanied by a enum of type `H` representing @@ -204,6 +213,7 @@ where items: Vec::new(), rows_to_items: Vec::new(), selected_rows: BTreeSet::new(), + last_focus_time: Instant::now(), visible_rows: Cell::new(0), on_sort: None, on_submit: None, @@ -590,7 +600,15 @@ where pub fn item(&self) -> Option { self.rows_to_items.get(self.focus).copied() } - + /// + ///++artie + pub fn get_selected_item(&self) -> &T { + let inx = match self.item() { + Some(inx) => inx, + None => 0, + }; + self.borrow_item(inx).unwrap() + } /// Selects the item at the specified index within the underlying storage /// vector. pub fn set_selected_item(&mut self, item_index: usize) { @@ -900,8 +918,12 @@ where } fn on_inner_event(&mut self, event: Event) -> EventResult { - let last_focus = self.focus; + let last_focus = self.focus; //++artie, the focus here is bit confusing. What it really means is currently selected and in focus row match event { + Event::Key(Key::Tab) => { + self.last_focus_time = Instant::now(); + return EventResult::Ignored; + } Event::Key(Key::Ins) => { if self.selected_rows.contains(&last_focus) { self.selected_rows.remove(&last_focus); From 69701d8a4d282833d773832423ef2623d8a95ef3 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Fri, 11 Nov 2022 12:44:16 +0100 Subject: [PATCH 04/22] Table doesn't use unnecessary member --- src/lib.rs | 62 ++++++++++++------------------------------------------ 1 file changed, 14 insertions(+), 48 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0a85ed5..736a3f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -130,8 +130,6 @@ pub struct TableView { focus: usize, items: Vec, - rows_to_items: Vec, - selected_rows: BTreeSet, last_focus_time: Instant, visible_rows: Cell, @@ -211,7 +209,6 @@ where focus: 0, items: Vec::new(), - rows_to_items: Vec::new(), selected_rows: BTreeSet::new(), last_focus_time: Instant::now(), visible_rows: Cell::new(0), @@ -493,7 +490,6 @@ where /// Removes all items from this view. pub fn clear(&mut self) { self.items.clear(); - self.rows_to_items.clear(); self.focus = 0; self.needs_relayout = true; } @@ -540,11 +536,6 @@ where fn set_items_and_focus(&mut self, items: Vec, new_location: usize) { self.items = items; - self.rows_to_items = Vec::with_capacity(self.items.len()); - - for i in 0..self.items.len() { - self.rows_to_items.push(i); - } if let Some((column, order)) = self.order() { // Preserve the selected column if possible. @@ -598,10 +589,9 @@ where /// Returns the index of the currently selected item within the underlying /// storage vector. pub fn item(&self) -> Option { - self.rows_to_items.get(self.focus).copied() + Some(self.focus) } /// - ///++artie pub fn get_selected_item(&self) -> &T { let inx = match self.item() { Some(inx) => inx, @@ -612,16 +602,8 @@ where /// Selects the item at the specified index within the underlying storage /// vector. pub fn set_selected_item(&mut self, item_index: usize) { - // TODO optimize the performance for very large item lists - if item_index < self.items.len() { - for (row, item) in self.rows_to_items.iter().enumerate() { - if *item == item_index { - self.focus = row; - self.scroll_core.scroll_to_y(row); - break; - } - } - } + self.focus = item_index; + self.scroll_core.scroll_to_y(self.focus); } /// Selects the item at the specified index within the underlying storage @@ -652,11 +634,9 @@ where /// # Panics /// /// If `index > self.len()`. + #[deprecated(note = "Don't use it. It is slow with current underlying storage container")] pub fn insert_item_at(&mut self, index: usize, item: T) { - self.items.push(item); - - // Here we know self.items.len() > 0 - self.rows_to_items.insert(index, self.items.len() - 1); + self.items.insert(index, item); if let Some((column, order)) = self.order() { self.sort_by(column, order); @@ -669,21 +649,10 @@ where pub fn remove_item(&mut self, item_index: usize) -> Option { if item_index < self.items.len() { // Move the selection if the currently selected item gets removed - if let Some(selected_index) = self.item() { - if selected_index == item_index { - self.focus_up(1); - } + if self.focus == item_index { + self.focus_up(1); } - // Remove the sorted reference to the item - self.rows_to_items.retain(|i| *i != item_index); - - // Adjust remaining references - for ref_index in &mut self.rows_to_items { - if *ref_index > item_index { - *ref_index -= 1; - } - } self.needs_relayout = true; // Remove actual item from the underlying storage @@ -696,7 +665,6 @@ where /// Removes all items from the underlying storage and returns them. pub fn take_items(&mut self) -> Vec { self.set_selected_row(0); - self.rows_to_items.clear(); self.needs_relayout = true; self.items.drain(0..).collect() } @@ -732,15 +700,13 @@ where if !self.is_empty() { let old_item = self.item(); - let mut rows_to_items = self.rows_to_items.clone(); - rows_to_items.sort_by(|a, b| { + self.items.sort_by(|a, b| { if order == Ordering::Less { - self.items[*a].cmp(&self.items[*b], column) + a.cmp(b, column) } else { - self.items[*b].cmp(&self.items[*a], column) + b.cmp(a, column) } }); - self.rows_to_items = rows_to_items; if let Some(old_item) = old_item { self.set_selected_item(old_item); @@ -750,7 +716,7 @@ where fn draw_item(&self, printer: &Printer, i: usize) { self.draw_columns(printer, "┆ ", |printer, column| { - let value = self.items[self.rows_to_items[i]].to_column(column.column); + let value = self.items[i].to_column(column.column); column.draw_row(printer, value.as_str()); }); } @@ -849,7 +815,7 @@ where } fn draw_content(&self, printer: &Printer) { - for i in 0..self.rows_to_items.len() { + for i in 0..self.items.len() { let printer = printer.offset((0, i)); let color = if self.selected_rows.contains(&i) && i != self.focus && self.enabled { theme::ColorStyle::new( @@ -914,7 +880,7 @@ where } fn content_required_size(&mut self, req: Vec2) -> Vec2 { - Vec2::new(req.x, self.rows_to_items.len()) + Vec2::new(req.x, self.items.len()) } fn on_inner_event(&mut self, event: Event) -> EventResult { @@ -1004,7 +970,7 @@ where offset, event: MouseEvent::Press(_), } if !self.is_empty() => match position.checked_sub(offset) { - Some(position) if position.y < self.rows_to_items.len() => { + Some(position) if position.y < self.items.len() => { self.column_cancel(); self.focus = position.y; } From ccdc2be87c3c8b7082b14e743d801949e4d04c07 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Fri, 25 Nov 2022 09:59:11 +0100 Subject: [PATCH 05/22] Enabling/Disabling header for TableViewColumn --- src/lib.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 736a3f7..ad8f4d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,9 +35,9 @@ use cursive::{ /// A trait for displaying and sorting items inside a /// [`TableView`](struct.TableView.html). -pub trait TableViewItem: Clone + Sized +pub trait TableViewItem: Sized where - H: Eq + Hash + Copy + Clone + 'static, + H: Eq + Hash + Copy + 'static, { /// Method returning a string representation of the item for the /// specified column from type `H`. @@ -82,13 +82,13 @@ type IndexCallback = Rc; /// /// // Define the item type /// #[derive(Clone, Debug)] -/// struct Foo { +/// struct DirView { /// name: String, /// count: usize, /// rate: usize /// } /// -/// impl TableViewItem for Foo { +/// impl TableViewItem for DirView { /// /// fn to_column(&self, column: BasicColumn) -> String { /// match column { @@ -109,7 +109,7 @@ type IndexCallback = Rc; /// } /// /// // Configure the actual table -/// let table = TableView::::new() +/// let table = TableView::::new() /// .column(BasicColumn::Name, "Name", |c| c.width(20)) /// .column(BasicColumn::Count, "Count", |c| c.align(HAlign::Center)) /// .column(BasicColumn::Rate, "Rate", |c| { @@ -228,8 +228,9 @@ where column: H, title: S, callback: C, + has_header: bool, ) -> Self { - self.add_column(column, title, callback); + self.add_column(column, title, callback, has_header); self } @@ -243,8 +244,9 @@ where column: H, title: S, callback: C, + has_header: bool, ) { - self.insert_column(self.columns.len(), column, title, callback); + self.insert_column(self.columns.len(), column, title, callback, has_header); } /// Remove a column. @@ -270,6 +272,7 @@ where column: H, title: S, callback: C, + has_header: bool, ) { // Update all existing indices for column in &self.columns[i..] { @@ -277,8 +280,10 @@ where } self.column_indicies.insert(column, i); - self.columns - .insert(i, callback(TableColumn::new(column, title.into()))); + self.columns.insert( + i, + callback(TableColumn::new(column, title.into(), has_header)), + ); // Make the first colum the default one if self.columns.len() == 1 { @@ -1109,6 +1114,7 @@ pub struct TableColumn { width: usize, default_order: Ordering, requested_width: Option, + has_header: bool, } enum TableColumnWidth { @@ -1142,7 +1148,7 @@ impl TableColumn { self } - fn new(column: H, title: String) -> Self { + fn new(column: H, title: String, has_header: bool) -> Self { Self { column, title, @@ -1152,10 +1158,14 @@ impl TableColumn { width: 0, default_order: Ordering::Less, requested_width: None, + has_header, } } fn draw_header(&self, printer: &Printer) { + if !self.has_header { + return; + } let order = match self.order { Ordering::Less => "^", Ordering::Greater => "v", From 86feb435de7da63e03425e5a9540bf09274449da Mon Sep 17 00:00:00 2001 From: smallB007 Date: Sat, 26 Nov 2022 16:04:12 +0100 Subject: [PATCH 06/22] Don't highlight column header if not focused --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ad8f4d0..5f93baa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1021,7 +1021,8 @@ where if self.column_select && column.selected && self.enabled && printer.focused { theme::ColorStyle::highlight() } else { - theme::ColorStyle::highlight_inactive() + //theme::ColorStyle::highlight_inactive() + theme::ColorStyle::primary() //++artie, if column is not selected don't draw background } } else { theme::ColorStyle::primary() From bf0c8a53fddf010d98197ba8ebaae7a10b5e2acd Mon Sep 17 00:00:00 2001 From: smallB007 Date: Sat, 26 Nov 2022 16:18:06 +0100 Subject: [PATCH 07/22] Don't highlight row if table is not focused --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 5f93baa..9b71b6d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -836,7 +836,8 @@ where if !self.column_select && self.enabled && printer.focused { theme::ColorStyle::highlight() } else { - theme::ColorStyle::highlight_inactive() + //theme::ColorStyle::highlight_inactive() + theme::ColorStyle::primary() //++artie, no highlight if !printer.focused } } else { theme::ColorStyle::primary() From 1ddae846a94d988e38403b279323c0c96adbfa86 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Sun, 27 Nov 2022 09:51:58 +0100 Subject: [PATCH 08/22] On Tab loose focus from selected column --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 9b71b6d..6709cd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -894,6 +894,7 @@ where match event { Event::Key(Key::Tab) => { self.last_focus_time = Instant::now(); + self.column_cancel(); return EventResult::Ignored; } Event::Key(Key::Ins) => { From 95c4e0d94d26600ceb28400683b719f6aad402d3 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Mon, 28 Nov 2022 12:02:00 +0100 Subject: [PATCH 09/22] PeekView --- src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 6709cd7..ae605bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -138,6 +138,7 @@ pub struct TableView { // can be created easily? on_submit: Option, on_select: Option, + on_peek: Option, } cursive::impl_scroller!(TableView < T, H > ::scroll_core); @@ -215,6 +216,7 @@ where on_sort: None, on_submit: None, on_select: None, + on_peek: None, } } @@ -491,6 +493,24 @@ where { self.with(|t| t.set_on_select(cb)) } + ////////////////////// + /// Sets a callback to be used when an item is selected in peek mode + + pub fn set_on_peek(&mut self, cb: F) + where + F: Fn(&mut Cursive, usize, usize) + 'static, + { + self.on_peek = Some(Rc::new(move |s, row, index| cb(s, row, index))); + } + + /// Sets a callback to be used when an item is selected in peek mode + + pub fn on_peek(self, cb: F) -> Self + where + F: Fn(&mut Cursive, usize, usize) + 'static, + { + self.with(|t| t.set_on_peek(cb)) + } /// Removes all items from this view. pub fn clear(&mut self) { @@ -928,6 +948,9 @@ where self.column_cancel(); } else { self.focus_up(1); + if self.on_peek.is_some() { + return self.on_peek_event(); //++artie, must return otherwise the result is Ignored + } } } Event::Key(Key::Down) if self.focus + 1 < self.items.len() || self.column_select => { @@ -935,6 +958,9 @@ where self.column_cancel(); } else { self.focus_down(1); + if self.on_peek.is_some() { + return self.on_peek_event(); //++artie, must return otherwise the result is Ignored + } } } Event::Key(Key::PageUp) => { @@ -1010,6 +1036,16 @@ where } EventResult::Ignored } + + fn on_peek_event(&mut self) -> EventResult { + if let Some(ref cb) = &self.on_peek { + let cb = Rc::clone(cb); + let row = self.row().unwrap(); + let index = self.item().unwrap(); + return EventResult::Consumed(Some(Callback::from_fn(move |s| cb(s, row, index)))); + } + EventResult::Ignored + } } impl View for TableView From 442833a915c06f253653e515aceb6b1e21112012 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Tue, 29 Nov 2022 14:30:33 +0100 Subject: [PATCH 10/22] Added get_selected_items --- src/lib.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ae605bc..96a02c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -620,10 +620,19 @@ where pub fn get_selected_item(&self) -> &T { let inx = match self.item() { Some(inx) => inx, - None => 0, + None => 0, //++artie, zero or None? }; self.borrow_item(inx).unwrap() } + /// + pub fn get_selected_items(&self) -> Vec<&T> { + let mut res = Vec::new(); + for inx in &self.selected_rows { + res.push(self.borrow_item(*inx).unwrap()); + } + res + } + /// Selects the item at the specified index within the underlying storage /// vector. pub fn set_selected_item(&mut self, item_index: usize) { From 1cddb8f4d34df5413f653c72a659126597561200 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Fri, 9 Dec 2022 12:11:45 +0100 Subject: [PATCH 11/22] Getting selected items and inx --- src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 96a02c5..9d00afa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -632,6 +632,14 @@ where } res } + /// + pub fn get_selected_items_with_indexes(&self) -> Vec<(&usize, &T)> { + let mut res = Vec::new(); + for inx in &self.selected_rows { + res.push((inx, self.borrow_item(*inx).unwrap())); + } + res + } /// Selects the item at the specified index within the underlying storage /// vector. From 66c282ce6043bd257f8d175ce5fbc33ee3609c9e Mon Sep 17 00:00:00 2001 From: smallB007 Date: Sat, 10 Dec 2022 12:08:55 +0100 Subject: [PATCH 12/22] Deselecting item --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 9d00afa..d830448 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -640,6 +640,10 @@ where } res } + /// + pub fn deselect_item(&mut self, inx: usize) { + self.selected_rows.remove(&inx); + } /// Selects the item at the specified index within the underlying storage /// vector. From 26e95c219e1af0bf4c584389b80dc81552cadf1a Mon Sep 17 00:00:00 2001 From: smallB007 Date: Thu, 15 Dec 2022 12:03:19 +0100 Subject: [PATCH 13/22] Clearing selection on items set --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index d830448..23817fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -556,6 +556,7 @@ where /// The currently active sort order is preserved and will be applied to all /// items. pub fn set_items(&mut self, items: Vec) { + self.selected_rows.clear(); self.set_items_and_focus(items, 0); } @@ -624,7 +625,7 @@ where }; self.borrow_item(inx).unwrap() } - /// + ///++artie, just return the container pub fn get_selected_items(&self) -> Vec<&T> { let mut res = Vec::new(); for inx in &self.selected_rows { From 7736822fbd83d3da7fc5e955595afeef8fbb786f Mon Sep 17 00:00:00 2001 From: smallB007 Date: Sat, 17 Dec 2022 09:59:46 +0100 Subject: [PATCH 14/22] Clearing selection on items set --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 23817fb..01399a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -645,6 +645,10 @@ where pub fn deselect_item(&mut self, inx: usize) { self.selected_rows.remove(&inx); } + /// + pub fn deselect_all_items(&mut self) { + self.selected_rows.clear(); + } /// Selects the item at the specified index within the underlying storage /// vector. From fec059571165e52d61a1d19a5bbaea92936584be Mon Sep 17 00:00:00 2001 From: smallB007 Date: Sat, 31 Dec 2022 12:38:56 +0100 Subject: [PATCH 15/22] Non selected means get one that is focused --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 01399a0..9302407 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -621,7 +621,7 @@ where pub fn get_selected_item(&self) -> &T { let inx = match self.item() { Some(inx) => inx, - None => 0, //++artie, zero or None? + None => self.focus, }; self.borrow_item(inx).unwrap() } From c408e7e296d6b04cd340f7c5526f11347c66a5a1 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Sat, 31 Dec 2022 12:46:54 +0100 Subject: [PATCH 16/22] Non selected means get one that is focused --- src/lib.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9302407..67432c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -617,13 +617,9 @@ where pub fn item(&self) -> Option { Some(self.focus) } - /// - pub fn get_selected_item(&self) -> &T { - let inx = match self.item() { - Some(inx) => inx, - None => self.focus, - }; - self.borrow_item(inx).unwrap() + ///++artie Returns the item, as opposed to just the inx + pub fn get_focused_item(&self) -> &T { + self.borrow_item(self.item().unwrap()).unwrap() } ///++artie, just return the container pub fn get_selected_items(&self) -> Vec<&T> { From 6b59e28d0b50ce5456c1a5d28a3ef7f1ba465712 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Sat, 4 Feb 2023 19:04:57 +0100 Subject: [PATCH 17/22] Changed BasicColumn to ClassicColumn --- src/lib.rs | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 67432c0..0349dd0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,7 +74,7 @@ type IndexCallback = Rc; /// # fn main() { /// // Provide a type for the table's columns /// #[derive(Copy, Clone, PartialEq, Eq, Hash)] -/// enum BasicColumn { +/// enum ClassicColumns { /// Name, /// Count, /// Rate @@ -88,34 +88,34 @@ type IndexCallback = Rc; /// rate: usize /// } /// -/// impl TableViewItem for DirView { +/// impl TableViewItem for DirView { /// -/// fn to_column(&self, column: BasicColumn) -> String { +/// fn to_column(&self, column: ClassicColumns) -> String { /// match column { -/// BasicColumn::Name => self.name.to_string(), -/// BasicColumn::Count => format!("{}", self.count), -/// BasicColumn::Rate => format!("{}", self.rate) +/// ClassicColumns::Name => self.name.to_string(), +/// ClassicColumns::Count => format!("{}", self.count), +/// ClassicColumns::Rate => format!("{}", self.rate) /// } /// } /// -/// fn cmp(&self, other: &Self, column: BasicColumn) -> Ordering where Self: Sized { +/// fn cmp(&self, other: &Self, column: ClassicColumns) -> Ordering where Self: Sized { /// match column { -/// BasicColumn::Name => self.name.cmp(&other.name), -/// BasicColumn::Count => self.count.cmp(&other.count), -/// BasicColumn::Rate => self.rate.cmp(&other.rate) +/// ClassicColumns::Name => self.name.cmp(&other.name), +/// ClassicColumns::Count => self.count.cmp(&other.count), +/// ClassicColumns::Rate => self.rate.cmp(&other.rate) /// } /// } /// /// } /// /// // Configure the actual table -/// let table = TableView::::new() -/// .column(BasicColumn::Name, "Name", |c| c.width(20)) -/// .column(BasicColumn::Count, "Count", |c| c.align(HAlign::Center)) -/// .column(BasicColumn::Rate, "Rate", |c| { +/// let table = TableView::::new() +/// .column(ClassicColumns::Name, "Name", |c| c.width(20)) +/// .column(ClassicColumns::Count, "Count", |c| c.align(HAlign::Center)) +/// .column(ClassicColumns::Rate, "Rate", |c| { /// c.ordering(Ordering::Greater).align(HAlign::Right).width(20) /// }) -/// .default_column(BasicColumn::Name); +/// .default_column(ClassicColumns::Name); /// # } /// ``` @@ -382,7 +382,7 @@ where /// # Example /// /// ```ignore - /// table.set_on_sort(|siv: &mut Cursive, column: BasicColumn, order: Ordering| { + /// table.set_on_sort(|siv: &mut Cursive, column: ClassicColumns, order: Ordering| { /// /// }); /// ``` @@ -401,7 +401,7 @@ where /// # Example /// /// ```ignore - /// table.on_sort(|siv: &mut Cursive, column: BasicColumn, order: Ordering| { + /// table.on_sort(|siv: &mut Cursive, column: ClassicColumns, order: Ordering| { /// /// }); /// ``` @@ -612,6 +612,14 @@ where &mut self.items } + /// ++artie + /// + /// Can be used to modify the items in place. + pub fn get_items_mut(&mut self) -> &mut Vec { + self.needs_relayout = true; + &mut self.items + } + /// Returns the index of the currently selected item within the underlying /// storage vector. pub fn item(&self) -> Option { @@ -790,6 +798,12 @@ where self.columns.iter().position(|c| c.selected).unwrap_or(0) } + ///++artie + pub fn get_active_column(&mut self) -> &mut TableColumn { + let active_col_inx = self.active_column(); + &mut self.columns[active_col_inx] + } + fn column_cancel(&mut self) { self.column_select = false; for column in &mut self.columns { @@ -1167,8 +1181,10 @@ where /// A type used for the construction of columns in a /// [`TableView`](struct.TableView.html). pub struct TableColumn { - column: H, - title: String, + ///++artie + pub column: H, + ///++artie + pub title: String, selected: bool, alignment: HAlign, order: Ordering, From 90cf9679b4dc7cd59d8ce21d8d09cf7a9196c664 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Sun, 26 Feb 2023 13:12:46 +0100 Subject: [PATCH 18/22] Added get_items --- src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 0349dd0..c456c4e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -619,6 +619,11 @@ where self.needs_relayout = true; &mut self.items } + /// ++artie + pub fn get_items(&mut self) -> &Vec { + self.needs_relayout = true; + &self.items + } /// Returns the index of the currently selected item within the underlying /// storage vector. From abbfc80e9bbe748732d6598996ca61916e99e307 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Mon, 27 Feb 2023 19:08:01 +0100 Subject: [PATCH 19/22] Passing item inx to to_column --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c456c4e..15cd06e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,7 +41,7 @@ where { /// Method returning a string representation of the item for the /// specified column from type `H`. - fn to_column(&self, column: H) -> String; + fn to_column(&self, column: H, item_inx: usize) -> String; /// Method comparing two items via their specified column from type `H`. fn cmp(&self, other: &Self, column: H) -> Ordering @@ -776,7 +776,7 @@ where fn draw_item(&self, printer: &Printer, i: usize) { self.draw_columns(printer, "┆ ", |printer, column| { - let value = self.items[i].to_column(column.column); + let value = self.items[i].to_column(column.column, i); column.draw_row(printer, value.as_str()); }); } From bbff3cfdae13b3b06ecb807748fcec60664cb7a1 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Sun, 9 Apr 2023 14:03:53 +0200 Subject: [PATCH 20/22] Rfx --- src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 15cd06e..72ce312 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,13 +82,13 @@ type IndexCallback = Rc; /// /// // Define the item type /// #[derive(Clone, Debug)] -/// struct DirView { +/// struct DirViewRow { /// name: String, /// count: usize, /// rate: usize /// } /// -/// impl TableViewItem for DirView { +/// impl TableViewItem for DirViewRow { /// /// fn to_column(&self, column: ClassicColumns) -> String { /// match column { @@ -109,7 +109,7 @@ type IndexCallback = Rc; /// } /// /// // Configure the actual table -/// let table = TableView::::new() +/// let table = TableView::::new() /// .column(ClassicColumns::Name, "Name", |c| c.width(20)) /// .column(ClassicColumns::Count, "Count", |c| c.align(HAlign::Center)) /// .column(ClassicColumns::Rate, "Rate", |c| { @@ -632,7 +632,8 @@ where } ///++artie Returns the item, as opposed to just the inx pub fn get_focused_item(&self) -> &T { - self.borrow_item(self.item().unwrap()).unwrap() + let inx = self.item().unwrap(); + self.borrow_item(inx).unwrap() } ///++artie, just return the container pub fn get_selected_items(&self) -> Vec<&T> { From fc7b72bced3688d7630ee01fe92a19ac48caa3a8 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Mon, 10 Apr 2023 10:24:18 +0200 Subject: [PATCH 21/22] Finding active table name --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 72ce312..1181d8d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -131,7 +131,8 @@ pub struct TableView { focus: usize, items: Vec, selected_rows: BTreeSet, - last_focus_time: Instant, + //#[cfg(deprecated)] + last_focus_time: Instant, //++artie visible_rows: Cell, on_sort: Option>, // TODO Pass drawing offsets into the handlers so a popup menu From e859fe459c3dd491f554599d5c4d46eecc58cc70 Mon Sep 17 00:00:00 2001 From: smallB007 Date: Sun, 18 Jun 2023 11:14:00 +0200 Subject: [PATCH 22/22] Removed rand --- examples/basic.rs | 1 - examples/double.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index 0c2c802..a84a02d 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -14,7 +14,6 @@ use cursive::align::HAlign; use cursive::traits::*; use cursive::views::{Dialog, TextView}; use cursive::Cursive; -use rand::Rng; // Modules -------------------------------------------------------------------- // ---------------------------------------------------------------------------- diff --git a/examples/double.rs b/examples/double.rs index 692890a..3c92e5f 100644 --- a/examples/double.rs +++ b/examples/double.rs @@ -14,7 +14,6 @@ use cursive::align::HAlign; use cursive::direction::Orientation; use cursive::traits::*; use cursive::views::{Dialog, DummyView, LinearLayout, ResizedView}; -use rand::Rng; // Modules -------------------------------------------------------------------- // ----------------------------------------------------------------------------