refactor: Move mode_info to src/modes/info.rs

This commit is contained in:
Andrew Phillips (aider)
2025-05-10 12:19:00 -03:00
parent 86a3a7da61
commit 1818024bc9
2 changed files with 144 additions and 0 deletions

143
src/modes/info.rs Normal file
View File

@@ -0,0 +1,143 @@
use anyhow::anyhow;
use clap::Command;
use std::path::PathBuf;
use std::str::FromStr;
use crate::compression::CompressionType;
use crate::db::{get_item, get_item_last, get_item_matching};
use crate::compression::get_engine;
use crate::common::{format_size, string_column};
use chrono::prelude::*;
use prettytable::format;
use prettytable::{Attr, Cell, Row, Table};
pub fn mode_info(
cmd: &mut Command,
args: crate::Args,
ids: &mut Vec<i64>,
tags: &mut Vec<String>,
conn: &mut rusqlite::Connection,
data_path: PathBuf,
) -> anyhow::Result<()> {
if !ids.is_empty() && !tags.is_empty() {
cmd.error(ErrorKind::InvalidValue, "Both ID and tags given, you must supply exactly one ID or atleast one tag when using --info").exit();
} else if ids.len() > 1 {
cmd.error(ErrorKind::InvalidValue, "More than one ID given, you must supply exactly one ID or atleast one tag when using --info").exit();
}
let mut meta: std::collections::HashMap<String, String> = std::collections::HashMap::new();
for item in args.item.meta.iter() {
let item = item.clone();
meta.insert(item.key, item.value);
}
let item_maybe = match tags.is_empty() && meta.is_empty() {
true => match ids.iter().next() {
Some(item_id) => get_item(conn, *item_id)?,
None => get_item_last(conn)?,
},
false => get_item_matching(conn, tags, &meta)?,
};
if let Some(item) = item_maybe {
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 table = Table::new();
if std::io::stdout().is_terminal() {
table.set_format(*format::consts::FORMAT_BOX_CHARS_NO_BORDER_LINE_SEPARATOR);
} else {
table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
}
table.add_row(Row::new(vec![
Cell::new("ID").with_style(Attr::Bold),
Cell::new(&item_id.to_string()),
]));
let ts_cell = Cell::new(&item.ts.with_timezone(&Local).format("%F %T %Z").to_string());
table.add_row(Row::new(vec![
Cell::new("Timestamp").with_style(Attr::Bold),
ts_cell,
]));
let mut item_path = data_path.clone();
item_path.push(item.id.unwrap().to_string());
table.add_row(Row::new(vec![
Cell::new("Path").with_style(Attr::Bold),
Cell::new(item_path.to_str().expect("Unable to get item path")),
]));
let size_cell = match item.size {
Some(size) => Cell::new(format_size(size as u64, args.options.human_readable).as_str()),
None => Cell::new("Missing")
.with_style(Attr::ForegroundColor(prettytable::color::RED))
.with_style(Attr::Bold),
};
table.add_row(Row::new(vec![
Cell::new("Stream Size").with_style(Attr::Bold),
size_cell,
]));
let compression_type = CompressionType::from_str(&item.compression)?;
table.add_row(Row::new(vec![
Cell::new("Compression").with_style(Attr::Bold),
Cell::new(&compression_type.to_string()),
]));
let file_size_cell = match item_path.metadata() {
Ok(metadata) => {
Cell::new(format_size(metadata.len(), args.options.human_readable).as_str())
}
Err(_) => Cell::new("Missing")
.with_style(Attr::ForegroundColor(prettytable::color::RED))
.with_style(Attr::Bold),
};
table.add_row(Row::new(vec![
Cell::new("File Size").with_style(Attr::Bold),
file_size_cell,
]));
let compression_engine =
get_engine(compression_type).expect("Unable to get compression engine");
let magic = compression_engine.magic(item_path.clone());
let file_magic_cell = match magic {
Ok(magic) => Cell::new(magic.as_str()),
Err(e) => Cell::new(&e.to_string())
.with_style(Attr::ForegroundColor(prettytable::color::RED))
.with_style(Attr::Bold),
};
table.add_row(Row::new(vec![
Cell::new("File Magic").with_style(Attr::Bold),
file_magic_cell,
]));
table.add_row(Row::new(vec![
Cell::new("Tags").with_style(Attr::Bold),
Cell::new(&item_tags.join(" ")),
]));
for meta in crate::db::get_item_meta(conn, &item)? {
let meta_name = format!("Meta: {}", &meta.name);
table.add_row(Row::new(vec![
Cell::new(meta_name.as_str()).with_style(Attr::Bold),
Cell::new(&meta.value),
]));
}
table.printstd();
Ok(())
} else {
Err(anyhow!("Unable to find matching item in database"))
}
}

View File

@@ -1,6 +1,7 @@
pub mod common;
pub mod delete;
pub mod get;
pub mod info;
pub mod save;
pub mod status;
pub mod update;