use clap::*; use is_terminal::IsTerminal; use std::path::PathBuf; use crate::modes::common::{get_format_box_chars_no_border_line_separator, get_output_format, OutputFormat}; 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}; 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->"Default", 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")); for info in meta_plugin_info { 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), ])); } meta_plugin_table } pub fn mode_status( _cmd: &mut Command, args: &crate::Args, data_path: PathBuf, db_path: PathBuf, ) -> Result<(), anyhow::Error> { // Determine which meta plugins would be enabled for a save operation let mut meta_plugin_types: Vec = crate::modes::common::cmd_args_meta_plugin_types(_cmd, &args); // Add digest type if specified let digest_type = crate::modes::common::cmd_args_digest_type(_cmd, &args); 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); } } let output_format = get_output_format(args); let status_info = generate_status_info(data_path, db_path, &meta_plugin_types); 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(()) } } }