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::Item;
use crate::modes::common::format_size;
use crate::modes::common::{format_size, get_output_format, OutputFormat};
use anyhow::anyhow;
use serde_json;
use serde_yaml;
use serde::{Deserialize, Serialize};
use clap::Command;
use clap::error::ErrorKind;
use std::path::PathBuf;
@@ -48,6 +51,20 @@ pub fn mode_info(
}
}
#[derive(Serialize, Deserialize)]
struct ItemInfo {
id: i64,
timestamp: String,
path: String,
stream_size: Option<u64>,
stream_size_formatted: String,
compression: String,
file_size: Option<u64>,
file_size_formatted: String,
tags: Vec<String>,
meta: std::collections::HashMap<String, String>,
}
fn show_item(
item: Item, // Using the provided struct definition
args: &crate::Args,
@@ -61,6 +78,12 @@ fn show_item(
.map(|x| x.name)
.collect();
let output_format = get_output_format(args);
if output_format != OutputFormat::Table {
return show_item_structured(item, args, conn, data_path, output_format);
}
let mut table = Table::new();
if std::io::stdout().is_terminal() {
table.set_format(get_format_box_chars_no_border_line_separator());
@@ -136,3 +159,61 @@ fn show_item(
table.printstd();
Ok(())
}
fn show_item_structured(
item: Item,
args: &crate::Args,
conn: &mut rusqlite::Connection,
data_path: PathBuf,
output_format: OutputFormat,
) -> anyhow::Result<()> {
let item_id = item.id.unwrap();
let item_tags: Vec<String> = crate::db::get_item_tags(conn, &item)?
.into_iter()
.map(|x| x.name)
.collect();
let mut item_path_buf = data_path.clone();
item_path_buf.push(item_id.to_string());
let file_size = item_path_buf.metadata().map(|m| m.len()).ok();
let file_size_formatted = match file_size {
Some(size) => format_size(size, args.options.human_readable),
None => "Missing".to_string(),
};
let stream_size_formatted = match item.size {
Some(size) => format_size(size as u64, args.options.human_readable),
None => "Missing".to_string(),
};
let mut meta_map = std::collections::HashMap::new();
for meta in crate::db::get_item_meta(conn, &item)? {
meta_map.insert(meta.name, meta.value);
}
let item_info = ItemInfo {
id: item_id,
timestamp: item.ts.with_timezone(&chrono::Local).format("%F %T %Z").to_string(),
path: item_path_buf.to_str().unwrap_or("").to_string(),
stream_size: item.size.map(|s| s as u64),
stream_size_formatted,
compression: item.compression,
file_size,
file_size_formatted,
tags: item_tags,
meta: meta_map,
};
match output_format {
OutputFormat::Json => {
println!("{}", serde_json::to_string_pretty(&item_info)?);
}
OutputFormat::Yaml => {
println!("{}", serde_yaml::to_string(&item_info)?);
}
OutputFormat::Table => unreachable!(),
}
Ok(())
}