add optional footer to table (#392)

* add optional footer to table

* missed a draw_table
This commit is contained in:
Darren Schroeder
2021-12-01 13:20:23 -06:00
committed by GitHub
parent d2a1564b94
commit d8c721282b
7 changed files with 89 additions and 12 deletions

View File

@ -14,6 +14,7 @@ path = "src/main.rs"
[dependencies]
# nu-ansi-term = "0.39.0"
nu-ansi-term = { path = "../nu-ansi-term" }
nu-protocol = { path = "../nu-protocol"}
regex = "1.4"
unicode-width = "0.1.8"
strip-ansi-escapes = "0.1.1"

View File

@ -1,3 +1,4 @@
use nu_protocol::Config;
use nu_table::{draw_table, StyledString, Table, TextStyle, Theme};
use std::collections::HashMap;
@ -25,8 +26,10 @@ fn main() {
let table = Table::new(headers, vec![rows; 3], Theme::rounded());
// FIXME: Config isn't available from here so just put these here to compile
let color_hm: HashMap<String, nu_ansi_term::Style> = HashMap::new();
// get the default config
let config = Config::default();
// Capture the table as a string
let output_table = draw_table(&table, width, &color_hm);
let output_table = draw_table(&table, width, &color_hm, &config);
// Draw the table
println!("{}", output_table)
}

View File

@ -1,5 +1,6 @@
use crate::wrap::{column_width, split_sublines, wrap, Alignment, Subline, WrappedCell};
use nu_ansi_term::{Color, Style};
use nu_protocol::{Config, FooterMode};
use std::collections::HashMap;
use std::fmt::Write;
@ -629,6 +630,7 @@ pub struct WrappedTable {
pub headers: Vec<WrappedCell>,
pub data: Vec<Vec<WrappedCell>>,
pub theme: Theme,
pub footer: Vec<WrappedCell>,
}
impl WrappedTable {
@ -878,7 +880,7 @@ impl WrappedTable {
total_output
}
fn print_table(&self, color_hm: &HashMap<String, Style>) -> String {
fn print_table(&self, color_hm: &HashMap<String, Style>, config: &Config) -> String {
let mut output = String::new();
#[cfg(windows)]
@ -890,10 +892,12 @@ impl WrappedTable {
return output;
}
// The top border
if self.theme.print_top_border {
output.push_str(&self.print_separator(SeparatorPosition::Top, color_hm));
}
// The header
let skip_headers = (self.headers.len() == 2 && self.headers[1].max_width == 0)
|| (self.headers.len() == 1 && self.headers[0].max_width == 0);
@ -901,8 +905,8 @@ impl WrappedTable {
output.push_str(&self.print_cell_contents(&self.headers, color_hm));
}
// The middle section
let mut first_row = true;
for row in &self.data {
if !first_row {
if self.theme.separate_rows {
@ -919,6 +923,31 @@ impl WrappedTable {
output.push_str(&self.print_cell_contents(row, color_hm));
}
match config.footer_mode {
FooterMode::Always => {
if self.theme.separate_header && !self.headers.is_empty() && !skip_headers {
output.push_str(&self.print_separator(SeparatorPosition::Middle, color_hm));
}
if !self.headers.is_empty() && !skip_headers {
output.push_str(&self.print_cell_contents(&self.footer, color_hm));
}
}
FooterMode::RowCount(r) => {
if self.data.len() as u64 > r {
if self.theme.separate_header && !self.headers.is_empty() && !skip_headers {
output.push_str(&self.print_separator(SeparatorPosition::Middle, color_hm));
}
if !self.headers.is_empty() && !skip_headers {
output.push_str(&self.print_cell_contents(&self.footer, color_hm));
}
}
}
_ => {} // Never and Auto aka auto get eaten and nothing happens
}
// The table finish
if self.theme.print_bottom_border {
output.push_str(&self.print_separator(SeparatorPosition::Bottom, color_hm));
}
@ -1013,7 +1042,12 @@ pub fn maybe_truncate_columns(termwidth: usize, processed_table: &mut ProcessedT
}
}
pub fn draw_table(table: &Table, termwidth: usize, color_hm: &HashMap<String, Style>) -> String {
pub fn draw_table(
table: &Table,
termwidth: usize,
color_hm: &HashMap<String, Style>,
config: &Config,
) -> String {
// Remove the edges, if used
let termwidth = if table.theme.print_left_border && table.theme.print_right_border {
termwidth - 2
@ -1073,7 +1107,7 @@ pub fn draw_table(table: &Table, termwidth: usize, color_hm: &HashMap<String, St
&re_trailing,
);
wrapped_table.print_table(color_hm)
wrapped_table.print_table(color_hm, config)
}
fn wrap_cells(
@ -1151,11 +1185,24 @@ fn wrap_cells(
output_data.push(output_row);
}
let mut footer = vec![
WrappedCell {
lines: vec![],
max_width: 0,
style: TextStyle {
..Default::default()
},
};
output_headers.len()
];
footer.clone_from_slice(&output_headers[..]);
WrappedTable {
column_widths,
headers: output_headers,
data: output_data,
theme: processed_table.theme,
footer,
}
}

View File

@ -24,13 +24,13 @@ pub struct Line {
pub width: usize,
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct WrappedLine {
pub line: String,
pub width: usize,
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct WrappedCell {
pub lines: Vec<WrappedLine>,
pub max_width: usize,