Fix of a fix of #8671 (#8675)

Hi @fdncred,

I accidentally noticed that your fix tricks wrapping a bit.


![image](https://user-images.githubusercontent.com/20165848/228908201-0d5c7878-739f-43a3-b931-a8dc9df85cd7.png)


It must address it.

PS: I believe the issue was originally caused by a false positive of
clippy. (maybe we shall report it I am not sure)

---------

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
This commit is contained in:
Maxim Zhiburt 2023-03-31 15:53:57 +03:00 committed by GitHub
parent 3db0aed9f7
commit 8a85299575
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,8 +7,9 @@ use tabled::{
}; };
use self::{ use self::{
global_horizontal_char::SetHorizontalChar, peak2::Peak2, table_column_width::GetColumnWidths, global_horizontal_char::SetHorizontalCharOnFirstRow, peak2::Peak2,
truncate_table::TruncateTable, width_increase::IncWidth, table_column_width::get_first_cell_width, truncate_table::TruncateTable,
width_increase::IncWidth,
}; };
pub fn build_table(value: Value, description: String, termsize: usize) -> String { pub fn build_table(value: Value, description: String, termsize: usize) -> String {
@ -23,11 +24,8 @@ pub fn build_table(value: Value, description: String, termsize: usize) -> String
let mut desc_table = Builder::from(desc).build(); let mut desc_table = Builder::from(desc).build();
let desc_table_width = desc_table.total_width(); let desc_table_width = desc_table.total_width();
let width = if desc_table_width > termsize { #[allow(clippy::manual_clamp)]
val_table_width.clamp(termsize, desc_table_width) let width = val_table_width.max(desc_table_width).min(termsize);
} else {
val_table_width.clamp(desc_table_width, termsize)
};
desc_table desc_table
.with(Style::rounded().off_bottom()) .with(Style::rounded().off_bottom())
@ -40,10 +38,11 @@ pub fn build_table(value: Value, description: String, termsize: usize) -> String
.with(Wrap::new(width).priority::<PriorityMax>()) .with(Wrap::new(width).priority::<PriorityMax>())
.with(IncWidth(width)); .with(IncWidth(width));
let mut desc_widths = GetColumnWidths(Vec::new()); // we use only 1, cause left border considered 0 position
desc_table.with(&mut desc_widths); let count_split_lines = 1;
let desc_width = get_first_cell_width(&mut desc_table) + count_split_lines;
val_table.with(SetHorizontalChar::new('┼', '┴', 0, desc_widths.0[0])); val_table.with(SetHorizontalCharOnFirstRow::new('┼', '┴', desc_width));
format!("{desc_table}\n{val_table}") format!("{desc_table}\n{val_table}")
} }
@ -138,17 +137,12 @@ mod util {
), ),
Value::List { vals, .. } => { Value::List { vals, .. } => {
let mut columns = get_columns(&vals); let mut columns = get_columns(&vals);
let mut data = convert_records_to_dataset(&columns, vals); let data = convert_records_to_dataset(&columns, vals);
if columns.is_empty() && !data.is_empty() { if columns.is_empty() {
columns = vec![String::from("")]; columns = vec![String::from("")];
} }
// We need something to draw a table with
if data.is_empty() {
data.push(vec!["<empty data>".to_string()])
}
(columns, data) (columns, data)
} }
Value::String { val, span } => { Value::String { val, span } => {
@ -163,7 +157,7 @@ mod util {
(vec![String::from("")], lines) (vec![String::from("")], lines)
} }
Value::Nothing { .. } => (vec!["".to_string()], vec![vec!["<empty data>".to_string()]]), Value::Nothing { .. } => (vec![], vec![]),
value => ( value => (
vec![String::from("")], vec![String::from("")],
vec![vec![debug_string_without_formatting(&value)]], vec![vec![debug_string_without_formatting(&value)]],
@ -270,18 +264,30 @@ mod peak2 {
} }
mod table_column_width { mod table_column_width {
use tabled::papergrid::{records::Records, Estimate}; use tabled::{
papergrid::{records::Records, width::CfgWidthFunction},
Table,
};
pub struct GetColumnWidths(pub Vec<usize>); pub fn get_first_cell_width<R: Records>(table: &mut Table<R>) -> usize {
let mut opt = GetFirstCellWidth(0);
table.with(&mut opt);
opt.0
}
impl<R> tabled::TableOption<R> for GetColumnWidths struct GetFirstCellWidth(pub usize);
where
R: Records, impl<R: Records> tabled::TableOption<R> for GetFirstCellWidth {
{
fn change(&mut self, table: &mut tabled::Table<R>) { fn change(&mut self, table: &mut tabled::Table<R>) {
let mut evaluator = tabled::papergrid::width::WidthEstimator::default(); let w = table
evaluator.estimate(table.get_records(), table.get_config()); .get_records()
self.0 = evaluator.into(); .get_width((0, 0), CfgWidthFunction::default());
let pad = table
.get_config()
.get_padding(tabled::papergrid::Entity::Cell(0, 0));
let pad = pad.left.size + pad.right.size;
self.0 = w + pad;
} }
} }
} }
@ -292,91 +298,65 @@ mod global_horizontal_char {
Table, TableOption, Table, TableOption,
}; };
pub struct SetHorizontalChar { pub struct SetHorizontalCharOnFirstRow {
c1: char, c1: char,
c2: char, c2: char,
line: usize, pos: usize,
position: usize,
} }
impl SetHorizontalChar { impl SetHorizontalCharOnFirstRow {
pub fn new(c1: char, c2: char, line: usize, position: usize) -> Self { pub fn new(c1: char, c2: char, pos: usize) -> Self {
Self { Self { c1, c2, pos }
c1,
c2,
line,
position,
}
} }
} }
impl<R> TableOption<R> for SetHorizontalChar impl<R> TableOption<R> for SetHorizontalCharOnFirstRow
where where
R: Records, R: Records,
{ {
fn change(&mut self, table: &mut Table<R>) { fn change(&mut self, table: &mut Table<R>) {
let shape = table.shape(); if table.is_empty() {
return;
let is_last_line = self.line == (shape.0 * 2);
let mut row = self.line;
if is_last_line {
row = self.line - 1;
} }
let shape = table.shape();
let mut evaluator = WidthEstimator::default(); let mut evaluator = WidthEstimator::default();
evaluator.estimate(table.get_records(), table.get_config()); evaluator.estimate(table.get_records(), table.get_config());
let widths: Vec<_> = evaluator.into(); let widths: Vec<_> = evaluator.into();
let mut i = 0; let has_vertical = table.get_config().has_vertical(0, shape.1);
if has_vertical && self.pos == 0 {
let mut border = table.get_config().get_border((0, 0), shape);
border.left_top_corner = Some(self.c1);
table.get_config_mut().set_border((0, 0), border);
return;
}
let mut i = 1;
#[allow(clippy::needless_range_loop)] #[allow(clippy::needless_range_loop)]
for column in 0..shape.1 { for (col, width) in widths.into_iter().enumerate() {
let has_vertical = table.get_config().has_vertical(column, shape.1); if self.pos < i + width {
let o = self.pos - i;
table
.get_config_mut()
.override_horizontal_border((0, col), self.c2, Begin(o));
return;
}
i += width;
let has_vertical = table.get_config().has_vertical(col, shape.1);
if has_vertical { if has_vertical {
if self.position == i { if self.pos == i {
let mut border = table.get_config().get_border((row, column), shape); let mut border = table.get_config().get_border((0, col), shape);
if is_last_line { border.right_top_corner = Some(self.c1);
border.left_bottom_corner = Some(self.c1); table.get_config_mut().set_border((0, col), border);
} else {
border.left_top_corner = Some(self.c1);
}
table.get_config_mut().set_border((row, column), border);
return; return;
} }
i += 1; i += 1;
} }
let width = widths[column];
if self.position < i + width {
let offset = self.position + 1 - i;
// let offset = width - offset;
table.get_config_mut().override_horizontal_border(
(self.line, column),
self.c2,
Begin(offset),
);
return;
}
i += width;
}
let has_vertical = table.get_config().has_vertical(shape.1, shape.1);
if self.position == i && has_vertical {
let mut border = table.get_config().get_border((row, shape.1), shape);
if is_last_line {
border.left_bottom_corner = Some(self.c1);
} else {
border.left_top_corner = Some(self.c1);
}
table.get_config_mut().set_border((row, shape.1), border);
} }
} }
} }