feat: add support for percentage-based max_len values
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -12,7 +12,7 @@ pub struct ColumnConfig {
|
||||
#[serde(default)]
|
||||
pub align: ColumnAlignment,
|
||||
#[serde(default)]
|
||||
pub max_len: Option<usize>,
|
||||
pub max_len: Option<String>,
|
||||
}
|
||||
|
||||
#[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<usize>,
|
||||
max_len: Option<String>,
|
||||
}
|
||||
|
||||
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()),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
@@ -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::<usize>().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::<f64>()
|
||||
.unwrap_or(0.0);
|
||||
(term_width as f64 * percent / 100.0) as usize
|
||||
} else {
|
||||
// Parse absolute number
|
||||
max_len_str.parse::<usize>().unwrap_or(0)
|
||||
}
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
if let ColumnType::Meta = column_type {
|
||||
let parts: Vec<&str> = column.name.split(':').collect();
|
||||
|
||||
Reference in New Issue
Block a user