From 9d4c59d0b57448e08694132e4758efa40c1ac98c Mon Sep 17 00:00:00 2001 From: "Andrew Phillips (aider)" Date: Sat, 10 May 2025 09:19:54 -0300 Subject: [PATCH] refactor: move mode_status to src/modes/status.rs --- src/main.rs | 2 +- src/modes/mod.rs | 1 + src/modes/status.rs | 119 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 src/modes/status.rs diff --git a/src/main.rs b/src/main.rs index 58d1b0f..da9aef5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -320,7 +320,7 @@ fn main() -> Result<(), Error> { KeepModes::Update => crate::modes::update::mode_update(&mut cmd, args, ids, tags, &mut conn, data_path)?, KeepModes::Info => mode_info(&mut cmd, args, ids, tags, &mut conn, data_path)?, KeepModes::Delete => crate::modes::delete::mode_delete(&mut cmd, args, ids, tags, &mut conn, data_path)?, - KeepModes::Status => mode_status(&mut cmd, args, data_path, db_path)?, + KeepModes::Status => crate::modes::status::mode_status(&mut cmd, args, data_path, db_path)?, _ => todo!() } diff --git a/src/modes/mod.rs b/src/modes/mod.rs index 22b8bd9..6047c33 100644 --- a/src/modes/mod.rs +++ b/src/modes/mod.rs @@ -2,3 +2,4 @@ pub mod common; pub mod get; pub mod delete; pub mod update; +pub mod status; diff --git a/src/modes/status.rs b/src/modes/status.rs new file mode 100644 index 0000000..d8ee34b --- /dev/null +++ b/src/modes/status.rs @@ -0,0 +1,119 @@ +use std::io; +use std::path::PathBuf; +use anyhow::{Context, Result, Error, anyhow}; +use rusqlite::Connection; +use gethostname::gethostname; +use strum::IntoEnumIterator; +use clap::error::ErrorKind; +use clap::*; +use log::*; +use chrono::prelude::*; +use std::os::fd::FromRawFd; +use std::process::Stdio; +use nix::fcntl::{FdFlag}; +use nix::unistd::{close, pipe}; +use nix::Error as NixError; + +use crate::compression::CompressionType; +use crate::compression::program::CompressionEngineProgram; +use crate::compression::COMPRESSION_PROGRAMS; +use crate::compression::default_type; +use crate::db; +use crate::modes::common::{format_size, string_column}; +use crate::compression::CompressionEngine; + +use prettytable::{Table, Row, Cell, Attr}; +use prettytable::format; +use prettytable::format::{TableFormat, Alignment}; +use prettytable::row; +use prettytable::color; +use crate::FORMAT_BOX_CHARS_NO_BORDER_LINE_SEPARATOR; +use crate::FORMAT_NO_BORDER_LINE_SEPARATOR; + +pub fn mode_status(_cmd: &mut Command, args: Args, data_path: PathBuf, db_path: PathBuf) -> Result<()> { + 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")) + ])); + + + 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 = match args.item.compression { + Some(compression_name) => CompressionType::from_str(&compression_name) + .context(anyhow!("Invalid compression type {}", compression_name))?, + None => default_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.eq("") { + 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(" ")), + ])); + } + + println!("PATHS:"); + path_table.printstd(); + println!(); + println!("COMPRESSION:"); + compression_table.printstd(); + + Ok(()) +}