refactor: Consolidate cell alignment logic in modes::list

Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-09-08 18:27:45 -03:00
parent eccdb0e13e
commit b4046e0b18

View File

@@ -91,182 +91,56 @@ pub fn mode_list(
} }
} }
let cell = match column_type { let mut cell = match column_type {
ColumnType::Id => { ColumnType::Id => Cell::new(item.id.unwrap_or(0).to_string()),
let mut cell = Cell::new(item.id.unwrap_or(0).to_string()); ColumnType::Time => Cell::new(
match column.align {
crate::config::ColumnAlignment::Right => {
cell = cell.set_alignment(CellAlignment::Right);
}
crate::config::ColumnAlignment::Left => {
cell = cell.set_alignment(CellAlignment::Left);
}
}
cell
}
ColumnType::Time => {
let mut cell = Cell::new(
item.ts item.ts
.with_timezone(&chrono::Local) .with_timezone(&chrono::Local)
.format("%F %T") .format("%F %T")
.to_string(), .to_string(),
); ),
match column.align {
crate::config::ColumnAlignment::Right => {
cell = cell.set_alignment(CellAlignment::Right);
}
crate::config::ColumnAlignment::Left => {
cell = cell.set_alignment(CellAlignment::Left);
}
}
cell
}
ColumnType::Size => match item.size { ColumnType::Size => match item.size {
Some(size) => { Some(size) => Cell::new(format_size(
let mut cell = Cell::new(format_size(
size as u64, size as u64,
settings.human_readable, settings.human_readable,
)); )),
match column.align { None => match item_path.metadata() {
crate::config::ColumnAlignment::Right => {
cell = cell.set_alignment(CellAlignment::Right);
}
crate::config::ColumnAlignment::Left => {
cell = cell.set_alignment(CellAlignment::Left);
}
}
cell
}
None => {
let mut cell = match item_path.metadata() {
Ok(_) => Cell::new("Unknown") Ok(_) => Cell::new("Unknown")
.fg(Color::Yellow) .fg(Color::Yellow)
.add_attribute(Attribute::Bold), .add_attribute(Attribute::Bold),
Err(_) => Cell::new("Missing") Err(_) => Cell::new("Missing")
.fg(Color::Red) .fg(Color::Red)
.add_attribute(Attribute::Bold), .add_attribute(Attribute::Bold),
};
match column.align {
crate::config::ColumnAlignment::Right => {
cell = cell.set_alignment(CellAlignment::Right);
}
crate::config::ColumnAlignment::Left => {
cell = cell.set_alignment(CellAlignment::Left);
}
}
cell
}
}, },
ColumnType::Compression => { },
let mut cell = Cell::new(item.compression.to_string()); ColumnType::Compression => Cell::new(item.compression.to_string()),
match column.align {
crate::config::ColumnAlignment::Right => {
cell = cell.set_alignment(CellAlignment::Right);
}
crate::config::ColumnAlignment::Left => {
cell = cell.set_alignment(CellAlignment::Left);
}
}
cell
}
ColumnType::FileSize => match item_path.metadata() { ColumnType::FileSize => match item_path.metadata() {
Ok(metadata) => { Ok(metadata) => Cell::new(format_size(
let mut cell = Cell::new(format_size(
metadata.len(), metadata.len(),
settings.human_readable, settings.human_readable,
)); )),
match column.align { Err(_) => Cell::new("Missing")
crate::config::ColumnAlignment::Right => {
cell = cell.set_alignment(CellAlignment::Right);
}
crate::config::ColumnAlignment::Left => {
cell = cell.set_alignment(CellAlignment::Left);
}
}
cell
}
Err(_) => {
let mut cell = Cell::new("Missing")
.fg(Color::Red) .fg(Color::Red)
.add_attribute(Attribute::Bold); .add_attribute(Attribute::Bold),
match column.align {
crate::config::ColumnAlignment::Right => {
cell = cell.set_alignment(CellAlignment::Right);
}
crate::config::ColumnAlignment::Left => {
cell = cell.set_alignment(CellAlignment::Left);
}
}
cell
}
}, },
ColumnType::FilePath => { ColumnType::FilePath => Cell::new(
let mut cell = Cell::new(
item_path.clone().into_os_string().into_string().unwrap(), item_path.clone().into_os_string().into_string().unwrap(),
); ),
match column.align { ColumnType::Tags => Cell::new(tags.join(" ")),
crate::config::ColumnAlignment::Right => {
cell = cell.set_alignment(CellAlignment::Right);
}
crate::config::ColumnAlignment::Left => {
cell = cell.set_alignment(CellAlignment::Left);
}
}
cell
}
ColumnType::Tags => {
let mut cell = Cell::new(tags.join(" "));
match column.align {
crate::config::ColumnAlignment::Right => {
cell = cell.set_alignment(CellAlignment::Right);
}
crate::config::ColumnAlignment::Left => {
cell = cell.set_alignment(CellAlignment::Left);
}
}
cell
}
ColumnType::Meta => match meta_name { ColumnType::Meta => 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(meta_value.to_string()),
let mut cell = Cell::new(meta_value.to_string()); None => Cell::new(""),
match column.align {
crate::config::ColumnAlignment::Right => {
cell = cell.set_alignment(CellAlignment::Right);
}
crate::config::ColumnAlignment::Left => {
cell = cell.set_alignment(CellAlignment::Left);
}
}
cell
}
None => {
let mut cell = Cell::new("");
match column.align {
crate::config::ColumnAlignment::Right => {
cell = cell.set_alignment(CellAlignment::Right);
}
crate::config::ColumnAlignment::Left => {
cell = cell.set_alignment(CellAlignment::Left);
}
}
cell
}
}, },
None => { None => Cell::new(""),
let mut cell = Cell::new("");
match column.align {
crate::config::ColumnAlignment::Right => {
cell = cell.set_alignment(CellAlignment::Right);
}
crate::config::ColumnAlignment::Left => {
cell = cell.set_alignment(CellAlignment::Left);
}
}
cell
}
}, },
}; };
// Apply alignment in one place
cell = match column.align {
crate::config::ColumnAlignment::Right => cell.set_alignment(CellAlignment::Right),
crate::config::ColumnAlignment::Left => cell.set_alignment(CellAlignment::Left),
};
table_row.add_cell(cell); table_row.add_cell(cell);
} }
table.add_row(table_row); table.add_row(table_row);