feat: add support for left/right alignment in list_format columns

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-16 13:42:12 -03:00
parent 21e8eb1d09
commit 65dd800526
2 changed files with 95 additions and 45 deletions

View File

@@ -9,6 +9,16 @@ use crate::args::{Args};
pub struct ColumnConfig { pub struct ColumnConfig {
pub name: String, pub name: String,
pub label: String, pub label: String,
#[serde(default)]
pub align: ColumnAlignment,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum ColumnAlignment {
#[default]
Left,
Right,
} }
impl<'de> serde::Deserialize<'de> for ColumnConfig { impl<'de> serde::Deserialize<'de> for ColumnConfig {
@@ -20,6 +30,8 @@ impl<'de> serde::Deserialize<'de> for ColumnConfig {
struct Helper { struct Helper {
name: String, name: String,
label: Option<String>, label: Option<String>,
#[serde(default)]
align: ColumnAlignment,
} }
let helper = Helper::deserialize(deserializer)?; let helper = Helper::deserialize(deserializer)?;
@@ -28,6 +40,7 @@ impl<'de> serde::Deserialize<'de> for ColumnConfig {
Ok(ColumnConfig { Ok(ColumnConfig {
name: helper.name, name: helper.name,
label, label,
align: helper.align,
}) })
} }
} }

View File

@@ -119,49 +119,81 @@ pub fn mode_list(
} }
let cell = match column_type { let cell = match column_type {
ColumnType::Id => Cell::new_align( ColumnType::Id => {
&string_column(item.id.unwrap_or(0).to_string(), column_width), let cell = Cell::new(&string_column(item.id.unwrap_or(0).to_string(), column_width));
Alignment::RIGHT, match column.align {
), crate::config::ColumnAlignment::Right => cell.align(Alignment::RIGHT),
ColumnType::Time => Cell::new(&string_column( crate::config::ColumnAlignment::Left => cell.align(Alignment::LEFT),
}
},
ColumnType::Time => {
let cell = Cell::new(&string_column(
item.ts item.ts
.with_timezone(&chrono::Local) .with_timezone(&chrono::Local)
.format("%F %T") .format("%F %T")
.to_string(), .to_string(),
column_width, column_width,
)), ));
ColumnType::Size => match item.size { match column.align {
Some(size) => Cell::new_align( crate::config::ColumnAlignment::Right => cell.align(Alignment::RIGHT),
&size_column(size as u64, settings.human_readable, column_width), crate::config::ColumnAlignment::Left => cell.align(Alignment::LEFT),
Alignment::RIGHT, }
), },
ColumnType::Size => {
let cell = match item.size {
Some(size) => Cell::new(&size_column(size as u64, settings.human_readable, column_width)),
None => match item_path.metadata() { None => match item_path.metadata() {
Ok(_) => Cell::new_align("Unknown", Alignment::RIGHT) Ok(_) => Cell::new("Unknown")
.with_style(Attr::ForegroundColor(color::YELLOW)) .with_style(Attr::ForegroundColor(color::YELLOW))
.with_style(Attr::Bold), .with_style(Attr::Bold),
Err(_) => Cell::new_align("Missing", Alignment::RIGHT) Err(_) => Cell::new("Missing")
.with_style(Attr::ForegroundColor(color::RED)) .with_style(Attr::ForegroundColor(color::RED))
.with_style(Attr::Bold), .with_style(Attr::Bold),
}, },
};
match column.align {
crate::config::ColumnAlignment::Right => cell.align(Alignment::RIGHT),
crate::config::ColumnAlignment::Left => cell.align(Alignment::LEFT),
}
}, },
ColumnType::Compression => { ColumnType::Compression => {
Cell::new(&string_column(item.compression.to_string(), column_width)) let cell = Cell::new(&string_column(item.compression.to_string(), column_width));
match column.align {
crate::config::ColumnAlignment::Right => cell.align(Alignment::RIGHT),
crate::config::ColumnAlignment::Left => cell.align(Alignment::LEFT),
}
}, },
ColumnType::FileSize => match item_path.metadata() { ColumnType::FileSize => {
Ok(metadata) => Cell::new_align( let cell = match item_path.metadata() {
&size_column(metadata.len(), settings.human_readable, column_width), Ok(metadata) => Cell::new(&size_column(metadata.len(), settings.human_readable, column_width)),
Alignment::RIGHT, Err(_) => Cell::new("Missing")
),
Err(_) => Cell::new_align("Missing", Alignment::RIGHT)
.with_style(Attr::ForegroundColor(color::RED)) .with_style(Attr::ForegroundColor(color::RED))
.with_style(Attr::Bold), .with_style(Attr::Bold),
};
match column.align {
crate::config::ColumnAlignment::Right => cell.align(Alignment::RIGHT),
crate::config::ColumnAlignment::Left => cell.align(Alignment::LEFT),
}
}, },
ColumnType::FilePath => Cell::new(&string_column( ColumnType::FilePath => {
let cell = Cell::new(&string_column(
item_path.clone().into_os_string().into_string().unwrap(), item_path.clone().into_os_string().into_string().unwrap(),
column_width, column_width,
)), ));
ColumnType::Tags => Cell::new(&string_column(tags.join(" "), column_width)), match column.align {
ColumnType::Meta => match meta_name { crate::config::ColumnAlignment::Right => cell.align(Alignment::RIGHT),
crate::config::ColumnAlignment::Left => cell.align(Alignment::LEFT),
}
},
ColumnType::Tags => {
let cell = Cell::new(&string_column(tags.join(" "), column_width));
match column.align {
crate::config::ColumnAlignment::Right => cell.align(Alignment::RIGHT),
crate::config::ColumnAlignment::Left => cell.align(Alignment::LEFT),
}
},
ColumnType::Meta => {
let cell = match meta_name {
Some(meta_name) => match meta.get(meta_name) { Some(meta_name) => match meta.get(meta_name) {
Some(meta_value) => { Some(meta_value) => {
Cell::new(&string_column(meta_value.to_string(), column_width)) Cell::new(&string_column(meta_value.to_string(), column_width))
@@ -169,6 +201,11 @@ pub fn mode_list(
None => Cell::new(""), None => Cell::new(""),
}, },
None => Cell::new(""), None => Cell::new(""),
};
match column.align {
crate::config::ColumnAlignment::Right => cell.align(Alignment::RIGHT),
crate::config::ColumnAlignment::Left => cell.align(Alignment::LEFT),
}
}, },
}; };
table_row.add_cell(cell); table_row.add_cell(cell);