feat: implement core services and refactor modes
Co-authored-by: aider (openai/andrew/openrouter/google/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
@@ -1,16 +1,15 @@
|
||||
use crate::db::{get_items, get_items_matching};
|
||||
use crate::config;
|
||||
use crate::core::item_service::ItemService;
|
||||
use crate::core::types::ItemWithMeta;
|
||||
use crate::modes::common::ColumnType;
|
||||
use crate::modes::common::{size_column, string_column, OutputFormat};
|
||||
use crate::config;
|
||||
use anyhow::{anyhow, Result};
|
||||
use prettytable::format::Alignment;
|
||||
use prettytable::{color, row, Attr, Cell, Row, Table};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
use serde_yaml;
|
||||
use anyhow::anyhow;
|
||||
use log::debug;
|
||||
use prettytable::color;
|
||||
use prettytable::row;
|
||||
use prettytable::format::Alignment;
|
||||
use prettytable::{Attr, Cell, Row, Table};
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct ListItem {
|
||||
@@ -33,7 +32,7 @@ pub fn mode_list(
|
||||
tags: &Vec<String>,
|
||||
conn: &mut rusqlite::Connection,
|
||||
data_path: std::path::PathBuf,
|
||||
) -> anyhow::Result<()> {
|
||||
) -> Result<()> {
|
||||
if !ids.is_empty() {
|
||||
cmd.error(
|
||||
clap::error::ErrorKind::InvalidValue,
|
||||
@@ -51,34 +50,13 @@ pub fn mode_list(
|
||||
}
|
||||
}
|
||||
|
||||
let items = match tags.is_empty() && meta.is_empty() {
|
||||
true => get_items(conn)?,
|
||||
false => get_items_matching(conn, tags, &meta)?,
|
||||
};
|
||||
|
||||
debug!("MAIN: Items: {:?}", items);
|
||||
|
||||
// Collect all item IDs for batch queries
|
||||
let item_ids: Vec<i64> = items.iter().map(|item| item.id.unwrap()).collect();
|
||||
|
||||
// Fetch all tags for all items in a single query
|
||||
let all_tags = crate::db::get_tags_for_items(conn, &item_ids)?;
|
||||
let mut tags_by_item: std::collections::HashMap<i64, Vec<String>> =
|
||||
std::collections::HashMap::new();
|
||||
|
||||
// Convert Tag structs to just names
|
||||
for (item_id, tags) in all_tags {
|
||||
let tag_names: Vec<String> = tags.into_iter().map(|tag| tag.name).collect();
|
||||
tags_by_item.insert(item_id, tag_names);
|
||||
}
|
||||
|
||||
// Fetch all metadata for all items in a single query
|
||||
let meta_by_item = crate::db::get_meta_for_items(conn, &item_ids)?;
|
||||
let item_service = ItemService::new(data_path.clone());
|
||||
let items_with_meta = item_service.list_items(conn, tags, &meta)?;
|
||||
|
||||
let output_format = crate::modes::common::settings_output_format(settings);
|
||||
|
||||
if output_format != OutputFormat::Table {
|
||||
return show_list_structured(items, tags_by_item, meta_by_item, data_path, settings, output_format);
|
||||
return show_list_structured(items_with_meta, data_path, settings, output_format);
|
||||
}
|
||||
|
||||
let mut table = Table::new();
|
||||
@@ -87,18 +65,16 @@ pub fn mode_list(
|
||||
let mut title_row = row!();
|
||||
|
||||
for column in &settings.list_format {
|
||||
let _column_type = ColumnType::from_str(&column.name)
|
||||
.map_err(|_| anyhow!("Unknown column {:?}", column.name))?;
|
||||
|
||||
title_row.add_cell(Cell::new(&column.label).with_style(Attr::Bold));
|
||||
}
|
||||
|
||||
table.set_titles(title_row);
|
||||
|
||||
for item in items {
|
||||
let item_id = item.id.unwrap();
|
||||
let tags = tags_by_item.get(&item_id).unwrap();
|
||||
let meta = meta_by_item.get(&item_id).unwrap();
|
||||
for item_with_meta in items_with_meta {
|
||||
let item = item_with_meta.item;
|
||||
let tags: Vec<String> = item_with_meta.tags.into_iter().map(|t| t.name).collect();
|
||||
let meta = item_with_meta.meta_as_map();
|
||||
|
||||
let mut item_path = data_path.clone();
|
||||
item_path.push(item.id.unwrap().to_string());
|
||||
|
||||
@@ -107,11 +83,11 @@ pub fn mode_list(
|
||||
for column in &settings.list_format {
|
||||
let column_type = ColumnType::from_str(&column.name)
|
||||
.unwrap_or_else(|_| panic!("Unknown column {:?}", column.name));
|
||||
|
||||
|
||||
let mut meta_name: Option<&str> = None;
|
||||
let column_width = 0; // We're not supporting width in the new format
|
||||
|
||||
if column_type == ColumnType::Meta {
|
||||
if let ColumnType::Meta = column_type {
|
||||
let parts: Vec<&str> = column.name.split(':').collect();
|
||||
if parts.len() > 1 {
|
||||
meta_name = Some(parts[1]);
|
||||
@@ -120,13 +96,18 @@ pub fn mode_list(
|
||||
|
||||
let cell = match column_type {
|
||||
ColumnType::Id => {
|
||||
let mut cell = Cell::new(&string_column(item.id.unwrap_or(0).to_string(), column_width));
|
||||
let mut 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); },
|
||||
crate::config::ColumnAlignment::Right => {
|
||||
cell.align(Alignment::RIGHT);
|
||||
}
|
||||
crate::config::ColumnAlignment::Left => {
|
||||
cell.align(Alignment::LEFT);
|
||||
}
|
||||
}
|
||||
cell
|
||||
},
|
||||
}
|
||||
ColumnType::Time => {
|
||||
let mut cell = Cell::new(&string_column(
|
||||
item.ts
|
||||
@@ -136,69 +117,96 @@ pub fn mode_list(
|
||||
column_width,
|
||||
));
|
||||
match column.align {
|
||||
crate::config::ColumnAlignment::Right => { cell.align(Alignment::RIGHT); },
|
||||
crate::config::ColumnAlignment::Left => { cell.align(Alignment::LEFT); },
|
||||
crate::config::ColumnAlignment::Right => {
|
||||
cell.align(Alignment::RIGHT);
|
||||
}
|
||||
crate::config::ColumnAlignment::Left => {
|
||||
cell.align(Alignment::LEFT);
|
||||
}
|
||||
}
|
||||
cell
|
||||
},
|
||||
ColumnType::Size => {
|
||||
let cell = match item.size {
|
||||
Some(size) => {
|
||||
let mut cell = Cell::new(&size_column(size as u64, settings.human_readable, column_width));
|
||||
match column.align {
|
||||
crate::config::ColumnAlignment::Right => { cell.align(Alignment::RIGHT); },
|
||||
crate::config::ColumnAlignment::Left => { cell.align(Alignment::LEFT); },
|
||||
}
|
||||
ColumnType::Size => match item.size {
|
||||
Some(size) => {
|
||||
let mut cell = Cell::new(&size_column(
|
||||
size as u64,
|
||||
settings.human_readable,
|
||||
column_width,
|
||||
));
|
||||
match column.align {
|
||||
crate::config::ColumnAlignment::Right => {
|
||||
cell.align(Alignment::RIGHT);
|
||||
}
|
||||
cell
|
||||
},
|
||||
None => {
|
||||
let mut cell = 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); },
|
||||
crate::config::ColumnAlignment::Left => {
|
||||
cell.align(Alignment::LEFT);
|
||||
}
|
||||
cell
|
||||
},
|
||||
};
|
||||
cell
|
||||
}
|
||||
cell
|
||||
}
|
||||
None => {
|
||||
let mut cell = 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);
|
||||
}
|
||||
}
|
||||
cell
|
||||
}
|
||||
},
|
||||
ColumnType::Compression => {
|
||||
let mut cell = Cell::new(&string_column(item.compression.to_string(), column_width));
|
||||
let mut 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); },
|
||||
crate::config::ColumnAlignment::Right => {
|
||||
cell.align(Alignment::RIGHT);
|
||||
}
|
||||
crate::config::ColumnAlignment::Left => {
|
||||
cell.align(Alignment::LEFT);
|
||||
}
|
||||
}
|
||||
cell
|
||||
},
|
||||
ColumnType::FileSize => {
|
||||
let cell = match item_path.metadata() {
|
||||
Ok(metadata) => {
|
||||
let mut cell = Cell::new(&size_column(metadata.len(), settings.human_readable, 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) => {
|
||||
let mut cell = Cell::new(&size_column(
|
||||
metadata.len(),
|
||||
settings.human_readable,
|
||||
column_width,
|
||||
));
|
||||
match column.align {
|
||||
crate::config::ColumnAlignment::Right => {
|
||||
cell.align(Alignment::RIGHT);
|
||||
}
|
||||
cell
|
||||
},
|
||||
Err(_) => {
|
||||
let mut cell = 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); },
|
||||
crate::config::ColumnAlignment::Left => {
|
||||
cell.align(Alignment::LEFT);
|
||||
}
|
||||
cell
|
||||
},
|
||||
};
|
||||
cell
|
||||
}
|
||||
cell
|
||||
}
|
||||
Err(_) => {
|
||||
let mut cell = 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);
|
||||
}
|
||||
}
|
||||
cell
|
||||
}
|
||||
},
|
||||
ColumnType::FilePath => {
|
||||
let mut cell = Cell::new(&string_column(
|
||||
@@ -206,49 +214,67 @@ pub fn mode_list(
|
||||
column_width,
|
||||
));
|
||||
match column.align {
|
||||
crate::config::ColumnAlignment::Right => { cell.align(Alignment::RIGHT); },
|
||||
crate::config::ColumnAlignment::Left => { cell.align(Alignment::LEFT); },
|
||||
crate::config::ColumnAlignment::Right => {
|
||||
cell.align(Alignment::RIGHT);
|
||||
}
|
||||
crate::config::ColumnAlignment::Left => {
|
||||
cell.align(Alignment::LEFT);
|
||||
}
|
||||
}
|
||||
cell
|
||||
},
|
||||
}
|
||||
ColumnType::Tags => {
|
||||
let mut 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); },
|
||||
crate::config::ColumnAlignment::Right => {
|
||||
cell.align(Alignment::RIGHT);
|
||||
}
|
||||
crate::config::ColumnAlignment::Left => {
|
||||
cell.align(Alignment::LEFT);
|
||||
}
|
||||
}
|
||||
cell
|
||||
},
|
||||
ColumnType::Meta => {
|
||||
let cell = match meta_name {
|
||||
Some(meta_name) => match meta.get(meta_name) {
|
||||
Some(meta_value) => {
|
||||
let mut cell = Cell::new(&string_column(meta_value.to_string(), column_width));
|
||||
match column.align {
|
||||
crate::config::ColumnAlignment::Right => { cell.align(Alignment::RIGHT); },
|
||||
crate::config::ColumnAlignment::Left => { cell.align(Alignment::LEFT); },
|
||||
}
|
||||
ColumnType::Meta => match meta_name {
|
||||
Some(meta_name) => match meta.get(meta_name) {
|
||||
Some(meta_value) => {
|
||||
let mut cell =
|
||||
Cell::new(&string_column(meta_value.to_string(), column_width));
|
||||
match column.align {
|
||||
crate::config::ColumnAlignment::Right => {
|
||||
cell.align(Alignment::RIGHT);
|
||||
}
|
||||
crate::config::ColumnAlignment::Left => {
|
||||
cell.align(Alignment::LEFT);
|
||||
}
|
||||
cell
|
||||
}
|
||||
None => {
|
||||
let mut cell = Cell::new("");
|
||||
match column.align {
|
||||
crate::config::ColumnAlignment::Right => { cell.align(Alignment::RIGHT); },
|
||||
crate::config::ColumnAlignment::Left => { cell.align(Alignment::LEFT); },
|
||||
}
|
||||
cell
|
||||
},
|
||||
},
|
||||
cell
|
||||
}
|
||||
None => {
|
||||
let mut cell = Cell::new("");
|
||||
match column.align {
|
||||
crate::config::ColumnAlignment::Right => { cell.align(Alignment::RIGHT); },
|
||||
crate::config::ColumnAlignment::Left => { cell.align(Alignment::LEFT); },
|
||||
crate::config::ColumnAlignment::Right => {
|
||||
cell.align(Alignment::RIGHT);
|
||||
}
|
||||
crate::config::ColumnAlignment::Left => {
|
||||
cell.align(Alignment::LEFT);
|
||||
}
|
||||
}
|
||||
cell
|
||||
},
|
||||
};
|
||||
cell
|
||||
}
|
||||
},
|
||||
None => {
|
||||
let mut cell = Cell::new("");
|
||||
match column.align {
|
||||
crate::config::ColumnAlignment::Right => {
|
||||
cell.align(Alignment::RIGHT);
|
||||
}
|
||||
crate::config::ColumnAlignment::Left => {
|
||||
cell.align(Alignment::LEFT);
|
||||
}
|
||||
}
|
||||
cell
|
||||
}
|
||||
},
|
||||
};
|
||||
table_row.add_cell(cell);
|
||||
@@ -262,20 +288,19 @@ pub fn mode_list(
|
||||
}
|
||||
|
||||
fn show_list_structured(
|
||||
items: Vec<crate::db::Item>,
|
||||
tags_by_item: std::collections::HashMap<i64, Vec<String>>,
|
||||
meta_by_item: std::collections::HashMap<i64, std::collections::HashMap<String, String>>,
|
||||
items_with_meta: Vec<ItemWithMeta>,
|
||||
data_path: std::path::PathBuf,
|
||||
settings: &config::Settings,
|
||||
output_format: OutputFormat,
|
||||
) -> anyhow::Result<()> {
|
||||
) -> Result<()> {
|
||||
let mut list_items = Vec::new();
|
||||
|
||||
for item in items {
|
||||
for item_with_meta in items_with_meta {
|
||||
let item = item_with_meta.item;
|
||||
let item_id = item.id.unwrap();
|
||||
let tags = tags_by_item.get(&item_id).cloned().unwrap_or_default();
|
||||
let meta = meta_by_item.get(&item_id).cloned().unwrap_or_default();
|
||||
|
||||
let tags = item_with_meta.tags.into_iter().map(|t| t.name).collect();
|
||||
let meta = item_with_meta.meta_as_map();
|
||||
|
||||
let mut item_path = data_path.clone();
|
||||
item_path.push(item_id.to_string());
|
||||
|
||||
@@ -292,7 +317,11 @@ fn show_list_structured(
|
||||
|
||||
let list_item = ListItem {
|
||||
id: item.id,
|
||||
time: item.ts.with_timezone(&chrono::Local).format("%F %T").to_string(),
|
||||
time: item
|
||||
.ts
|
||||
.with_timezone(&chrono::Local)
|
||||
.format("%F %T")
|
||||
.to_string(),
|
||||
size: item.size.map(|s| s as u64),
|
||||
size_formatted,
|
||||
compression: item.compression,
|
||||
|
||||
Reference in New Issue
Block a user