use clap::*; use is_terminal::IsTerminal; use std::path::PathBuf; use std::str::FromStr; use crate::modes::common::{get_format_box_chars_no_border_line_separator, OutputFormat}; use crate::config; use prettytable::color; use serde_json; use serde_yaml; use prettytable::row; use prettytable::{Attr, Cell, Row, Table}; use prettytable::format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR; use crate::meta_plugin::MetaPluginType; use crate::common::status::{generate_status_info, PathInfo, CompressionInfo, MetaPluginInfo}; use crate::compression_engine::CompressionType; fn build_path_table(path_info: &PathInfo) -> Table { let mut path_table = Table::new(); if std::io::stdout().is_terminal() { path_table.set_format(get_format_box_chars_no_border_line_separator()); } else { path_table.set_format(*FORMAT_NO_BORDER_LINE_SEPARATOR); } path_table.set_titles(Row::new(vec![ Cell::new("Type").with_style(Attr::Bold), Cell::new("Path").with_style(Attr::Bold), ])); path_table.add_row(Row::new(vec![ Cell::new("Data"), Cell::new(&path_info.data), ])); path_table.add_row(Row::new(vec![ Cell::new("Database"), Cell::new(&path_info.database), ])); path_table } fn build_compression_table(compression_info: &Vec) -> Table { let mut compression_table = Table::new(); if std::io::stdout().is_terminal() { compression_table.set_format(get_format_box_chars_no_border_line_separator()); } else { compression_table.set_format(*FORMAT_NO_BORDER_LINE_SEPARATOR); } compression_table.set_titles(row!( b->"Type", b->"Found", b->"Enabled", b->"Binary", b->"Compress", b->"Decompress")); for info in compression_info { compression_table.add_row(Row::new(vec![ Cell::new(&info.compression_type), match info.found { true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)), false => Cell::new("No").with_style(Attr::ForegroundColor(color::RED)), }, match info.default { true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)), false => Cell::new("No"), }, match info.binary.as_str() { "" => Cell::new(&info.binary).with_style(Attr::ForegroundColor(color::BRIGHT_BLACK)), _ => Cell::new(&info.binary), }, Cell::new(&info.compress), Cell::new(&info.decompress), ])); } compression_table } fn build_meta_plugin_table(meta_plugin_info: &Vec) -> Table { let mut meta_plugin_table = Table::new(); if std::io::stdout().is_terminal() { meta_plugin_table.set_format(get_format_box_chars_no_border_line_separator()); } else { meta_plugin_table.set_format(*FORMAT_NO_BORDER_LINE_SEPARATOR); } meta_plugin_table.set_titles(row!( b->"Meta Name", b->"Found", b->"Enabled", b->"Binary", b->"Args", b->"Outputs")); for info in meta_plugin_info { let outputs_display = if info.outputs.is_empty() { "{}".to_string() } else { serde_yaml::to_string(&info.outputs).unwrap_or_else(|_| "{}".to_string()).trim().to_string() }; meta_plugin_table.add_row(Row::new(vec![ Cell::new(&info.meta_name), match info.found { true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)), false => Cell::new("No").with_style(Attr::ForegroundColor(color::RED)), }, match info.enabled { true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)), false => Cell::new("No"), }, match info.binary.as_str() { "" => Cell::new(&info.binary).with_style(Attr::ForegroundColor(color::BRIGHT_BLACK)), "" => Cell::new(&info.binary).with_style(Attr::ForegroundColor(color::RED)), _ => Cell::new(&info.binary), }, Cell::new(&info.args), Cell::new(&outputs_display), ])); } meta_plugin_table } pub fn mode_status( _cmd: &mut Command, settings: &config::Settings, data_path: PathBuf, db_path: PathBuf, ) -> Result<(), anyhow::Error> { log::debug!("STATUS: Starting mode_status function"); // Determine which meta plugins would be enabled for a save operation log::debug!("STATUS: Getting meta plugin types from settings"); let mut meta_plugin_types: Vec = crate::modes::common::settings_meta_plugin_types(_cmd, settings); log::debug!("STATUS: Got {} meta plugin types", meta_plugin_types.len()); // Add digest type if specified let digest_type = crate::modes::common::settings_digest_type(_cmd, settings); let digest_meta_plugin_type = match digest_type { crate::meta_plugin::MetaPluginType::DigestSha256 => Some(MetaPluginType::DigestSha256), crate::meta_plugin::MetaPluginType::DigestMd5 => Some(MetaPluginType::DigestMd5), _ => None, }; if let Some(digest_plugin_type) = digest_meta_plugin_type { if !meta_plugin_types.contains(&digest_plugin_type) { meta_plugin_types.push(digest_plugin_type); } } // Determine which compression type would be enabled for a save operation let enabled_compression_type = if let Some(compression_name) = &settings.compression() { CompressionType::from_str(compression_name).ok() } else { Some(crate::compression_engine::default_compression_type()) }; let output_format = crate::modes::common::settings_output_format(settings); log::debug!("STATUS: About to call generate_status_info"); let status_info = generate_status_info(data_path, db_path, &meta_plugin_types, enabled_compression_type); log::debug!("STATUS: generate_status_info completed successfully"); match output_format { OutputFormat::Table => { println!("PATHS:"); build_path_table(&status_info.paths).printstd(); println!(); println!("COMPRESSION:"); build_compression_table(&status_info.compression).printstd(); println!(); println!("META PLUGINS:"); build_meta_plugin_table(&status_info.meta_plugins).printstd(); Ok(()) }, OutputFormat::Json => { println!("{}", serde_json::to_string_pretty(&status_info)?); Ok(()) }, OutputFormat::Yaml => { println!("{}", serde_yaml::to_string(&status_info)?); Ok(()) } } }