[WIP] table: Change Record view in expand-mode (#6885)

* table: Change Record view in expand-mode

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>

* Fix width issue

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>

* Remove debug println!

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>

* Update logic

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>

* Improve the logic via a wrapping

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>

* `table -e` spread table to the whole width

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>

* fix CI

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>

* Fixing tests

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>

* Fix coloring issues

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>

* Don't expand when can

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>

* Fix tests

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>

* Change the logic

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>

* Fix cargo fmt

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
This commit is contained in:
Maxim Zhiburt
2022-11-16 17:03:56 +03:00
committed by GitHub
parent 708fee535c
commit 02ad491dea
8 changed files with 639 additions and 343 deletions

View File

@ -8,6 +8,19 @@ pub use table::{Alignments, Table};
pub use table_theme::TableTheme;
pub use textstyle::{Alignment, TextStyle};
use tabled::{Padding, Style, Width};
pub fn string_width(text: &str) -> usize {
tabled::papergrid::util::string_width_multiline_tab(text, 4)
}
pub fn wrap_string(text: &str, width: usize) -> String {
// well... it's not effitient to build a table to wrap a string,
// but ... it's better than a copy paste
tabled::builder::Builder::from_iter([[text]])
.build()
.with(Padding::zero())
.with(Style::empty())
.with(Width::wrap(width))
.to_string()
}

View File

@ -20,7 +20,7 @@ use tabled::{
use crate::{table_theme::TableTheme, TextStyle};
/// Table represent a table view.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Table {
data: Data,
is_empty: bool,
@ -63,8 +63,11 @@ impl Table {
}
}
pub fn create_cell(text: String, style: TextStyle) -> TCell<CellInfo<'static>, TextStyle> {
TCell::new(CellInfo::new(text, CfgWidthFunction::new(4)), style)
pub fn create_cell(
text: impl Into<String>,
style: TextStyle,
) -> TCell<CellInfo<'static>, TextStyle> {
TCell::new(CellInfo::new(text.into(), CfgWidthFunction::new(4)), style)
}
pub fn is_empty(&self) -> bool {
@ -85,6 +88,10 @@ impl Table {
let mut table = Builder::custom(self.data.clone()).build();
load_theme(&mut table, &HashMap::new(), theme, false, false);
let total = table.total_width();
// println!("{}", table);
// println!("width={:?} total={:?}", width, total);
drop(table);
if total > width {
@ -120,8 +127,9 @@ impl Table {
alignments: Alignments,
theme: &TableTheme,
termwidth: usize,
expand: bool,
) -> Option<String> {
draw_table(self, config, color_hm, alignments, theme, termwidth)
draw_table(self, config, color_hm, alignments, theme, termwidth, expand)
}
}
@ -149,6 +157,7 @@ fn draw_table(
alignments: Alignments,
theme: &TableTheme,
termwidth: usize,
expand: bool,
) -> Option<String> {
if table.is_empty {
return None;
@ -165,6 +174,11 @@ fn draw_table(
let mut table = Builder::custom(table.data).build();
load_theme(&mut table, color_hm, theme, with_footer, with_header);
align_table(&mut table, alignments, with_index, with_header, with_footer);
if expand {
table.with(Width::increase(termwidth));
}
table_trim_columns(&mut table, termwidth, &config.trim_strategy);
let table = print_table(table, config);