Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selecting an item in a cursive_table_view and rendering it appropriately #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -127,6 +130,8 @@ pub struct TableView<T, H> {
items: Vec<T>,
rows_to_items: Vec<usize>,

selected_rows: BTreeSet<usize>,

on_sort: Option<OnSortCallback<H>>,
// TODO Pass drawing offsets into the handlers so a popup menu
// can be created easily?
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down