From 389e7d25022e9d07959373a6efc3db8d88f1f033 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Fri, 11 Oct 2024 13:36:09 -0500 Subject: [PATCH] make `FooterMode::Auto` work (#14063) # Description @Yethal discovered that `FooterMode::Auto` in the config as `$env.config.footer_mode = auto` did not work. This PR attempts to fix that problem by implementing the auto algorithm that was already supposed to work. # User-Facing Changes # Tests + Formatting # After Submitting --- Cargo.lock | 1 + crates/nu-protocol/src/config/table.rs | 2 +- crates/nu-table/Cargo.toml | 1 + crates/nu-table/src/common.rs | 20 ++++++++++++++++++-- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ced79abfdb..8d35cea895 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3493,6 +3493,7 @@ dependencies = [ "nu-utils", "once_cell", "tabled", + "terminal_size", ] [[package]] diff --git a/crates/nu-protocol/src/config/table.rs b/crates/nu-protocol/src/config/table.rs index c1c763c08e..7dbe1dd9d2 100644 --- a/crates/nu-protocol/src/config/table.rs +++ b/crates/nu-protocol/src/config/table.rs @@ -63,7 +63,7 @@ pub enum FooterMode { Always, /// Only show the footer if there are more than RowCount rows RowCount(u64), - /// Calculate the screen height, calculate row count, if display will be bigger than screen, add the footer + /// Calculate the screen height and row count, if screen height is larger than row count, don't show footer Auto, } diff --git a/crates/nu-table/Cargo.toml b/crates/nu-table/Cargo.toml index f9aeaca041..33554ae795 100644 --- a/crates/nu-table/Cargo.toml +++ b/crates/nu-table/Cargo.toml @@ -22,6 +22,7 @@ nu-ansi-term = { workspace = true } once_cell = { workspace = true } fancy-regex = { workspace = true } tabled = { workspace = true, features = ["ansi"], default-features = false } +terminal_size = { workspace = true } [dev-dependencies] # nu-test-support = { path="../nu-test-support", version = "0.98.1" } diff --git a/crates/nu-table/src/common.rs b/crates/nu-table/src/common.rs index b15874d7c8..3a793a690e 100644 --- a/crates/nu-table/src/common.rs +++ b/crates/nu-table/src/common.rs @@ -3,6 +3,7 @@ use crate::{ }; use nu_color_config::{Alignment, StyleComputer, TextStyle}; use nu_protocol::{Config, FooterMode, ShellError, Span, TableMode, TrimStrategy, Value}; +use terminal_size::{terminal_size, Height, Width}; pub type NuText = (String, TextStyle); pub type TableResult = Result, ShellError>; @@ -194,6 +195,21 @@ fn with_footer(config: &Config, with_header: bool, count_records: usize) -> bool } fn need_footer(config: &Config, count_records: u64) -> bool { - matches!(config.footer_mode, FooterMode::RowCount(limit) if count_records > limit) - || matches!(config.footer_mode, FooterMode::Always) + match config.footer_mode { + // Only show the footer if there are more than RowCount rows + FooterMode::RowCount(limit) => count_records > limit, + // Always show the footer + FooterMode::Always => true, + // Never show the footer + FooterMode::Never => false, + // Calculate the screen height and row count, if screen height is larger than row count, don't show footer + FooterMode::Auto => { + let (_width, height) = match terminal_size() { + Some((w, h)) => (Width(w.0).0 as u64, Height(h.0).0 as u64), + None => (Width(0).0 as u64, Height(0).0 as u64), + }; + + height <= count_records + } + } }