use clap::*; use is_terminal::IsTerminal; use std::path::PathBuf; use strum::IntoEnumIterator; use crate::compression_engine; use crate::compression_engine::COMPRESSION_PROGRAMS; use crate::compression_engine::CompressionType; use crate::compression_engine::program::CompressionEngineProgram; use crate::FORMAT_BOX_CHARS_NO_BORDER_LINE_SEPARATOR; use crate::FORMAT_NO_BORDER_LINE_SEPARATOR; use prettytable::color; use prettytable::row; use prettytable::{Attr, Cell, Row, Table}; fn build_path_table(data_path: PathBuf, db_path: PathBuf) -> Table { let mut path_table = Table::new(); if std::io::stdout().is_terminal() { path_table.set_format(*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( &data_path .into_os_string() .into_string() .expect("Unable to convert data path to string"), ), ])); path_table.add_row(Row::new(vec![ Cell::new("Database"), Cell::new( &db_path .into_os_string() .into_string() .expect("Unable to convert DB path to string"), ), ])); path_table } fn build_compression_table() -> Table { let mut compression_table = Table::new(); if std::io::stdout().is_terminal() { compression_table.set_format(*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")); let default_type = compression_engine::default_compression_type(); for compression_type in CompressionType::iter() { let compression_program: CompressionEngineProgram = match &COMPRESSION_PROGRAMS[compression_type.clone()] { Some(compression_program) => compression_program.clone(), None => CompressionEngineProgram { program: "".to_string(), compress: Vec::new(), decompress: Vec::new(), supported: true, }, }; let is_default = compression_type == default_type; compression_table.add_row(Row::new(vec![ Cell::new(&compression_type.to_string()), match compression_program.supported { true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)), false => Cell::new("No").with_style(Attr::ForegroundColor(color::RED)), }, match is_default { true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)), false => Cell::new("No"), }, match compression_program.program.is_empty() { true => { Cell::new("").with_style(Attr::ForegroundColor(color::BRIGHT_BLACK)) } false => Cell::new(&compression_program.program), }, Cell::new(&compression_program.compress.join(" ")), Cell::new(&compression_program.decompress.join(" ")), ])); } compression_table } fn build_digest_table() -> Table { use crate::digest_engine; use crate::digest_engine::DIGEST_PROGRAMS; use crate::digest_engine::DigestType; use crate::digest_engine::program::DigestEngineProgram; let mut digest_table = Table::new(); if std::io::stdout().is_terminal() { digest_table.set_format(*FORMAT_BOX_CHARS_NO_BORDER_LINE_SEPARATOR); } else { digest_table.set_format(*FORMAT_NO_BORDER_LINE_SEPARATOR); } digest_table.set_titles(row!( b->"Type", b->"Found", b->"Default", b->"Binary", b->"Args")); let default_type = digest_engine::default_digest_type(); for digest_type in DigestType::iter() { let digest_program: DigestEngineProgram = match &DIGEST_PROGRAMS[digest_type.clone()] { Some(digest_program) => digest_program.clone(), None => DigestEngineProgram { program: "".to_string(), args: Vec::new(), supported: true, }, }; let is_default = digest_type == default_type; digest_table.add_row(Row::new(vec![ Cell::new(&digest_type.to_string()), match digest_program.supported { true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)), false => Cell::new("No").with_style(Attr::ForegroundColor(color::RED)), }, match is_default { true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)), false => Cell::new("No"), }, match digest_program.program.is_empty() { true => { Cell::new("").with_style(Attr::ForegroundColor(color::BRIGHT_BLACK)) } false => Cell::new(&digest_program.program), }, Cell::new(&digest_program.args.join(" ")), ])); } digest_table } pub fn mode_status( _cmd: &mut Command, _args: &crate::Args, data_path: PathBuf, db_path: PathBuf, ) -> Result<(), anyhow::Error> { println!("PATHS:"); build_path_table(data_path, db_path).printstd(); println!(); println!("COMPRESSION:"); build_compression_table().printstd(); println!(); println!("DIGEST:"); build_digest_table().printstd(); Ok(()) }