From b4046e0b18a4b56f9879d12cfd180e18a56bf68c Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Mon, 8 Sep 2025 18:27:45 -0300 Subject: [PATCH] refactor: Consolidate cell alignment logic in `modes::list` Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) --- src/modes/list.rs | 210 ++++++++++------------------------------------ 1 file changed, 42 insertions(+), 168 deletions(-) diff --git a/src/modes/list.rs b/src/modes/list.rs index b16db5e..f246880 100644 --- a/src/modes/list.rs +++ b/src/modes/list.rs @@ -91,182 +91,56 @@ pub fn mode_list( } } - let cell = match column_type { - ColumnType::Id => { - let mut cell = Cell::new(item.id.unwrap_or(0).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::Time => { - let mut cell = Cell::new( - item.ts - .with_timezone(&chrono::Local) - .format("%F %T") - .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 - } + let mut cell = match column_type { + ColumnType::Id => Cell::new(item.id.unwrap_or(0).to_string()), + ColumnType::Time => Cell::new( + item.ts + .with_timezone(&chrono::Local) + .format("%F %T") + .to_string(), + ), ColumnType::Size => match item.size { - Some(size) => { - let mut cell = Cell::new(format_size( - size as u64, - settings.human_readable, - )); - 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 = match item_path.metadata() { - Ok(_) => Cell::new("Unknown") - .fg(Color::Yellow) - .add_attribute(Attribute::Bold), - Err(_) => Cell::new("Missing") - .fg(Color::Red) - .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()); - 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() { - Ok(metadata) => { - let mut cell = Cell::new(format_size( - metadata.len(), - settings.human_readable, - )); - match column.align { - 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") + Some(size) => Cell::new(format_size( + size as u64, + settings.human_readable, + )), + None => match item_path.metadata() { + Ok(_) => Cell::new("Unknown") + .fg(Color::Yellow) + .add_attribute(Attribute::Bold), + Err(_) => Cell::new("Missing") .fg(Color::Red) - .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 - } + .add_attribute(Attribute::Bold), + }, }, - ColumnType::FilePath => { - let mut cell = Cell::new( - item_path.clone().into_os_string().into_string().unwrap(), - ); - 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::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::Compression => Cell::new(item.compression.to_string()), + ColumnType::FileSize => match item_path.metadata() { + Ok(metadata) => Cell::new(format_size( + metadata.len(), + settings.human_readable, + )), + Err(_) => Cell::new("Missing") + .fg(Color::Red) + .add_attribute(Attribute::Bold), + }, + ColumnType::FilePath => Cell::new( + item_path.clone().into_os_string().into_string().unwrap(), + ), + ColumnType::Tags => Cell::new(tags.join(" ")), ColumnType::Meta => match meta_name { Some(meta_name) => match meta.get(meta_name) { - Some(meta_value) => { - let mut cell = Cell::new(meta_value.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 - } - 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 - } + Some(meta_value) => Cell::new(meta_value.to_string()), + None => Cell::new(""), }, - 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 => Cell::new(""), }, }; + + // 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.add_row(table_row);