mirror of
https://github.com/nushell/nushell.git
synced 2025-08-11 16:44:54 +02:00
Rework implementation method
This commit is contained in:
@ -10,8 +10,11 @@ use prettytable::{color, Attr, Cell, Row, Table};
|
||||
|
||||
#[derive(Debug, new)]
|
||||
pub struct TableView {
|
||||
// List of header cell values:
|
||||
headers: Vec<String>,
|
||||
entries: Vec<Vec<String>>,
|
||||
|
||||
// List of rows of cells, each containing value and prettytable style-string:
|
||||
entries: Vec<Vec<(String, &'static str)>>,
|
||||
}
|
||||
|
||||
impl TableView {
|
||||
@ -41,21 +44,26 @@ impl TableView {
|
||||
let mut entries = vec![];
|
||||
|
||||
for (idx, value) in values.iter().enumerate() {
|
||||
let mut row: Vec<String> = match value {
|
||||
let mut row: Vec<(String, &'static str)> = match value {
|
||||
Tagged {
|
||||
item: Value::Object(..),
|
||||
..
|
||||
} => headers
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, d)| value.get_data(d).borrow().format_leaf(Some(&headers[i])))
|
||||
.map(|(i, d)| {
|
||||
let data = value.get_data(d);
|
||||
return (data.borrow().format_leaf(Some(&headers[i])), data.borrow().style_leaf());
|
||||
})
|
||||
.collect(),
|
||||
x => vec![x.format_leaf(None)],
|
||||
x => vec![(x.format_leaf(None), x.style_leaf())],
|
||||
};
|
||||
|
||||
if values.len() > 1 {
|
||||
row.insert(0, format!("{}", idx.to_string()));
|
||||
// Indices are black, bold, right-aligned:
|
||||
row.insert(0, (format!("{}", idx.to_string()), "Fdbr"));
|
||||
}
|
||||
|
||||
entries.push(row);
|
||||
}
|
||||
|
||||
@ -66,13 +74,15 @@ impl TableView {
|
||||
}
|
||||
|
||||
for head in 0..headers.len() {
|
||||
let mut current_row_max = 0;
|
||||
let mut current_col_max = 0;
|
||||
for row in 0..values.len() {
|
||||
if entries[row][head].len() > current_row_max {
|
||||
current_row_max = entries[row][head].len();
|
||||
let value_length = entries[row][head].0.len();
|
||||
if value_length > current_col_max {
|
||||
current_col_max = value_length;
|
||||
}
|
||||
}
|
||||
max_per_column.push(std::cmp::max(current_row_max, headers[head].len()));
|
||||
|
||||
max_per_column.push(std::cmp::max(current_col_max, headers[head].len()));
|
||||
}
|
||||
|
||||
// Different platforms want different amounts of buffer, not sure why
|
||||
@ -88,9 +98,9 @@ impl TableView {
|
||||
entries[row].truncate(max_num_of_columns);
|
||||
}
|
||||
|
||||
headers.push("...".to_string());
|
||||
headers.push("…".to_string());
|
||||
for row in 0..entries.len() {
|
||||
entries[row].push("...".to_string());
|
||||
entries[row].push(("…".to_string(), "c")); // ellipsis is centred
|
||||
}
|
||||
}
|
||||
|
||||
@ -167,19 +177,11 @@ impl TableView {
|
||||
if max_per_column[head] > max_naive_column_width {
|
||||
headers[head] = fill(&headers[head], max_column_width);
|
||||
for row in 0..entries.len() {
|
||||
entries[row][head] = fill(&entries[row][head], max_column_width);
|
||||
entries[row][head].0 = fill(&entries[row][head].0, max_column_width);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Paint the number column, if it exists
|
||||
if entries.len() > 1 {
|
||||
for row in 0..entries.len() {
|
||||
entries[row][0] =
|
||||
format!("{}", Color::Black.bold().paint(entries[row][0].to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
Some(TableView { headers, entries })
|
||||
}
|
||||
}
|
||||
@ -215,7 +217,7 @@ impl RenderView for TableView {
|
||||
table.set_titles(Row::new(header));
|
||||
|
||||
for row in &self.entries {
|
||||
table.add_row(Row::new(row.iter().map(|h| Cell::new(h)).collect()));
|
||||
table.add_row(Row::new(row.iter().map(|(v, s)| Cell::new(v).style_spec(s)).collect()));
|
||||
}
|
||||
|
||||
table.print_term(&mut *host.out_terminal()).unwrap();
|
||||
|
Reference in New Issue
Block a user