diff --git a/src/config.rs b/src/config.rs index 102183d..0d3677d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,6 +9,16 @@ use crate::args::{Args}; pub struct ColumnConfig { pub name: 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 { @@ -20,6 +30,8 @@ impl<'de> serde::Deserialize<'de> for ColumnConfig { struct Helper { name: String, label: Option, + #[serde(default)] + align: ColumnAlignment, } let helper = Helper::deserialize(deserializer)?; @@ -28,6 +40,7 @@ impl<'de> serde::Deserialize<'de> for ColumnConfig { Ok(ColumnConfig { name: helper.name, label, + align: helper.align, }) } } diff --git a/src/modes/list.rs b/src/modes/list.rs index fe2d6ec..59be6b0 100644 --- a/src/modes/list.rs +++ b/src/modes/list.rs @@ -119,56 +119,93 @@ pub fn mode_list( } let cell = match column_type { - ColumnType::Id => Cell::new_align( - &string_column(item.id.unwrap_or(0).to_string(), column_width), - Alignment::RIGHT, - ), - ColumnType::Time => Cell::new(&string_column( - item.ts - .with_timezone(&chrono::Local) - .format("%F %T") - .to_string(), - column_width, - )), - ColumnType::Size => match item.size { - Some(size) => Cell::new_align( - &size_column(size as u64, settings.human_readable, column_width), - Alignment::RIGHT, - ), - None => match item_path.metadata() { - Ok(_) => Cell::new_align("Unknown", Alignment::RIGHT) - .with_style(Attr::ForegroundColor(color::YELLOW)) - .with_style(Attr::Bold), - Err(_) => Cell::new_align("Missing", Alignment::RIGHT) - .with_style(Attr::ForegroundColor(color::RED)) - .with_style(Attr::Bold), - }, + ColumnType::Id => { + let cell = Cell::new(&string_column(item.id.unwrap_or(0).to_string(), column_width)); + match column.align { + crate::config::ColumnAlignment::Right => cell.align(Alignment::RIGHT), + crate::config::ColumnAlignment::Left => cell.align(Alignment::LEFT), + } + }, + ColumnType::Time => { + let cell = Cell::new(&string_column( + item.ts + .with_timezone(&chrono::Local) + .format("%F %T") + .to_string(), + column_width, + )); + match column.align { + crate::config::ColumnAlignment::Right => cell.align(Alignment::RIGHT), + crate::config::ColumnAlignment::Left => cell.align(Alignment::LEFT), + } + }, + 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() { + Ok(_) => Cell::new("Unknown") + .with_style(Attr::ForegroundColor(color::YELLOW)) + .with_style(Attr::Bold), + Err(_) => Cell::new("Missing") + .with_style(Attr::ForegroundColor(color::RED)) + .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 => { - 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() { - Ok(metadata) => Cell::new_align( - &size_column(metadata.len(), settings.human_readable, column_width), - Alignment::RIGHT, - ), - Err(_) => Cell::new_align("Missing", Alignment::RIGHT) - .with_style(Attr::ForegroundColor(color::RED)) - .with_style(Attr::Bold), + ColumnType::FileSize => { + let cell = match item_path.metadata() { + Ok(metadata) => Cell::new(&size_column(metadata.len(), settings.human_readable, column_width)), + Err(_) => Cell::new("Missing") + .with_style(Attr::ForegroundColor(color::RED)) + .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( - item_path.clone().into_os_string().into_string().unwrap(), - column_width, - )), - ColumnType::Tags => Cell::new(&string_column(tags.join(" "), column_width)), - ColumnType::Meta => match meta_name { - Some(meta_name) => match meta.get(meta_name) { - Some(meta_value) => { - Cell::new(&string_column(meta_value.to_string(), column_width)) - } + ColumnType::FilePath => { + let cell = Cell::new(&string_column( + item_path.clone().into_os_string().into_string().unwrap(), + column_width, + )); + match column.align { + 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_value) => { + Cell::new(&string_column(meta_value.to_string(), column_width)) + } + 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);