refactor: move list mode handling to modes/list.rs

This commit is contained in:
Andrew Phillips
2025-05-10 14:11:37 -03:00
committed by Andrew Phillips (aider)
parent 5c76d4c15c
commit d9defdec45
3 changed files with 17 additions and 179 deletions

View File

@@ -3,11 +3,11 @@ use crate::Alignment;
use crate::db::{get_items, get_items_matching};
use crate::modes::common::ColumnType;
use crate::modes::common::{size_column, string_column};
use anyhow::anyhow;
use log::debug;
use prettytable::color;
use prettytable::row;
use prettytable::{Attr, Cell, Row, Table};
use log::debug;
use anyhow::anyhow;
pub fn mode_list(
cmd: &mut clap::Command,
@@ -38,8 +38,12 @@ pub fn mode_list(
debug!("MAIN: Items: {:?}", items);
let mut tags_by_item: std::collections::HashMap<i64, Vec<String>> = std::collections::HashMap::new();
let mut meta_by_item: std::collections::HashMap<i64, std::collections::HashMap<String, String>> = std::collections::HashMap::new();
let mut tags_by_item: std::collections::HashMap<i64, Vec<String>> =
std::collections::HashMap::new();
let mut meta_by_item: std::collections::HashMap<
i64,
std::collections::HashMap<String, String>,
> = std::collections::HashMap::new();
for item in items.iter() {
let item_id = item.id.unwrap();
@@ -50,7 +54,8 @@ pub fn mode_list(
.collect();
tags_by_item.insert(item_id, item_tags);
let mut item_meta: std::collections::HashMap<String, String> = std::collections::HashMap::new();
let mut item_meta: std::collections::HashMap<String, String> =
std::collections::HashMap::new();
for meta in crate::db::get_item_meta(conn, item)? {
item_meta.insert(meta.name.clone(), meta.value);
@@ -68,9 +73,8 @@ pub fn mode_list(
for column in list_format.clone() {
let mut column_format = column.split(":").into_iter();
let column_name = column_format.next().expect("Unable to parse column name");
let column_type = ColumnType::from_str(column_name).map_err(|_| {
anyhow!("Unknown column {:?}", column_name)
})?;
let column_type = ColumnType::from_str(column_name)
.map_err(|_| anyhow!("Unknown column {:?}", column_name))?;
if column_type == ColumnType::Meta {
let meta_name = column_format
@@ -115,7 +119,10 @@ pub fn mode_list(
Alignment::RIGHT,
),
ColumnType::Time => Cell::new(&string_column(
item.ts.with_timezone(&chrono::Local).format("%F %T").to_string(),
item.ts
.with_timezone(&chrono::Local)
.format("%F %T")
.to_string(),
column_width,
)),
ColumnType::Size => match item.size {