From 224b5e597699cd33d58ae3a2dd97866ddb5c251f Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Mon, 25 Aug 2025 22:05:36 -0300 Subject: [PATCH] feat: add support for percentage-based max_len values Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) --- src/config.rs | 14 +++++++------- src/modes/list.rs | 23 ++++++++++++++++++++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/config.rs b/src/config.rs index c7f55af..dc70967 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,7 +12,7 @@ pub struct ColumnConfig { #[serde(default)] pub align: ColumnAlignment, #[serde(default)] - pub max_len: Option, + pub max_len: Option, } #[derive(Debug, Clone, Serialize, Deserialize, Default)] @@ -35,7 +35,7 @@ impl<'de> serde::Deserialize<'de> for ColumnConfig { #[serde(default)] align: ColumnAlignment, #[serde(default)] - max_len: Option, + max_len: Option, } let helper = Helper::deserialize(deserializer)?; @@ -207,31 +207,31 @@ impl Settings { name: "id".to_string(), label: "id".to_string(), align: ColumnAlignment::Left, - max_len: Some(8), + max_len: Some("8".to_string()), }, ColumnConfig { name: "time".to_string(), label: "time".to_string(), align: ColumnAlignment::Left, - max_len: Some(19), + max_len: Some("19".to_string()), }, ColumnConfig { name: "size".to_string(), label: "size".to_string(), align: ColumnAlignment::Left, - max_len: Some(10), + max_len: Some("10".to_string()), }, ColumnConfig { name: "tags".to_string(), label: "tags".to_string(), align: ColumnAlignment::Left, - max_len: Some(20), + max_len: Some("20".to_string()), }, ColumnConfig { name: "meta:hostname".to_string(), label: "hostname".to_string(), align: ColumnAlignment::Left, - max_len: Some(15), + max_len: Some("15".to_string()), }, ]; } diff --git a/src/modes/list.rs b/src/modes/list.rs index 7ee6ad9..dfa20a1 100644 --- a/src/modes/list.rs +++ b/src/modes/list.rs @@ -9,6 +9,7 @@ use prettytable::{color, row, Attr, Cell, Row, Table}; use serde::{Deserialize, Serialize}; use serde_json; use serde_yaml; +use std::env; #[derive(Serialize, Deserialize)] struct ListItem { @@ -84,7 +85,27 @@ pub fn mode_list( .unwrap_or_else(|_| panic!("Unknown column {:?}", column.name)); let mut meta_name: Option<&str> = None; - let column_width = column.max_len.unwrap_or(0); + // Get terminal width, default to 80 if not available + let term_width = env::var("COLUMNS") + .ok() + .and_then(|w| w.parse::().ok()) + .unwrap_or(80); + + // Parse max_len, handling both numbers and percentages + let column_width = if let Some(max_len_str) = &column.max_len { + if max_len_str.ends_with('%') { + // Parse percentage + let percent = max_len_str.trim_end_matches('%') + .parse::() + .unwrap_or(0.0); + (term_width as f64 * percent / 100.0) as usize + } else { + // Parse absolute number + max_len_str.parse::().unwrap_or(0) + } + } else { + 0 + }; if let ColumnType::Meta = column_type { let parts: Vec<&str> = column.name.split(':').collect();