From 20b53067cd3f8da2af930370659172e11db81161 Mon Sep 17 00:00:00 2001 From: Embers-of-the-Fire Date: Sat, 3 Aug 2024 16:55:35 +0800 Subject: [PATCH] Fix overflow table display in command documentation (#13526) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Check and set table width beforehand. Closes #13520. # User-Facing Changes Before: ```plain ❯ help net cmd1 network Usage: > net cmd1 Flags: -h, --help - Display the help message for this command Input/output types: ╭───┬─────────┬─────────────────────────────────────────────────────────╮ │ # │ input │ output │ ├───┼─────────┼─────────────────────────────────────────────────────────┤ │ 0 │ nothing │ table, │ │ │ │ flags: record> │ ╰───┴─────────┴─────────────────────────────────────────────────────────╯ ``` After: ```plain ❯ help net cmd1 network Usage: > net cmd1 Flags: -h, --help - Display the help message for this command Input/output types: ╭───┬─────────┬───────────────────────────────────────────────────────╮ │ # │ input │ output │ ├───┼─────────┼───────────────────────────────────────────────────────┤ │ 0 │ nothing │ table, │ │ │ │ flags: record> │ ╰───┴─────────┴───────────────────────────────────────────────────────╯ ``` # Tests + Formatting - [x] `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - [x] `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - [x] `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the tests for the standard library # After Submitting - [x] Bug fix, no doc update. --- Cargo.lock | 1 + crates/nu-engine/Cargo.toml | 3 ++- crates/nu-engine/src/documentation.rs | 35 ++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1c0fd9a954..f4495d6bfa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3157,6 +3157,7 @@ dependencies = [ "nu-path", "nu-protocol", "nu-utils", + "terminal_size", ] [[package]] diff --git a/crates/nu-engine/Cargo.toml b/crates/nu-engine/Cargo.toml index be544265fe..2324eb5676 100644 --- a/crates/nu-engine/Cargo.toml +++ b/crates/nu-engine/Cargo.toml @@ -16,6 +16,7 @@ nu-path = { path = "../nu-path", version = "0.96.2" } nu-glob = { path = "../nu-glob", version = "0.96.2" } nu-utils = { path = "../nu-utils", version = "0.96.2" } log = { workspace = true } +terminal_size = { workspace = true } [features] -plugin = [] \ No newline at end of file +plugin = [] diff --git a/crates/nu-engine/src/documentation.rs b/crates/nu-engine/src/documentation.rs index a0dee3d343..0a7d742d57 100644 --- a/crates/nu-engine/src/documentation.rs +++ b/crates/nu-engine/src/documentation.rs @@ -7,6 +7,7 @@ use nu_protocol::{ Spanned, SyntaxShape, Type, Value, }; use std::{collections::HashMap, fmt::Write}; +use terminal_size::{Height, Width}; pub fn get_full_help( command: &dyn Command, @@ -234,6 +235,14 @@ fn get_documentation( } } + fn get_term_width() -> usize { + if let Some((Width(w), Height(_))) = terminal_size::terminal_size() { + w as usize + } else { + 80 + } + } + if !is_parser_keyword && !sig.input_output_types.is_empty() { if let Some(decl_id) = engine_state.find_decl(b"table", &[]) { // FIXME: we may want to make this the span of the help command in the future @@ -256,7 +265,18 @@ fn get_documentation( &Call { decl_id, head: span, - arguments: vec![], + arguments: vec![Argument::Named(( + Spanned { + item: "width".to_string(), + span: Span::unknown(), + }, + None, + Some(Expression::new_unknown( + Expr::Int(get_term_width() as i64 - 2), // padding, see below + Span::unknown(), + Type::Int, + )), + ))], parser_info: HashMap::new(), }, PipelineData::Value(Value::list(vals, span), None), @@ -334,6 +354,19 @@ fn get_documentation( None, )) } + table_call.add_named(( + Spanned { + item: "expand".to_string(), + span: Span::unknown(), + }, + None, + Some(Expression::new_unknown( + Expr::Int(get_term_width() as i64 - 2), + Span::unknown(), + Type::Int, + )), + )); + let table = engine_state .find_decl("table".as_bytes(), &[]) .and_then(|decl_id| {