return string from draw_table instead of printing directly (#3088)

This commit is contained in:
rezural 2021-02-23 20:25:49 +11:00 committed by GitHub
parent 7f303a856e
commit 23d8dc959c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 17 deletions

View File

@ -238,7 +238,8 @@ async fn table(
if !input.is_empty() { if !input.is_empty() {
let t = from_list(&input, &configuration, start_number, &color_hm); let t = from_list(&input, &configuration, start_number, &color_hm);
draw_table(&t, term_width, &color_hm); let output = draw_table(&t, term_width, &color_hm);
println!("{}", output);
} }
start_number += input.len(); start_number += input.len();

View File

@ -631,7 +631,7 @@ impl WrappedTable {
&self, &self,
separator_position: SeparatorPosition, separator_position: SeparatorPosition,
color_hm: &HashMap<String, Style>, color_hm: &HashMap<String, Style>,
) { ) -> String {
let column_count = self.column_widths.len(); let column_count = self.column_widths.len();
let mut output = String::new(); let mut output = String::new();
let sep_color = color_hm let sep_color = color_hm
@ -773,16 +773,22 @@ impl WrappedTable {
} }
} }
} }
output.push('\n');
println!("{}", output); output
} }
fn print_cell_contents(&self, cells: &[WrappedCell], color_hm: &HashMap<String, Style>) { fn print_cell_contents(
&self,
cells: &[WrappedCell],
color_hm: &HashMap<String, Style>,
) -> String {
let sep_color = color_hm let sep_color = color_hm
.get("separator_color") .get("separator_color")
.unwrap_or(&Style::default()) .unwrap_or(&Style::default())
.to_owned(); .to_owned();
let mut total_output = String::new();
for current_line in 0.. { for current_line in 0.. {
let mut lines_printed = 0; let mut lines_printed = 0;
@ -859,30 +865,37 @@ impl WrappedTable {
if lines_printed == 0 { if lines_printed == 0 {
break; break;
} else { } else {
println!("{}", output); total_output.push_str(output.as_str());
total_output.push('\n');
} }
} }
total_output
} }
fn print_table(&self, color_hm: &HashMap<String, Style>) { fn print_table(&self, color_hm: &HashMap<String, Style>) -> String {
let mut output = String::new();
#[cfg(windows)] #[cfg(windows)]
{ {
let _ = nu_ansi_term::enable_ansi_support(); let _ = nu_ansi_term::enable_ansi_support();
} }
if self.data.is_empty() { if self.data.is_empty() {
return; return output;
} }
if self.theme.print_top_border { if self.theme.print_top_border {
self.print_separator(SeparatorPosition::Top, &color_hm); output.push_str(
self.print_separator(SeparatorPosition::Top, &color_hm)
.as_str(),
);
} }
let skip_headers = (self.headers.len() == 2 && self.headers[1].max_width == 0) let skip_headers = (self.headers.len() == 2 && self.headers[1].max_width == 0)
|| (self.headers.len() == 1 && self.headers[0].max_width == 0); || (self.headers.len() == 1 && self.headers[0].max_width == 0);
if !self.headers.is_empty() && !skip_headers { if !self.headers.is_empty() && !skip_headers {
self.print_cell_contents(&self.headers, &color_hm); output.push_str(self.print_cell_contents(&self.headers, &color_hm).as_str());
} }
let mut first_row = true; let mut first_row = true;
@ -890,22 +903,32 @@ impl WrappedTable {
for row in &self.data { for row in &self.data {
if !first_row { if !first_row {
if self.theme.separate_rows { if self.theme.separate_rows {
self.print_separator(SeparatorPosition::Middle, &color_hm); output.push_str(
self.print_separator(SeparatorPosition::Middle, &color_hm)
.as_str(),
)
} }
} else { } else {
first_row = false; first_row = false;
if self.theme.separate_header && !self.headers.is_empty() && !skip_headers { if self.theme.separate_header && !self.headers.is_empty() && !skip_headers {
self.print_separator(SeparatorPosition::Middle, &color_hm); output.push_str(
self.print_separator(SeparatorPosition::Middle, &color_hm)
.as_str(),
);
} }
} }
self.print_cell_contents(row, &color_hm); output.push_str(self.print_cell_contents(row, &color_hm).as_str());
} }
if self.theme.print_bottom_border { if self.theme.print_bottom_border {
self.print_separator(SeparatorPosition::Bottom, &color_hm); output.push_str(
self.print_separator(SeparatorPosition::Bottom, &color_hm)
.as_str(),
);
} }
output
} }
} }
@ -995,7 +1018,7 @@ pub fn maybe_truncate_columns(termwidth: usize, processed_table: &mut ProcessedT
} }
} }
pub fn draw_table(table: &Table, termwidth: usize, color_hm: &HashMap<String, Style>) { pub fn draw_table(table: &Table, termwidth: usize, color_hm: &HashMap<String, Style>) -> String {
// Remove the edges, if used // Remove the edges, if used
let termwidth = if table.theme.print_left_border && table.theme.print_right_border { let termwidth = if table.theme.print_left_border && table.theme.print_right_border {
termwidth - 2 termwidth - 2
@ -1018,7 +1041,7 @@ pub fn draw_table(table: &Table, termwidth: usize, color_hm: &HashMap<String, St
if !table.data.is_empty() && !table.data[0].is_empty() { if !table.data.is_empty() && !table.data[0].is_empty() {
table.data[0].len() table.data[0].len()
} else { } else {
return; return String::new();
} }
} else { } else {
headers_len headers_len
@ -1055,7 +1078,7 @@ pub fn draw_table(table: &Table, termwidth: usize, color_hm: &HashMap<String, St
&re_trailing, &re_trailing,
); );
wrapped_table.print_table(&color_hm); wrapped_table.print_table(&color_hm)
} }
fn wrap_cells( fn wrap_cells(