Ugh
This commit is contained in:
@@ -4,13 +4,13 @@
|
||||
/// formatting, filtering by tags, and support for different output formats
|
||||
/// including table, JSON, and YAML.
|
||||
use crate::config;
|
||||
use crate::modes::common::ColumnType;
|
||||
use crate::modes::common::{OutputFormat, format_size};
|
||||
use crate::services::item_service::ItemService;
|
||||
use crate::services::types::ItemWithMeta;
|
||||
use crate::modes::common::ColumnType;
|
||||
use crate::modes::common::{format_size, OutputFormat};
|
||||
use anyhow::{Result};
|
||||
use comfy_table::{Cell, Row, Color, Attribute};
|
||||
use anyhow::Result;
|
||||
use comfy_table::CellAlignment;
|
||||
use comfy_table::{Attribute, Cell, Color, Row};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
use serde_yaml;
|
||||
@@ -80,7 +80,7 @@ struct ListItem {
|
||||
fn apply_color(mut cell: Cell, color: &crate::config::TableColor, is_foreground: bool) -> Cell {
|
||||
use crate::config::TableColor::*;
|
||||
use comfy_table::Color;
|
||||
|
||||
|
||||
let comfy_color = match color {
|
||||
Black => Color::Black,
|
||||
Red => Color::Red,
|
||||
@@ -97,15 +97,19 @@ fn apply_color(mut cell: Cell, color: &crate::config::TableColor, is_foreground:
|
||||
DarkBlue => Color::DarkBlue,
|
||||
DarkMagenta => Color::DarkMagenta,
|
||||
DarkCyan => Color::DarkCyan,
|
||||
Rgb(r, g, b) => Color::Rgb { r: *r, g: *g, b: *b },
|
||||
Rgb(r, g, b) => Color::Rgb {
|
||||
r: *r,
|
||||
g: *g,
|
||||
b: *b,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
if is_foreground {
|
||||
cell = cell.fg(comfy_color);
|
||||
} else {
|
||||
cell = cell.bg(comfy_color);
|
||||
}
|
||||
|
||||
|
||||
cell
|
||||
}
|
||||
|
||||
@@ -125,7 +129,7 @@ fn apply_color(mut cell: Cell, color: &crate::config::TableColor, is_foreground:
|
||||
fn apply_attribute(mut cell: Cell, attribute: &crate::config::TableAttribute) -> Cell {
|
||||
use crate::config::TableAttribute::*;
|
||||
use comfy_table::Attribute;
|
||||
|
||||
|
||||
match attribute {
|
||||
Bold => cell = cell.add_attribute(Attribute::Bold),
|
||||
Dim => cell = cell.add_attribute(Attribute::Dim),
|
||||
@@ -137,7 +141,7 @@ fn apply_attribute(mut cell: Cell, attribute: &crate::config::TableAttribute) ->
|
||||
Hidden => cell = cell.add_attribute(Attribute::Hidden),
|
||||
CrossedOut => cell = cell.add_attribute(Attribute::CrossedOut),
|
||||
}
|
||||
|
||||
|
||||
cell
|
||||
}
|
||||
|
||||
@@ -161,8 +165,8 @@ fn apply_attribute(mut cell: Cell, attribute: &crate::config::TableAttribute) ->
|
||||
pub fn mode_list(
|
||||
cmd: &mut clap::Command,
|
||||
settings: &config::Settings,
|
||||
ids: &mut Vec<i64>,
|
||||
tags: &Vec<String>,
|
||||
ids: &mut [i64],
|
||||
tags: &[String],
|
||||
conn: &mut rusqlite::Connection,
|
||||
data_path: std::path::PathBuf,
|
||||
) -> Result<()> {
|
||||
@@ -203,7 +207,9 @@ pub fn mode_list(
|
||||
let mut table_row = Row::new();
|
||||
|
||||
for column in &settings.list_format {
|
||||
let column_type = ColumnType::from_str(&column.name)
|
||||
let column_type = column
|
||||
.name
|
||||
.parse::<ColumnType>()
|
||||
.unwrap_or_else(|_| panic!("Unknown column {:?}", column.name));
|
||||
|
||||
let mut meta_name: Option<&str> = None;
|
||||
@@ -217,7 +223,8 @@ pub fn mode_list(
|
||||
|
||||
let cell_content = match column_type {
|
||||
ColumnType::Id => item.id.unwrap_or(0).to_string(),
|
||||
ColumnType::Time => item.ts
|
||||
ColumnType::Time => item
|
||||
.ts
|
||||
.with_timezone(&chrono::Local)
|
||||
.format("%F %T")
|
||||
.to_string(),
|
||||
@@ -243,9 +250,10 @@ pub fn mode_list(
|
||||
None => "".to_string(),
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
// Truncate content to max 3 lines
|
||||
let mut cell_lines: Vec<String> = cell_content.split('\n').map(|s| s.to_string()).collect();
|
||||
let mut cell_lines: Vec<String> =
|
||||
cell_content.split('\n').map(|s| s.to_string()).collect();
|
||||
if cell_lines.len() > 3 {
|
||||
cell_lines.truncate(3);
|
||||
// Add ellipsis to the last line if we truncated
|
||||
@@ -257,47 +265,53 @@ pub fn mode_list(
|
||||
}
|
||||
}
|
||||
let truncated_content = cell_lines.join("\n");
|
||||
|
||||
|
||||
let mut cell = Cell::new(truncated_content);
|
||||
|
||||
|
||||
// Apply column-specific styling
|
||||
if let Some(fg_color) = &column.fg_color {
|
||||
cell = apply_color(cell, fg_color, true);
|
||||
}
|
||||
|
||||
|
||||
if let Some(bg_color) = &column.bg_color {
|
||||
cell = apply_color(cell, bg_color, false);
|
||||
}
|
||||
|
||||
|
||||
for attribute in &column.attributes {
|
||||
cell = apply_attribute(cell, attribute);
|
||||
}
|
||||
|
||||
|
||||
// Apply padding if specified
|
||||
if let Some((_left_padding, _right_padding)) = column.padding {
|
||||
// Note: comfy-table doesn't directly support padding, so we'd need to handle this
|
||||
// by adding spaces to the content, or use a different approach
|
||||
}
|
||||
|
||||
|
||||
// Apply styling for specific cases
|
||||
match column_type {
|
||||
ColumnType::Size => {
|
||||
if item.size.is_none() {
|
||||
if item_path.metadata().is_ok() {
|
||||
cell = cell.fg(comfy_table::Color::Yellow).add_attribute(Attribute::Bold);
|
||||
cell = cell
|
||||
.fg(comfy_table::Color::Yellow)
|
||||
.add_attribute(Attribute::Bold);
|
||||
} else {
|
||||
cell = cell.fg(comfy_table::Color::Red).add_attribute(Attribute::Bold);
|
||||
cell = cell
|
||||
.fg(comfy_table::Color::Red)
|
||||
.add_attribute(Attribute::Bold);
|
||||
}
|
||||
}
|
||||
}
|
||||
ColumnType::FileSize => {
|
||||
if item_path.metadata().is_err() {
|
||||
cell = cell.fg(comfy_table::Color::Red).add_attribute(Attribute::Bold);
|
||||
cell = cell
|
||||
.fg(comfy_table::Color::Red)
|
||||
.add_attribute(Attribute::Bold);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
|
||||
// Apply alignment
|
||||
cell = match column.align {
|
||||
crate::config::ColumnAlignment::Right => cell.set_alignment(CellAlignment::Right),
|
||||
@@ -309,7 +323,10 @@ pub fn mode_list(
|
||||
table.add_row(table_row);
|
||||
}
|
||||
|
||||
println!("{}", crate::modes::common::trim_lines_end(&table.trim_fmt()));
|
||||
println!(
|
||||
"{}",
|
||||
crate::modes::common::trim_lines_end(&table.trim_fmt())
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user