mirror of
https://github.com/nushell/nushell
synced 2025-01-12 21:29:07 +00:00
nu-table: fix issue with truncation and text border (#10050)
I did only few manual tests, so maybe shall be run before. todo: maybe need to add a test case for it. fix #9993 cc: @fdncred --------- Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
This commit is contained in:
parent
2aa4cd5cc5
commit
1e3248dfe8
1 changed files with 84 additions and 60 deletions
|
@ -247,38 +247,42 @@ fn draw_table(
|
||||||
align_table(&mut table, alignments, with_index, with_header, with_footer);
|
align_table(&mut table, alignments, with_index, with_header, with_footer);
|
||||||
colorize_table(&mut table, styles, with_index, with_header, with_footer);
|
colorize_table(&mut table, styles, with_index, with_header, with_footer);
|
||||||
|
|
||||||
let total_width = control_table_width(&mut table, cfg, widths, termwidth);
|
let width_ctrl = TableWidthCtrl::new(widths, cfg, termwidth);
|
||||||
|
|
||||||
// we need to do it after width control cause we of ColumnNames internals.
|
|
||||||
if with_header && border_header {
|
if with_header && border_header {
|
||||||
set_border_head(&mut table, with_footer);
|
set_border_head(&mut table, with_footer, width_ctrl);
|
||||||
|
} else {
|
||||||
|
table.with(width_ctrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
build_table_with_width_check(table, total_width, termwidth)
|
let tablewidth = table.total_width();
|
||||||
|
|
||||||
|
table_to_string(table, tablewidth, termwidth)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_indent(table: &mut Table, left: usize, right: usize) {
|
fn set_indent(table: &mut Table, left: usize, right: usize) {
|
||||||
table.with(Padding::new(left, right, 0, 0));
|
table.with(Padding::new(left, right, 0, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_border_head(table: &mut Table, with_footer: bool) {
|
fn set_border_head(table: &mut Table, with_footer: bool, wctrl: TableWidthCtrl) {
|
||||||
let count_rows = table.count_rows();
|
|
||||||
|
|
||||||
if with_footer {
|
if with_footer {
|
||||||
table.with(Settings::new(
|
let count_rows = table.count_rows();
|
||||||
HeaderMove((0, 0), true),
|
table.with(
|
||||||
HeaderMove((count_rows - 1 - 1, count_rows - 1), false),
|
Settings::default()
|
||||||
));
|
.with(wctrl)
|
||||||
|
.with(HeaderMove((0, 0), true))
|
||||||
|
.with(HeaderMove((count_rows - 1 - 1, count_rows - 1), false)),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
table.with(HeaderMove((0, 0), true));
|
table.with(
|
||||||
|
Settings::default()
|
||||||
|
.with(wctrl)
|
||||||
|
.with(HeaderMove((0, 0), true)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_table_with_width_check(
|
fn table_to_string(table: Table, total_width: usize, termwidth: usize) -> Option<String> {
|
||||||
table: Table,
|
|
||||||
total_width: usize,
|
|
||||||
termwidth: usize,
|
|
||||||
) -> Option<String> {
|
|
||||||
if total_width > termwidth {
|
if total_width > termwidth {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
|
@ -287,25 +291,71 @@ fn build_table_with_width_check(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn control_table_width(
|
struct TableWidthCtrl {
|
||||||
table: &mut Table,
|
width: Vec<usize>,
|
||||||
cfg: NuTableConfig,
|
cfg: NuTableConfig,
|
||||||
widths: Vec<usize>,
|
max: usize,
|
||||||
termwidth: usize,
|
}
|
||||||
) -> usize {
|
|
||||||
let total_width = get_total_width2(&widths, table.get_config());
|
|
||||||
if total_width > termwidth {
|
|
||||||
table_trim_columns(table, widths, termwidth, &cfg.trim);
|
|
||||||
table.total_width()
|
|
||||||
} else if cfg.expand && termwidth > total_width {
|
|
||||||
table.with(Settings::new(
|
|
||||||
SetDimensions(widths),
|
|
||||||
Width::increase(termwidth),
|
|
||||||
));
|
|
||||||
|
|
||||||
termwidth
|
impl TableWidthCtrl {
|
||||||
} else {
|
fn new(width: Vec<usize>, cfg: NuTableConfig, max: usize) -> Self {
|
||||||
total_width
|
Self { width, cfg, max }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TableOption<NuRecords, CompleteDimensionVecRecords<'_>, ColoredConfig> for TableWidthCtrl {
|
||||||
|
fn change(
|
||||||
|
self,
|
||||||
|
rec: &mut NuRecords,
|
||||||
|
cfg: &mut ColoredConfig,
|
||||||
|
dim: &mut CompleteDimensionVecRecords<'_>,
|
||||||
|
) {
|
||||||
|
let total_width = get_total_width2(&self.width, cfg);
|
||||||
|
if total_width > self.max {
|
||||||
|
TableTrim {
|
||||||
|
max: self.max,
|
||||||
|
strategy: self.cfg.trim,
|
||||||
|
width: self.width,
|
||||||
|
}
|
||||||
|
.change(rec, cfg, dim);
|
||||||
|
} else if self.cfg.expand && self.max > total_width {
|
||||||
|
Settings::new(SetDimensions(self.width), Width::increase(self.max))
|
||||||
|
.change(rec, cfg, dim)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TableTrim {
|
||||||
|
width: Vec<usize>,
|
||||||
|
strategy: TrimStrategy,
|
||||||
|
max: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TableOption<NuRecords, CompleteDimensionVecRecords<'_>, ColoredConfig> for TableTrim {
|
||||||
|
fn change(
|
||||||
|
self,
|
||||||
|
recs: &mut NuRecords,
|
||||||
|
cfg: &mut ColoredConfig,
|
||||||
|
dims: &mut CompleteDimensionVecRecords<'_>,
|
||||||
|
) {
|
||||||
|
match self.strategy {
|
||||||
|
TrimStrategy::Wrap { try_to_keep_words } => {
|
||||||
|
let mut wrap = Width::wrap(self.max).priority::<PriorityMax>();
|
||||||
|
if try_to_keep_words {
|
||||||
|
wrap = wrap.keep_words();
|
||||||
|
}
|
||||||
|
|
||||||
|
Settings::new(SetDimensions(self.width), wrap).change(recs, cfg, dims);
|
||||||
|
}
|
||||||
|
TrimStrategy::Truncate { suffix } => {
|
||||||
|
let mut truncate = Width::truncate(self.max).priority::<PriorityMax>();
|
||||||
|
if let Some(suffix) = suffix {
|
||||||
|
truncate = truncate.suffix(suffix).suffix_try_color(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Settings::new(SetDimensions(self.width), truncate).change(recs, cfg, dims);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -411,32 +461,6 @@ impl<R: ExactRecords, D> TableOption<R, D, ColoredConfig> for FooterStyle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn table_trim_columns(
|
|
||||||
table: &mut Table,
|
|
||||||
widths: Vec<usize>,
|
|
||||||
termwidth: usize,
|
|
||||||
trim_strategy: &TrimStrategy,
|
|
||||||
) {
|
|
||||||
match trim_strategy {
|
|
||||||
TrimStrategy::Wrap { try_to_keep_words } => {
|
|
||||||
let mut wrap = Width::wrap(termwidth).priority::<PriorityMax>();
|
|
||||||
if *try_to_keep_words {
|
|
||||||
wrap = wrap.keep_words();
|
|
||||||
}
|
|
||||||
|
|
||||||
table.with(Settings::new(SetDimensions(widths), wrap));
|
|
||||||
}
|
|
||||||
TrimStrategy::Truncate { suffix } => {
|
|
||||||
let mut truncate = Width::truncate(termwidth).priority::<PriorityMax>();
|
|
||||||
if let Some(suffix) = suffix {
|
|
||||||
truncate = truncate.suffix(suffix).suffix_try_color(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
table.with(Settings::new(SetDimensions(widths), truncate));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn maybe_truncate_columns(
|
fn maybe_truncate_columns(
|
||||||
data: &mut NuRecords,
|
data: &mut NuRecords,
|
||||||
theme: &TableTheme,
|
theme: &TableTheme,
|
||||||
|
|
Loading…
Reference in a new issue