|
|
|
@ -15,30 +15,26 @@ use tui::{
|
|
|
|
|
use unicode_width::UnicodeWidthStr;
|
|
|
|
|
|
|
|
|
|
pub struct TableComponent {
|
|
|
|
|
pub state: TableState,
|
|
|
|
|
pub headers: Vec<String>,
|
|
|
|
|
pub rows: Vec<Vec<String>>,
|
|
|
|
|
pub eod: bool,
|
|
|
|
|
select_state: TableState,
|
|
|
|
|
selected_left_column_index: usize,
|
|
|
|
|
selected_right_cell: Option<(usize, usize)>,
|
|
|
|
|
pub selected_row: TableState,
|
|
|
|
|
selected_column: usize,
|
|
|
|
|
selection_area_corner: Option<(usize, usize)>,
|
|
|
|
|
column_page_start: std::cell::Cell<usize>,
|
|
|
|
|
scroll: VerticalScroll,
|
|
|
|
|
select_entire_row: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for TableComponent {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
state: TableState::default(),
|
|
|
|
|
select_state: TableState::default(),
|
|
|
|
|
selected_row: TableState::default(),
|
|
|
|
|
headers: vec![],
|
|
|
|
|
rows: vec![],
|
|
|
|
|
selected_left_column_index: 0,
|
|
|
|
|
selected_right_cell: None,
|
|
|
|
|
selected_column: 0,
|
|
|
|
|
selection_area_corner: None,
|
|
|
|
|
column_page_start: std::cell::Cell::new(0),
|
|
|
|
|
scroll: VerticalScroll::new(),
|
|
|
|
|
select_entire_row: false,
|
|
|
|
|
eod: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -46,31 +42,29 @@ impl Default for TableComponent {
|
|
|
|
|
|
|
|
|
|
impl TableComponent {
|
|
|
|
|
pub fn new(rows: Vec<Vec<String>>, headers: Vec<String>) -> Self {
|
|
|
|
|
let mut state = TableState::default();
|
|
|
|
|
let mut selected_row = TableState::default();
|
|
|
|
|
if !rows.is_empty() {
|
|
|
|
|
state.select(None);
|
|
|
|
|
state.select(Some(0))
|
|
|
|
|
selected_row.select(None);
|
|
|
|
|
selected_row.select(Some(0))
|
|
|
|
|
}
|
|
|
|
|
Self {
|
|
|
|
|
state,
|
|
|
|
|
headers,
|
|
|
|
|
rows,
|
|
|
|
|
selected_row,
|
|
|
|
|
..Self::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn reset(&mut self) {
|
|
|
|
|
self.select_state.select(None);
|
|
|
|
|
self.selected_right_cell = None;
|
|
|
|
|
self.select_entire_row = false;
|
|
|
|
|
fn reset(&mut self) {
|
|
|
|
|
self.selection_area_corner = None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn end(&mut self) {
|
|
|
|
|
self.eod = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn next(&mut self, lines: usize) {
|
|
|
|
|
let i = match self.state.selected() {
|
|
|
|
|
fn next_row(&mut self, lines: usize) {
|
|
|
|
|
let i = match self.selected_row.selected() {
|
|
|
|
|
Some(i) => {
|
|
|
|
|
if i + lines >= self.rows.len() {
|
|
|
|
|
Some(self.rows.len() - 1)
|
|
|
|
@ -81,25 +75,11 @@ impl TableComponent {
|
|
|
|
|
None => None,
|
|
|
|
|
};
|
|
|
|
|
self.reset();
|
|
|
|
|
self.state.select(i);
|
|
|
|
|
self.selected_row.select(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn next_select(&mut self, lines: usize) {
|
|
|
|
|
let i = match self.select_state.selected() {
|
|
|
|
|
Some(i) => {
|
|
|
|
|
if i + lines >= self.rows.len() {
|
|
|
|
|
Some(self.rows.len() - 1)
|
|
|
|
|
} else {
|
|
|
|
|
Some(i + lines)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => None,
|
|
|
|
|
};
|
|
|
|
|
self.select_state.select(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn previous(&mut self, lines: usize) {
|
|
|
|
|
let i = match self.state.selected() {
|
|
|
|
|
fn previous_row(&mut self, lines: usize) {
|
|
|
|
|
let i = match self.selected_row.selected() {
|
|
|
|
|
Some(i) => {
|
|
|
|
|
if i <= lines {
|
|
|
|
|
Some(0)
|
|
|
|
@ -110,70 +90,56 @@ impl TableComponent {
|
|
|
|
|
None => None,
|
|
|
|
|
};
|
|
|
|
|
self.reset();
|
|
|
|
|
self.state.select(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn previout_select(&mut self, lines: usize) {
|
|
|
|
|
let i = match self.select_state.selected() {
|
|
|
|
|
Some(i) => {
|
|
|
|
|
if i <= lines {
|
|
|
|
|
Some(0)
|
|
|
|
|
} else {
|
|
|
|
|
Some(i - lines)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => None,
|
|
|
|
|
};
|
|
|
|
|
self.select_state.select(i);
|
|
|
|
|
self.selected_row.select(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn scroll_top(&mut self) {
|
|
|
|
|
fn scroll_top(&mut self) {
|
|
|
|
|
if self.rows.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self.reset();
|
|
|
|
|
self.state.select(Some(0));
|
|
|
|
|
self.selected_row.select(Some(0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn scroll_bottom(&mut self) {
|
|
|
|
|
fn scroll_bottom(&mut self) {
|
|
|
|
|
if self.rows.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self.reset();
|
|
|
|
|
self.state.select(Some(self.rows.len() - 1));
|
|
|
|
|
self.selected_row.select(Some(self.rows.len() - 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn next_column(&mut self) {
|
|
|
|
|
fn next_column(&mut self) {
|
|
|
|
|
if self.rows.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if self.selected_left_column_index >= self.headers.len().saturating_sub(1) {
|
|
|
|
|
if self.selected_column >= self.headers.len().saturating_sub(1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self.reset();
|
|
|
|
|
self.selected_left_column_index += 1;
|
|
|
|
|
self.selected_column += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn previous_column(&mut self) {
|
|
|
|
|
fn previous_column(&mut self) {
|
|
|
|
|
if self.rows.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if self.selected_left_column_index == 0 {
|
|
|
|
|
if self.selected_column == 0 {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self.reset();
|
|
|
|
|
self.selected_left_column_index -= 1;
|
|
|
|
|
self.selected_column -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn expand_selected_area_x(&mut self, positive: bool) {
|
|
|
|
|
if self.selected_right_cell.is_none() {
|
|
|
|
|
self.selected_right_cell = Some((
|
|
|
|
|
self.selected_left_column_index,
|
|
|
|
|
self.state.selected().unwrap_or(0),
|
|
|
|
|
fn expand_selected_area_x(&mut self, positive: bool) {
|
|
|
|
|
if self.selection_area_corner.is_none() {
|
|
|
|
|
self.selection_area_corner = Some((
|
|
|
|
|
self.selected_column,
|
|
|
|
|
self.selected_row.selected().unwrap_or(0),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
if let Some((x, y)) = self.selected_right_cell {
|
|
|
|
|
self.selected_right_cell = Some((
|
|
|
|
|
if let Some((x, y)) = self.selection_area_corner {
|
|
|
|
|
self.selection_area_corner = Some((
|
|
|
|
|
if positive {
|
|
|
|
|
(x + 1).min(self.headers.len().saturating_sub(1))
|
|
|
|
|
} else {
|
|
|
|
@ -184,25 +150,15 @@ impl TableComponent {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn expand_selected_area_y(&mut self, positive: bool) {
|
|
|
|
|
if self.select_state.selected().is_none() {
|
|
|
|
|
self.select_state = self.state.clone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if positive {
|
|
|
|
|
self.next_select(1);
|
|
|
|
|
} else {
|
|
|
|
|
self.previout_select(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.selected_right_cell.is_none() {
|
|
|
|
|
self.selected_right_cell = Some((
|
|
|
|
|
self.selected_left_column_index,
|
|
|
|
|
self.state.selected().unwrap_or(0),
|
|
|
|
|
fn expand_selected_area_y(&mut self, positive: bool) {
|
|
|
|
|
if self.selection_area_corner.is_none() {
|
|
|
|
|
self.selection_area_corner = Some((
|
|
|
|
|
self.selected_column,
|
|
|
|
|
self.selected_row.selected().unwrap_or(0),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
if let Some((x, y)) = self.selected_right_cell {
|
|
|
|
|
self.selected_right_cell = Some((
|
|
|
|
|
if let Some((x, y)) = self.selection_area_corner {
|
|
|
|
|
self.selection_area_corner = Some((
|
|
|
|
|
x,
|
|
|
|
|
if positive {
|
|
|
|
|
(y + 1).min(self.rows.len().saturating_sub(1))
|
|
|
|
@ -213,50 +169,44 @@ impl TableComponent {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_row_number_clumn(&self, row_index: usize, column_index: usize) -> bool {
|
|
|
|
|
matches!(self.state.selected(), Some(selected_row_index) if row_index == selected_row_index && 0 == column_index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn selected_cells(&self) -> Option<String> {
|
|
|
|
|
if let Some((x, y)) = self.selected_right_cell {
|
|
|
|
|
let selected_row_index = self.state.selected()?;
|
|
|
|
|
if let Some((x, y)) = self.selection_area_corner {
|
|
|
|
|
let selected_row_index = self.selected_row.selected()?;
|
|
|
|
|
return Some(
|
|
|
|
|
self.rows[y.min(selected_row_index)..y.max(selected_row_index) + 1]
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|row| {
|
|
|
|
|
row[x.min(self.selected_left_column_index)
|
|
|
|
|
..x.max(self.selected_left_column_index) + 1]
|
|
|
|
|
.join(",")
|
|
|
|
|
row[x.min(self.selected_column)..x.max(self.selected_column) + 1].join(",")
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
|
.join("\n"),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
self.rows
|
|
|
|
|
.get(self.state.selected()?)?
|
|
|
|
|
.get(self.selected_left_column_index)
|
|
|
|
|
.get(self.selected_row.selected()?)?
|
|
|
|
|
.get(self.selected_column)
|
|
|
|
|
.map(|cell| cell.to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn selected_column_index(&self) -> usize {
|
|
|
|
|
if let Some((x, _)) = self.selected_right_cell {
|
|
|
|
|
fn selected_column_index(&self) -> usize {
|
|
|
|
|
if let Some((x, _)) = self.selection_area_corner {
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
self.selected_left_column_index
|
|
|
|
|
self.selected_column
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_selected_cell(
|
|
|
|
|
fn is_selected_cell(
|
|
|
|
|
&self,
|
|
|
|
|
row_index: usize,
|
|
|
|
|
column_index: usize,
|
|
|
|
|
selected_column_index: usize,
|
|
|
|
|
) -> bool {
|
|
|
|
|
if let Some((x, y)) = self.selected_right_cell {
|
|
|
|
|
if let Some((x, y)) = self.selection_area_corner {
|
|
|
|
|
let x_in_page = x
|
|
|
|
|
.saturating_add(1)
|
|
|
|
|
.saturating_sub(self.column_page_start.get());
|
|
|
|
|
return matches!(
|
|
|
|
|
self.state.selected(),
|
|
|
|
|
self.selected_row.selected(),
|
|
|
|
|
Some(selected_row_index)
|
|
|
|
|
if (x_in_page.min(selected_column_index).max(1)..x_in_page.max(selected_column_index) + 1)
|
|
|
|
|
.contains(&column_index)
|
|
|
|
@ -264,16 +214,26 @@ impl TableComponent {
|
|
|
|
|
.contains(&row_index)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
matches!(self.state.selected(), Some(selected_row_index) if row_index == selected_row_index && column_index == selected_column_index)
|
|
|
|
|
matches!(
|
|
|
|
|
self.selected_row.selected(),
|
|
|
|
|
Some(selected_row_index) if row_index == selected_row_index && column_index == selected_column_index
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn headers_with_number(&self, left: usize, right: usize) -> Vec<String> {
|
|
|
|
|
fn is_number_column(&self, row_index: usize, column_index: usize) -> bool {
|
|
|
|
|
matches!(
|
|
|
|
|
self.selected_row.selected(),
|
|
|
|
|
Some(selected_row_index) if row_index == selected_row_index && 0 == column_index
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn headers(&self, left: usize, right: usize) -> Vec<String> {
|
|
|
|
|
let mut headers = self.headers.clone()[left..right].to_vec();
|
|
|
|
|
headers.insert(0, "".to_string());
|
|
|
|
|
headers
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn rows_with_number(&self, left: usize, right: usize) -> Vec<Vec<String>> {
|
|
|
|
|
fn rows(&self, left: usize, right: usize) -> Vec<Vec<String>> {
|
|
|
|
|
let rows = self
|
|
|
|
|
.rows
|
|
|
|
|
.iter()
|
|
|
|
@ -287,7 +247,7 @@ impl TableComponent {
|
|
|
|
|
new_rows
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn calculate_widths(
|
|
|
|
|
fn calculate_cell_widths(
|
|
|
|
|
&self,
|
|
|
|
|
area_width: u16,
|
|
|
|
|
) -> (usize, Vec<String>, Vec<Vec<String>>, Vec<Constraint>) {
|
|
|
|
@ -298,10 +258,10 @@ impl TableComponent {
|
|
|
|
|
self.column_page_start.set(self.selected_column_index());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let right_column_index = self.selected_column_index();
|
|
|
|
|
let far_right_column_index = self.selected_column_index();
|
|
|
|
|
let mut column_index = self.selected_column_index();
|
|
|
|
|
let number_clomn_width = (self.rows.len() + 1).to_string().width() as u16;
|
|
|
|
|
let mut widths = vec![];
|
|
|
|
|
let number_column_width = (self.rows.len() + 1).to_string().width() as u16;
|
|
|
|
|
let mut widths = Vec::new();
|
|
|
|
|
loop {
|
|
|
|
|
let length = self
|
|
|
|
|
.rows
|
|
|
|
@ -324,7 +284,7 @@ impl TableComponent {
|
|
|
|
|
.clamp(&3, &20)
|
|
|
|
|
});
|
|
|
|
|
if widths.iter().map(|(_, width)| width).sum::<usize>() + length + widths.len()
|
|
|
|
|
> area_width.saturating_sub(number_clomn_width) as usize
|
|
|
|
|
>= area_width.saturating_sub(number_column_width) as usize
|
|
|
|
|
{
|
|
|
|
|
column_index += 1;
|
|
|
|
|
break;
|
|
|
|
@ -335,12 +295,13 @@ impl TableComponent {
|
|
|
|
|
}
|
|
|
|
|
column_index -= 1;
|
|
|
|
|
}
|
|
|
|
|
let left_column_index = column_index;
|
|
|
|
|
widths.reverse();
|
|
|
|
|
|
|
|
|
|
let far_left_column_index = column_index;
|
|
|
|
|
let selected_column_index = widths.len().saturating_sub(1);
|
|
|
|
|
let mut column_index = right_column_index + 1;
|
|
|
|
|
let mut column_index = far_right_column_index + 1;
|
|
|
|
|
while widths.iter().map(|(_, width)| width).sum::<usize>() + widths.len()
|
|
|
|
|
<= area_width.saturating_sub(number_clomn_width) as usize
|
|
|
|
|
<= area_width.saturating_sub(number_column_width) as usize
|
|
|
|
|
{
|
|
|
|
|
let length = self
|
|
|
|
|
.rows
|
|
|
|
@ -375,7 +336,7 @@ impl TableComponent {
|
|
|
|
|
if self.selected_column_index() != self.headers.len().saturating_sub(1) {
|
|
|
|
|
widths.pop();
|
|
|
|
|
}
|
|
|
|
|
let right_column_index = column_index;
|
|
|
|
|
let far_right_column_index = column_index;
|
|
|
|
|
let mut constraints = widths
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(_, width)| Constraint::Length(*width as u16))
|
|
|
|
@ -383,21 +344,22 @@ impl TableComponent {
|
|
|
|
|
if self.selected_column_index() != self.headers.len().saturating_sub(1) {
|
|
|
|
|
constraints.push(Constraint::Min(10));
|
|
|
|
|
}
|
|
|
|
|
constraints.insert(0, Constraint::Length(number_clomn_width));
|
|
|
|
|
self.column_page_start.set(left_column_index);
|
|
|
|
|
constraints.insert(0, Constraint::Length(number_column_width));
|
|
|
|
|
self.column_page_start.set(far_left_column_index);
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
self.selected_right_cell
|
|
|
|
|
self.selection_area_corner
|
|
|
|
|
.map_or(selected_column_index + 1, |(x, _)| {
|
|
|
|
|
if x > self.selected_left_column_index {
|
|
|
|
|
if x > self.selected_column {
|
|
|
|
|
(selected_column_index + 1)
|
|
|
|
|
.saturating_sub(x.saturating_sub(self.selected_left_column_index))
|
|
|
|
|
.saturating_sub(x.saturating_sub(self.selected_column))
|
|
|
|
|
} else {
|
|
|
|
|
(selected_column_index + 1)
|
|
|
|
|
.saturating_add(self.selected_left_column_index.saturating_sub(x))
|
|
|
|
|
.saturating_add(self.selected_column.saturating_sub(x))
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
self.headers_with_number(left_column_index, right_column_index),
|
|
|
|
|
self.rows_with_number(left_column_index, right_column_index),
|
|
|
|
|
self.headers(far_left_column_index, far_right_column_index),
|
|
|
|
|
self.rows(far_left_column_index, far_right_column_index),
|
|
|
|
|
constraints,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
@ -410,7 +372,7 @@ impl DrawableComponent for TableComponent {
|
|
|
|
|
.constraints(vec![Constraint::Length(3), Constraint::Length(5)])
|
|
|
|
|
.split(area);
|
|
|
|
|
|
|
|
|
|
self.state.selected().map_or_else(
|
|
|
|
|
self.selected_row.selected().map_or_else(
|
|
|
|
|
|| {
|
|
|
|
|
self.scroll.reset();
|
|
|
|
|
},
|
|
|
|
@ -428,7 +390,7 @@ impl DrawableComponent for TableComponent {
|
|
|
|
|
|
|
|
|
|
let block = Block::default().borders(Borders::ALL).title("Records");
|
|
|
|
|
let (selected_column_index, headers, rows, constraints) =
|
|
|
|
|
self.calculate_widths(block.inner(layout[1]).width);
|
|
|
|
|
self.calculate_cell_widths(block.inner(layout[1]).width);
|
|
|
|
|
let header_cells = headers.iter().enumerate().map(|(column_index, h)| {
|
|
|
|
|
Cell::from(h.to_string()).style(if selected_column_index == column_index {
|
|
|
|
|
Style::default().add_modifier(Modifier::BOLD)
|
|
|
|
@ -448,7 +410,7 @@ impl DrawableComponent for TableComponent {
|
|
|
|
|
Cell::from(c.to_string()).style(
|
|
|
|
|
if self.is_selected_cell(row_index, column_index, selected_column_index) {
|
|
|
|
|
Style::default().bg(Color::Blue)
|
|
|
|
|
} else if self.is_row_number_clumn(row_index, column_index) {
|
|
|
|
|
} else if self.is_number_column(row_index, column_index) {
|
|
|
|
|
Style::default().add_modifier(Modifier::BOLD)
|
|
|
|
|
} else {
|
|
|
|
|
Style::default()
|
|
|
|
@ -461,24 +423,21 @@ impl DrawableComponent for TableComponent {
|
|
|
|
|
let table = Table::new(rows)
|
|
|
|
|
.header(header)
|
|
|
|
|
.block(block)
|
|
|
|
|
.highlight_style(if self.select_entire_row {
|
|
|
|
|
Style::default().bg(Color::Blue)
|
|
|
|
|
} else {
|
|
|
|
|
Style::default()
|
|
|
|
|
})
|
|
|
|
|
.style(if focused {
|
|
|
|
|
Style::default()
|
|
|
|
|
} else {
|
|
|
|
|
Style::default().fg(Color::DarkGray)
|
|
|
|
|
})
|
|
|
|
|
.widths(&constraints);
|
|
|
|
|
let mut state = self.selected_row.clone();
|
|
|
|
|
f.render_stateful_widget(
|
|
|
|
|
table,
|
|
|
|
|
layout[1],
|
|
|
|
|
if self.select_state.selected().is_some() {
|
|
|
|
|
&mut self.select_state
|
|
|
|
|
if let Some((_, y)) = self.selection_area_corner {
|
|
|
|
|
state.select(Some(y));
|
|
|
|
|
&mut state
|
|
|
|
|
} else {
|
|
|
|
|
&mut self.state
|
|
|
|
|
&mut self.selected_row
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
@ -495,29 +454,25 @@ impl Component for TableComponent {
|
|
|
|
|
return Ok(EventState::Consumed);
|
|
|
|
|
}
|
|
|
|
|
Key::Char('j') => {
|
|
|
|
|
self.next(1);
|
|
|
|
|
self.next_row(1);
|
|
|
|
|
return Ok(EventState::NotConsumed);
|
|
|
|
|
}
|
|
|
|
|
Key::Ctrl('d') => {
|
|
|
|
|
self.next(10);
|
|
|
|
|
self.next_row(10);
|
|
|
|
|
return Ok(EventState::NotConsumed);
|
|
|
|
|
}
|
|
|
|
|
Key::Char('k') => {
|
|
|
|
|
self.previous(1);
|
|
|
|
|
self.previous_row(1);
|
|
|
|
|
return Ok(EventState::Consumed);
|
|
|
|
|
}
|
|
|
|
|
Key::Ctrl('u') => {
|
|
|
|
|
self.previous(10);
|
|
|
|
|
self.previous_row(10);
|
|
|
|
|
return Ok(EventState::Consumed);
|
|
|
|
|
}
|
|
|
|
|
Key::Char('g') => {
|
|
|
|
|
self.scroll_top();
|
|
|
|
|
return Ok(EventState::Consumed);
|
|
|
|
|
}
|
|
|
|
|
Key::Char('r') => {
|
|
|
|
|
self.select_entire_row = true;
|
|
|
|
|
return Ok(EventState::Consumed);
|
|
|
|
|
}
|
|
|
|
|
Key::Char('G') => {
|
|
|
|
|
self.scroll_bottom();
|
|
|
|
|
return Ok(EventState::Consumed);
|
|
|
|
@ -557,7 +512,7 @@ mod test {
|
|
|
|
|
fn test_headers() {
|
|
|
|
|
let mut component = TableComponent::default();
|
|
|
|
|
component.headers = vec!["a", "b", "c"].iter().map(|h| h.to_string()).collect();
|
|
|
|
|
assert_eq!(component.headers_with_number(1, 2), vec!["", "b"])
|
|
|
|
|
assert_eq!(component.headers(1, 2), vec!["", "b"])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
@ -567,10 +522,7 @@ mod test {
|
|
|
|
|
vec!["a", "b", "c"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
vec!["d", "e", "f"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
];
|
|
|
|
|
assert_eq!(
|
|
|
|
|
component.rows_with_number(1, 2),
|
|
|
|
|
vec![vec!["1", "b"], vec!["2", "e"]],
|
|
|
|
|
)
|
|
|
|
|
assert_eq!(component.rows(1, 2), vec![vec!["1", "b"], vec!["2", "e"]],)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
@ -591,10 +543,10 @@ mod test {
|
|
|
|
|
vec!["a", "b", "c"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
vec!["d", "e", "f"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
];
|
|
|
|
|
component.state.select(Some(1));
|
|
|
|
|
component.selected_left_column_index = 1;
|
|
|
|
|
component.selected_row.select(Some(1));
|
|
|
|
|
component.selected_column = 1;
|
|
|
|
|
component.expand_selected_area_x(false);
|
|
|
|
|
assert_eq!(component.selected_right_cell, Some((0, 1)));
|
|
|
|
|
assert_eq!(component.selection_area_corner, Some((0, 1)));
|
|
|
|
|
assert_eq!(component.selected_cells(), Some("d,e".to_string()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -616,10 +568,10 @@ mod test {
|
|
|
|
|
vec!["a", "b", "c"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
vec!["d", "e", "f"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
];
|
|
|
|
|
component.state.select(Some(1));
|
|
|
|
|
component.selected_left_column_index = 1;
|
|
|
|
|
component.selected_row.select(Some(1));
|
|
|
|
|
component.selected_column = 1;
|
|
|
|
|
component.expand_selected_area_x(true);
|
|
|
|
|
assert_eq!(component.selected_right_cell, Some((2, 1)));
|
|
|
|
|
assert_eq!(component.selection_area_corner, Some((2, 1)));
|
|
|
|
|
assert_eq!(component.selected_cells(), Some("e,f".to_string()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -640,10 +592,10 @@ mod test {
|
|
|
|
|
vec!["a", "b", "c"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
vec!["d", "e", "f"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
];
|
|
|
|
|
component.state.select(Some(1));
|
|
|
|
|
component.selected_left_column_index = 1;
|
|
|
|
|
component.selected_row.select(Some(1));
|
|
|
|
|
component.selected_column = 1;
|
|
|
|
|
component.expand_selected_area_y(false);
|
|
|
|
|
assert_eq!(component.selected_right_cell, Some((1, 0)));
|
|
|
|
|
assert_eq!(component.selection_area_corner, Some((1, 0)));
|
|
|
|
|
assert_eq!(component.selected_cells(), Some("b\ne".to_string()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -664,13 +616,26 @@ mod test {
|
|
|
|
|
vec!["a", "b", "c"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
vec!["d", "e", "f"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
];
|
|
|
|
|
component.state.select(Some(0));
|
|
|
|
|
component.selected_left_column_index = 1;
|
|
|
|
|
component.selected_row.select(Some(0));
|
|
|
|
|
component.selected_column = 1;
|
|
|
|
|
component.expand_selected_area_y(true);
|
|
|
|
|
assert_eq!(component.selected_right_cell, Some((1, 1)));
|
|
|
|
|
assert_eq!(component.selection_area_corner, Some((1, 1)));
|
|
|
|
|
assert_eq!(component.selected_cells(), Some("b\ne".to_string()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_is_number_column() {
|
|
|
|
|
let mut component = TableComponent::default();
|
|
|
|
|
component.headers = vec!["1", "2", "3"].iter().map(|h| h.to_string()).collect();
|
|
|
|
|
component.rows = vec![
|
|
|
|
|
vec!["a", "b", "c"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
vec!["d", "e", "f"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
];
|
|
|
|
|
component.selected_row.select(Some(0));
|
|
|
|
|
assert!(component.is_number_column(0, 0));
|
|
|
|
|
assert!(!component.is_number_column(0, 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_selected_cell_when_one_cell_selected() {
|
|
|
|
|
// 1 2 3
|
|
|
|
@ -683,7 +648,7 @@ mod test {
|
|
|
|
|
vec!["a", "b", "c"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
vec!["d", "e", "f"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
];
|
|
|
|
|
component.state.select(Some(0));
|
|
|
|
|
component.selected_row.select(Some(0));
|
|
|
|
|
assert_eq!(component.selected_cells(), Some("a".to_string()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -699,8 +664,8 @@ mod test {
|
|
|
|
|
vec!["a", "b", "c"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
vec!["d", "e", "f"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
];
|
|
|
|
|
component.state.select(Some(0));
|
|
|
|
|
component.selected_right_cell = Some((1, 1));
|
|
|
|
|
component.selected_row.select(Some(0));
|
|
|
|
|
component.selection_area_corner = Some((1, 1));
|
|
|
|
|
assert_eq!(component.selected_cells(), Some("a,b\nd,e".to_string()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -716,7 +681,7 @@ mod test {
|
|
|
|
|
vec!["a", "b", "c"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
vec!["d", "e", "f"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
];
|
|
|
|
|
component.state.select(Some(0));
|
|
|
|
|
component.selected_row.select(Some(0));
|
|
|
|
|
// a
|
|
|
|
|
assert!(component.is_selected_cell(0, 1, 1));
|
|
|
|
|
// d
|
|
|
|
@ -737,8 +702,8 @@ mod test {
|
|
|
|
|
vec!["a", "b", "c"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
vec!["d", "e", "f"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
];
|
|
|
|
|
component.state.select(Some(0));
|
|
|
|
|
component.selected_right_cell = Some((1, 1));
|
|
|
|
|
component.selected_row.select(Some(0));
|
|
|
|
|
component.selection_area_corner = Some((1, 1));
|
|
|
|
|
// a
|
|
|
|
|
assert!(component.is_selected_cell(0, 1, 1));
|
|
|
|
|
// b
|
|
|
|
@ -752,7 +717,7 @@ mod test {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_calculate_widths() {
|
|
|
|
|
fn test_calculate_cell_widths() {
|
|
|
|
|
let mut component = TableComponent::default();
|
|
|
|
|
component.headers = vec!["1", "2", "3"].iter().map(|h| h.to_string()).collect();
|
|
|
|
|
component.rows = vec![
|
|
|
|
@ -762,7 +727,8 @@ mod test {
|
|
|
|
|
.collect(),
|
|
|
|
|
vec!["d", "e", "f"].iter().map(|h| h.to_string()).collect(),
|
|
|
|
|
];
|
|
|
|
|
let (selected_column_index, headers, rows, constraints) = component.calculate_widths(10);
|
|
|
|
|
let (selected_column_index, headers, rows, constraints) =
|
|
|
|
|
component.calculate_cell_widths(10);
|
|
|
|
|
assert_eq!(selected_column_index, 1);
|
|
|
|
|
assert_eq!(headers, vec!["", "1", "2"]);
|
|
|
|
|
assert_eq!(rows, vec![vec!["1", "aaaaa", "bbbbb"], vec!["2", "d", "e"]]);
|
|
|
|
@ -775,7 +741,8 @@ mod test {
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let (selected_column_index, headers, rows, constraints) = component.calculate_widths(20);
|
|
|
|
|
let (selected_column_index, headers, rows, constraints) =
|
|
|
|
|
component.calculate_cell_widths(20);
|
|
|
|
|
assert_eq!(selected_column_index, 1);
|
|
|
|
|
assert_eq!(headers, vec!["", "1", "2", "3"]);
|
|
|
|
|
assert_eq!(
|
|
|
|
@ -794,5 +761,39 @@ mod test {
|
|
|
|
|
Constraint::Min(10),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let mut component = TableComponent::default();
|
|
|
|
|
component.headers = vec!["1", "2", "3"].iter().map(|h| h.to_string()).collect();
|
|
|
|
|
component.rows = vec![
|
|
|
|
|
vec!["aaaaa", "bbbbb", "ccccc"]
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|h| h.to_string())
|
|
|
|
|
.collect(),
|
|
|
|
|
vec!["dddddddddd", "e", "f"]
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|h| h.to_string())
|
|
|
|
|
.collect(),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let (selected_column_index, headers, rows, constraints) =
|
|
|
|
|
component.calculate_cell_widths(20);
|
|
|
|
|
assert_eq!(selected_column_index, 1);
|
|
|
|
|
assert_eq!(headers, vec!["", "1", "2", "3"]);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
rows,
|
|
|
|
|
vec![
|
|
|
|
|
vec!["1", "aaaaa", "bbbbb", "ccccc"],
|
|
|
|
|
vec!["2", "dddddddddd", "e", "f"]
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
constraints,
|
|
|
|
|
vec![
|
|
|
|
|
Constraint::Length(1),
|
|
|
|
|
Constraint::Length(10),
|
|
|
|
|
Constraint::Length(5),
|
|
|
|
|
Constraint::Min(10),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|