feat[table]: Allow specific table width with -w, like command grid. (#5643)

This commit is contained in:
Vanilla 2022-05-26 19:53:05 +08:00 committed by GitHub
parent 3d62528d8c
commit 727ff5f2d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 16 deletions

View file

@ -18,6 +18,16 @@ use terminal_size::{Height, Width};
const STREAM_PAGE_SIZE: usize = 1000; const STREAM_PAGE_SIZE: usize = 1000;
const STREAM_TIMEOUT_CHECK_INTERVAL: usize = 100; const STREAM_TIMEOUT_CHECK_INTERVAL: usize = 100;
fn get_width_param(width_param: Option<i64>) -> usize {
if let Some(col) = width_param {
col as usize
} else if let Some((Width(w), Height(_h))) = terminal_size::terminal_size() {
(w - 1) as usize
} else {
80usize
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct Table; pub struct Table;
@ -44,6 +54,12 @@ impl Command for Table {
Some('n'), Some('n'),
) )
.switch("list", "list available table modes/themes", Some('l')) .switch("list", "list available table modes/themes", Some('l'))
.named(
"width",
SyntaxShape::Int,
"number of terminal columns wide (not output columns)",
Some('w'),
)
.category(Category::Viewers) .category(Category::Viewers)
} }
@ -62,11 +78,8 @@ impl Command for Table {
let row_offset = start_num.unwrap_or_default() as usize; let row_offset = start_num.unwrap_or_default() as usize;
let list: bool = call.has_flag("list"); let list: bool = call.has_flag("list");
let term_width = if let Some((Width(w), Height(_h))) = terminal_size::terminal_size() { let width_param: Option<i64> = call.get_flag(engine_state, stack, "width")?;
(w - 1) as usize let term_width = get_width_param(width_param);
} else {
80usize
};
if list { if list {
let table_modes = vec![ let table_modes = vec![
@ -222,7 +235,7 @@ impl Command for Table {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
fn handle_row_stream( fn handle_row_stream(
engine_state: &EngineState, engine_state: &EngineState,
stack: &Stack, stack: &mut Stack,
stream: ListStream, stream: ListStream,
call: &Call, call: &Call,
row_offset: usize, row_offset: usize,
@ -306,6 +319,7 @@ fn handle_row_stream(
}; };
let head = call.head; let head = call.head;
let width_param: Option<i64> = call.get_flag(engine_state, stack, "width")?;
Ok(PipelineData::ExternalStream { Ok(PipelineData::ExternalStream {
stdout: Some(RawStream::new( stdout: Some(RawStream::new(
@ -315,6 +329,7 @@ fn handle_row_stream(
ctrlc: ctrlc.clone(), ctrlc: ctrlc.clone(),
head, head,
stream, stream,
width_param,
}), }),
ctrlc, ctrlc,
head, head,
@ -469,6 +484,7 @@ struct PagingTableCreator {
ctrlc: Option<Arc<AtomicBool>>, ctrlc: Option<Arc<AtomicBool>>,
config: Config, config: Config,
row_offset: usize, row_offset: usize,
width_param: Option<i64>,
} }
impl Iterator for PagingTableCreator { impl Iterator for PagingTableCreator {
@ -507,12 +523,7 @@ impl Iterator for PagingTableCreator {
} }
let color_hm = get_color_config(&self.config); let color_hm = get_color_config(&self.config);
let term_width = get_width_param(self.width_param);
let term_width = if let Some((Width(w), Height(_h))) = terminal_size::terminal_size() {
(w - 1) as usize
} else {
80usize
};
let table = convert_to_table( let table = convert_to_table(
self.row_offset, self.row_offset,

View file

@ -491,14 +491,20 @@ pub fn draw_table(
config: &Config, config: &Config,
) -> String { ) -> 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 edges_width = if table.theme.print_left_border && table.theme.print_right_border {
termwidth - 3 3
} else if table.theme.print_left_border || table.theme.print_right_border { } else if table.theme.print_left_border || table.theme.print_right_border {
termwidth - 1 1
} else { } else {
termwidth 0
}; };
if termwidth < edges_width {
return format!("Couldn't fit table into {} columns!", termwidth);
}
let termwidth = termwidth - edges_width;
let mut processed_table = process_table(table); let mut processed_table = process_table(table);
let max_per_column = get_max_column_widths(&processed_table); let max_per_column = get_max_column_widths(&processed_table);