[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 639 additions and 343 deletions

File diff suppressed because it is too large Load Diff

View File

@ -31,8 +31,8 @@ fn table_expand_0() {
"╭───┬───┬───┬───────────╮\
# a b c \
\
0 1 2 3 \
1 4 5 \
1 1 2 3 \
2 4 5 \
0 1 \
1 2 \
2 3 \
@ -41,6 +41,48 @@ fn table_expand_0() {
);
}
// I am not sure whether the test is platform depent, cause we don't set a term_width on our own
#[test]
fn table_expand_exeed_overlap_0() {
// no expand
let actual = nu!(r#"[[a b, c]; [xxxxxxxxxxxxxxxxxxxxxx 2 3] [4 5 [1 2 3]]] | table --expand"#);
assert_eq!(
actual.out,
"╭───┬────────────────────────┬───┬───────────╮\
# a b c \
\
1 xxxxxxxxxxxxxxxxxxxxxx 2 3 \
2 4 5 \
0 1 \
1 2 \
2 3 \
\
",
);
// expand
let actual = nu!(
r#"[[a b, c]; [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 2 3] [4 5 [1 2 3]]] | table --expand"#
);
assert_eq!(
actual.out,
"╭──────┬───────────────────────────────────────────────────┬─────┬─────────────╮\
# a b c \
\
1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 2 3 \
2 4 5 \
0 1 \
1 2 \
2 3 \
\
"
);
}
#[test]
fn table_expand_deep_0() {
let actual = nu!(r#"[[a b, c]; [1 2 3] [4 5 [1 2 [1 2 3]]]] | table --expand --expand-deep=1"#);
@ -49,8 +91,8 @@ fn table_expand_deep_0() {
"╭───┬───┬───┬────────────────────────╮\
# a b c \
\
0 1 2 3 \
1 4 5 \
1 1 2 3 \
2 4 5 \
0 1 \
1 2 \
2 [list 3 items] \
@ -67,8 +109,8 @@ fn table_expand_deep_1() {
"╭───┬───┬───┬────────────────╮\
# a b c \
\
0 1 2 3 \
1 4 5 [list 3 items] \
1 1 2 3 \
2 4 5 [list 3 items] \
"
);
}
@ -81,8 +123,8 @@ fn table_expand_flatten_0() {
"╭───┬───┬───┬───────────────╮\
# a b c \
\
0 1 2 3 \
1 4 5 \
1 1 2 3 \
2 4 5 \
0 1 \
1 2 \
2 1 1 1 \
@ -101,8 +143,8 @@ fn table_expand_flatten_1() {
"╭───┬───┬───┬───────────────╮\
# a b c \
\
0 1 2 3 \
1 4 5 \
1 1 2 3 \
2 4 5 \
0 1 \
1 2 \
2 1,1,1 \
@ -122,8 +164,8 @@ fn table_expand_flatten_and_deep_1() {
"╭───┬───┬───┬────────────────────────────────╮\
# a b c \
\
0 1 2 3 \
1 4 5 \
1 1 2 3 \
2 4 5 \
0 1 \
1 2 \
2 \

View File

@ -40,6 +40,7 @@ fn main() {
Alignments::default(),
&TableTheme::rounded(),
width,
false,
)
.unwrap_or_else(|| format!("Couldn't fit table into {} columns!", width));
// Draw the table

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);

View File

@ -188,7 +188,7 @@ fn truncate_with_suffix_test() {
fn draw_table(table: Table, limit: usize, cfg: &Config) -> Option<String> {
let styles = HashMap::default();
let alignments = Alignments::default();
table.draw_table(cfg, &styles, alignments, &theme::heavy(), limit)
table.draw_table(cfg, &styles, alignments, &theme::heavy(), limit, false)
}
fn row(count_columns: usize) -> Vec<TCell<CellInfo<'static>, TextStyle>> {

View File

@ -0,0 +1,46 @@
use std::collections::HashMap;
use nu_protocol::Config;
use nu_table::{Alignments, Table, TableTheme as theme, TextStyle};
use tabled::papergrid::records::{cell_info::CellInfo, tcell::TCell};
#[test]
fn test_expand() {
assert_eq!(
draw_table(vec![row(4); 3], 4, true, theme::rounded(), 50),
"╭────────────┬───────────┬───────────┬───────────╮\n\
0 1 2 3 \n\
\n\
0 1 2 3 \n\
0 1 2 3 \n\
"
);
}
fn draw_table(
data: Vec<Vec<TCell<CellInfo<'static>, TextStyle>>>,
count_columns: usize,
with_header: bool,
theme: theme,
width: usize,
) -> String {
let size = (data.len(), count_columns);
let table = Table::new(data, size, width, with_header, false);
let cfg = Config::default();
let styles = HashMap::default();
let alignments = Alignments::default();
table
.draw_table(&cfg, &styles, alignments, &theme, width, true)
.expect("Unexpectdly got no table")
}
fn row(count_columns: usize) -> Vec<TCell<CellInfo<'static>, TextStyle>> {
let mut row = Vec::with_capacity(count_columns);
for i in 0..count_columns {
row.push(Table::create_cell(i.to_string(), TextStyle::default()));
}
row
}

View File

@ -464,7 +464,7 @@ fn draw_table(
let styles = HashMap::default();
let alignments = Alignments::default();
table
.draw_table(&cfg, &styles, alignments, &theme, std::usize::MAX)
.draw_table(&cfg, &styles, alignments, &theme, std::usize::MAX, false)
.expect("Unexpectdly got no table")
}