From 1ffb2c36c015a53f3bdb4b6c90652858df6e394c Mon Sep 17 00:00:00 2001 From: Takayuki Maeda <41065217+TaKO8Ki@users.noreply.github.com> Date: Sat, 11 Sep 2021 13:07:53 +0900 Subject: [PATCH] Integrate table_value into table (#90) * integrate table_value into table * remove wrap * remove wrap and alignment --- src/components/table.rs | 36 ++++++++++++++++++---------------- src/components/table_status.rs | 5 ++--- src/components/table_value.rs | 21 +++++--------------- 3 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/components/table.rs b/src/components/table.rs index d2d1688..6cb07a6 100644 --- a/src/components/table.rs +++ b/src/components/table.rs @@ -401,9 +401,18 @@ impl TableComponent { impl DrawableComponent for TableComponent { fn draw(&mut self, f: &mut Frame, area: Rect, focused: bool) -> Result<()> { - let layout = Layout::default() + let chunks = Layout::default() + .vertical_margin(1) + .horizontal_margin(1) .direction(Direction::Vertical) - .constraints(vec![Constraint::Length(3), Constraint::Length(5)]) + .constraints( + [ + Constraint::Length(2), + Constraint::Min(1), + Constraint::Length(2), + ] + .as_ref(), + ) .split(area); f.render_widget( @@ -415,16 +424,9 @@ impl DrawableComponent for TableComponent { } else { Style::default().fg(Color::DarkGray) }), - layout[1], + area, ); - let chunks = Layout::default() - .vertical_margin(1) - .horizontal_margin(1) - .direction(Direction::Vertical) - .constraints([Constraint::Min(1), Constraint::Length(2)].as_ref()) - .split(layout[1]); - self.selected_row.selected().map_or_else( || { self.scroll.reset(); @@ -433,14 +435,11 @@ impl DrawableComponent for TableComponent { self.scroll.update( selection, self.rows.len(), - layout[1].height.saturating_sub(2) as usize, + chunks[1].height.saturating_sub(2) as usize, ); }, ); - TableValueComponent::new(self.selected_cells().unwrap_or_default()) - .draw(f, layout[0], focused)?; - let block = Block::default().borders(Borders::NONE); let (selected_column_index, headers, rows, constraints) = self.calculate_cell_widths(block.inner(chunks[0]).width); @@ -485,7 +484,7 @@ impl DrawableComponent for TableComponent { let mut state = self.selected_row.clone(); f.render_stateful_widget( table, - chunks[0], + chunks[1], if let Some((_, y)) = self.selection_area_corner { state.select(Some(y)); &mut state @@ -494,6 +493,9 @@ impl DrawableComponent for TableComponent { }, ); + TableValueComponent::new(self.selected_cells().unwrap_or_default()) + .draw(f, chunks[0], focused)?; + TableStatusComponent::new( if self.rows.is_empty() { None @@ -507,9 +509,9 @@ impl DrawableComponent for TableComponent { }, self.table.as_ref().map(|t| t.1.clone()), ) - .draw(f, chunks[1], focused)?; + .draw(f, chunks[2], focused)?; - self.scroll.draw(f, chunks[0]); + self.scroll.draw(f, chunks[1]); Ok(()) } } diff --git a/src/components/table_status.rs b/src/components/table_status.rs index eebf268..38ce428 100644 --- a/src/components/table_status.rs +++ b/src/components/table_status.rs @@ -8,7 +8,7 @@ use tui::{ layout::Rect, style::{Color, Style}, text::{Span, Spans}, - widgets::{Block, Borders, Paragraph, Wrap}, + widgets::{Block, Borders, Paragraph}, Frame, }; @@ -64,8 +64,7 @@ impl DrawableComponent for TableStatusComponent { Style::default() } else { Style::default().fg(Color::DarkGray) - })) - .wrap(Wrap { trim: true }); + })); f.render_widget(status, area); Ok(()) } diff --git a/src/components/table_value.rs b/src/components/table_value.rs index 243803d..649bff4 100644 --- a/src/components/table_value.rs +++ b/src/components/table_value.rs @@ -4,10 +4,9 @@ use crate::event::Key; use anyhow::Result; use tui::{ backend::Backend, - layout::{Alignment, Rect}, - style::{Color, Modifier, Style}, - text::Span, - widgets::{Block, Borders, Paragraph, Wrap}, + layout::Rect, + style::{Color, Style}, + widgets::{Block, Borders, Paragraph}, Frame, }; @@ -24,22 +23,12 @@ impl TableValueComponent { impl DrawableComponent for TableValueComponent { fn draw(&mut self, f: &mut Frame, area: Rect, focused: bool) -> Result<()> { let paragraph = Paragraph::new(self.value.clone()) - .block( - Block::default() - .borders(Borders::ALL) - .style(Style::default()) - .title(Span::styled( - "Value", - Style::default().add_modifier(Modifier::BOLD), - )), - ) + .block(Block::default().borders(Borders::BOTTOM)) .style(if focused { Style::default() } else { Style::default().fg(Color::DarkGray) - }) - .alignment(Alignment::Left) - .wrap(Wrap { trim: true }); + }); f.render_widget(paragraph, area); Ok(()) }