Files
keep/src/modes/status.rs
Andrew Phillips f8c896fa25 fix: make generate_status_info public and remove unused imports
Co-authored-by: aider (openai/andrew/openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
2025-08-11 13:04:19 -03:00

298 lines
9.8 KiB
Rust

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::modes::common::{get_format_box_chars_no_border_line_separator, get_output_format, OutputFormat};
use prettytable::color;
use serde::{Deserialize, Serialize};
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;
use crate::meta_plugin::MetaPluginType;
#[derive(Serialize, Deserialize)]
pub struct StatusInfo {
pub paths: PathInfo,
pub compression: Vec<CompressionInfo>,
pub meta_plugins: Vec<MetaPluginInfo>,
}
#[derive(Serialize, Deserialize)]
pub struct PathInfo {
pub data: String,
pub database: String,
}
#[derive(Serialize, Deserialize)]
pub struct CompressionInfo {
#[serde(rename = "type")]
pub compression_type: String,
pub found: bool,
pub default: bool,
pub binary: String,
pub compress: String,
pub decompress: String,
}
#[derive(Serialize, Deserialize)]
pub struct MetaPluginInfo {
pub meta_name: String,
pub found: bool,
pub enabled: bool,
pub binary: String,
pub args: String,
}
pub fn generate_status_info(
data_path: PathBuf,
db_path: PathBuf,
enabled_meta_plugins: &Vec<MetaPluginType>,
) -> StatusInfo {
let path_info = PathInfo {
data: data_path.into_os_string().into_string().expect("Unable to convert data path to string"),
database: db_path.into_os_string().into_string().expect("Unable to convert DB path to string"),
};
let default_type = compression_engine::default_compression_type();
let mut compression_info = Vec::new();
// Sort compression types by their string representation
let mut sorted_compression_types: Vec<CompressionType> = CompressionType::iter().collect();
sorted_compression_types.sort_by_key(|ct| ct.to_string());
for compression_type in sorted_compression_types {
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;
let binary = if compression_program.program.is_empty() {
"<INTERNAL>".to_string()
} else {
compression_program.program
};
compression_info.push(CompressionInfo {
compression_type: compression_type.to_string(),
found: compression_program.supported,
default: is_default,
binary,
compress: compression_program.compress.join(" "),
decompress: compression_program.decompress.join(" "),
});
}
let mut meta_plugin_info = Vec::new();
// Sort meta plugin types by their meta name
let mut sorted_meta_plugins: Vec<MetaPluginType> = MetaPluginType::iter().collect();
sorted_meta_plugins.sort_by_key(|meta_plugin_type| {
let mut meta_plugin = meta_plugin::get_meta_plugin(meta_plugin_type.clone());
meta_plugin.meta_name()
});
for meta_plugin_type in sorted_meta_plugins {
let mut meta_plugin = meta_plugin::get_meta_plugin(meta_plugin_type.clone());
let is_supported = meta_plugin.is_supported();
let is_enabled = enabled_meta_plugins.contains(&meta_plugin_type);
let (binary_display, args_display) = if !is_supported {
("<NOT FOUND>".to_string(), "".to_string())
} else {
if meta_plugin.is_internal() {
("<INTERNAL>".to_string(), "".to_string())
} else {
if let Some((program, args)) = meta_plugin.program_info() {
(program.to_string(), args.join(" "))
} else {
("<NOT FOUND>".to_string(), "".to_string())
}
}
};
meta_plugin_info.push(MetaPluginInfo {
meta_name: meta_plugin.meta_name(),
found: is_supported,
enabled: is_enabled,
binary: binary_display,
args: args_display,
});
}
StatusInfo {
paths: path_info,
compression: compression_info,
meta_plugins: meta_plugin_info,
}
}
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<CompressionInfo>) -> 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() {
"<INTERNAL>" => 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<MetaPluginInfo>) -> 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() {
"<INTERNAL>" => Cell::new(&info.binary).with_style(Attr::ForegroundColor(color::BRIGHT_BLACK)),
"<NOT FOUND>" => 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<MetaPluginType> = 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(())
}
}
}