use crate::config; use crate::services::types::ItemWithMeta; use crate::modes::common::{format_size, OutputFormat}; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; use clap::Command; use clap::error::ErrorKind; use std::path::PathBuf; use crate::services::item_service::ItemService; use chrono::prelude::*; use is_terminal::IsTerminal; use comfy_table::{Table, ContentArrangement, Cell, Attribute}; use comfy_table::presets::UTF8_FULL; use comfy_table::modifiers::UTF8_ROUND_CORNERS; pub fn mode_info( cmd: &mut Command, settings: &config::Settings, ids: &mut Vec, tags: &mut Vec, conn: &mut rusqlite::Connection, data_path: PathBuf, ) -> Result<()> { // For --info, we can use either IDs or tags, but not both if !ids.is_empty() && !tags.is_empty() { cmd.error(ErrorKind::InvalidValue, "Both ID and tags given, you must supply either IDs or tags when using --info").exit(); } else if ids.len() > 1 { cmd.error(ErrorKind::InvalidValue, "More than one ID given, you must supply exactly one ID when using --info").exit(); } // If both are empty, find_item will find the last item let item_service = ItemService::new(data_path.clone()); // Use empty metadata HashMap let item_with_meta = item_service .find_item(conn, ids, tags, &std::collections::HashMap::new()) .map_err(|e| anyhow!("Unable to find matching item in database: {}", e))?; show_item(item_with_meta, settings, data_path) } #[derive(Debug, Serialize, Deserialize)] struct ItemInfo { id: i64, timestamp: String, path: String, stream_size: Option, stream_size_formatted: String, compression: String, file_size: Option, file_size_formatted: String, tags: Vec, meta: std::collections::HashMap, } fn show_item( item_with_meta: ItemWithMeta, settings: &config::Settings, data_path: PathBuf, ) -> Result<()> { let output_format = crate::modes::common::settings_output_format(settings); if output_format != OutputFormat::Table { return show_item_structured(item_with_meta, settings, data_path, output_format); } let item = item_with_meta.item; let item_id = item.id.unwrap(); let item_tags: Vec = item_with_meta.tags.iter().map(|t| t.name.clone()).collect(); let mut table = Table::new(); table .load_preset(UTF8_FULL) .apply_modifier(UTF8_ROUND_CORNERS) .set_content_arrangement(ContentArrangement::Dynamic); if !std::io::stdout().is_terminal() { table.force_no_tty(); } // Add all the rows table.add_row(vec![ Cell::new("ID").add_attribute(Attribute::Bold), Cell::new(&item_id.to_string()), ]); let timestamp_str = item.ts.with_timezone(&Local).format("%F %T %Z").to_string(); table.add_row(vec![ Cell::new("Timestamp").add_attribute(Attribute::Bold), Cell::new(×tamp_str), ]); let mut item_path_buf = data_path.clone(); item_path_buf.push(item.id.unwrap().to_string()); let path_str = item_path_buf.to_str().expect("Unable to get item path").to_string(); table.add_row(vec![ Cell::new("Path").add_attribute(Attribute::Bold), Cell::new(&path_str), ]); let size_str = match item.size { Some(size) => format_size(size as u64, settings.human_readable), None => "Missing".to_string(), }; table.add_row(vec![ Cell::new("Stream Size").add_attribute(Attribute::Bold), Cell::new(&size_str), ]); table.add_row(vec![ Cell::new("Compression").add_attribute(Attribute::Bold), Cell::new(&item.compression), ]); let file_size_str = match item_path_buf.metadata() { Ok(metadata) => format_size(metadata.len(), settings.human_readable), Err(_) => "Missing".to_string(), }; table.add_row(vec![ Cell::new("File Size").add_attribute(Attribute::Bold), Cell::new(&file_size_str), ]); let tags_str = item_tags.join(" "); table.add_row(vec![ Cell::new("Tags").add_attribute(Attribute::Bold), Cell::new(&tags_str), ]); // Add meta rows for meta in item_with_meta.meta { let meta_name = format!("Meta: {}", &meta.name); table.add_row(vec![ Cell::new(&meta_name).add_attribute(Attribute::Bold), Cell::new(&meta.value), ]); } println!("{}", table); Ok(()) } fn show_item_structured( item_with_meta: ItemWithMeta, settings: &config::Settings, data_path: PathBuf, output_format: OutputFormat, ) -> Result<()> { let item_tags: Vec = item_with_meta.tags.iter().map(|t| t.name.clone()).collect(); let meta_map = item_with_meta.meta_as_map(); let item = item_with_meta.item; let item_id = item.id.unwrap(); 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, settings.human_readable), None => "Missing".to_string(), }; let stream_size_formatted = match item.size { Some(size) => format_size(size as u64, settings.human_readable), None => "Missing".to_string(), }; 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(()) }