feat: add --output-format option for json/yaml support in info/status/list modes

Co-authored-by: aider (openai/andrew/openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-10 11:21:04 -03:00
parent 9f93d6965f
commit 0d1ae9ff12
5 changed files with 353 additions and 3 deletions

View File

@@ -1,6 +1,9 @@
use crate::db::{get_items, get_items_matching};
use crate::modes::common::ColumnType;
use crate::modes::common::{size_column, string_column};
use crate::modes::common::{size_column, string_column, get_output_format, OutputFormat};
use serde::{Deserialize, Serialize};
use serde_json;
use serde_yaml;
use anyhow::anyhow;
use log::debug;
use prettytable::color;
@@ -8,6 +11,20 @@ use prettytable::row;
use prettytable::format::Alignment;
use prettytable::{Attr, Cell, Row, Table};
#[derive(Serialize, Deserialize)]
struct ListItem {
id: Option<i64>,
time: String,
size: Option<u64>,
size_formatted: String,
compression: String,
file_size: Option<u64>,
file_size_formatted: String,
file_path: String,
tags: Vec<String>,
meta: std::collections::HashMap<String, String>,
}
pub fn mode_list(
cmd: &mut clap::Command,
args: &crate::Args,
@@ -54,6 +71,12 @@ pub fn mode_list(
// Fetch all metadata for all items in a single query
let meta_by_item = crate::db::get_meta_for_items(conn, &item_ids)?;
let output_format = get_output_format(args);
if output_format != OutputFormat::Table {
return show_list_structured(items, tags_by_item, meta_by_item, data_path, args, output_format);
}
let mut table = Table::new();
table.set_format(*prettytable::format::consts::FORMAT_CLEAN);
@@ -166,3 +189,61 @@ pub fn mode_list(
Ok(())
}
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>>,
data_path: std::path::PathBuf,
args: &crate::Args,
output_format: OutputFormat,
) -> anyhow::Result<()> {
let mut list_items = Vec::new();
for item in items {
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 mut item_path = data_path.clone();
item_path.push(item_id.to_string());
let file_size = item_path.metadata().map(|m| m.len()).ok();
let file_size_formatted = match file_size {
Some(size) => crate::modes::common::format_size(size, args.options.human_readable),
None => "Missing".to_string(),
};
let size_formatted = match item.size {
Some(size) => crate::modes::common::format_size(size as u64, args.options.human_readable),
None => "Unknown".to_string(),
};
let list_item = ListItem {
id: item.id,
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,
file_size,
file_size_formatted,
file_path: item_path.into_os_string().into_string().unwrap_or_default(),
tags,
meta,
};
list_items.push(list_item);
}
match output_format {
OutputFormat::Json => {
println!("{}", serde_json::to_string_pretty(&list_items)?);
}
OutputFormat::Yaml => {
println!("{}", serde_yaml::to_string(&list_items)?);
}
OutputFormat::Table => unreachable!(),
}
Ok(())
}