refactor: unify status data generation and output formatting

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-11 12:22:54 -03:00
parent c4e037d9c6
commit d5fb446763
2 changed files with 151 additions and 299 deletions

View File

@@ -25,7 +25,7 @@ use tower_http::trace::TraceLayer;
use crate::compression_engine::{CompressionType, get_compression_engine}; use crate::compression_engine::{CompressionType, get_compression_engine};
use crate::db; use crate::db;
use crate::Args; use crate::Args;
use crate::modes::status::{PathInfo, CompressionInfo, MetaPluginInfo, StatusInfo}; use crate::modes::status::{StatusInfo, generate_status_info};
use crate::compression_engine::{CompressionType as CompressionTypeEnum, COMPRESSION_PROGRAMS}; use crate::compression_engine::{CompressionType as CompressionTypeEnum, COMPRESSION_PROGRAMS};
use crate::compression_engine::program::CompressionEngineProgram; use crate::compression_engine::program::CompressionEngineProgram;
use crate::meta_plugin::{MetaPluginType, get_meta_plugin}; use crate::meta_plugin::{MetaPluginType, get_meta_plugin};
@@ -220,89 +220,7 @@ async fn handle_status(
let mut db_path = state.data_dir.clone(); let mut db_path = state.data_dir.clone();
db_path.push("keep-1.db"); db_path.push("keep-1.db");
let path_info = PathInfo { let status_info = generate_status_info(state.data_dir.clone(), db_path, &meta_plugin_types);
data: state.data_dir.to_string_lossy().to_string(),
database: db_path.to_string_lossy().to_string(),
};
let default_type = crate::compression_engine::default_compression_type();
let mut compression_info = Vec::new();
// Sort compression types by their string representation
let mut sorted_compression_types: Vec<CompressionTypeEnum> = CompressionTypeEnum::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 = get_meta_plugin(meta_plugin_type.clone());
meta_plugin.meta_name()
});
for meta_plugin_type in sorted_meta_plugins {
let mut meta_plugin = get_meta_plugin(meta_plugin_type.clone());
let is_supported = meta_plugin.is_supported();
let is_enabled = meta_plugin_types.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,
});
}
let status_info = StatusInfo {
paths: path_info,
compression: compression_info,
meta_plugins: meta_plugin_info,
};
let response = ApiResponse { let response = ApiResponse {
success: true, success: true,

View File

@@ -20,175 +20,44 @@ use prettytable::format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR;
use crate::meta_plugin; use crate::meta_plugin;
use crate::meta_plugin::MetaPluginType; use crate::meta_plugin::MetaPluginType;
fn build_path_table(data_path: PathBuf, db_path: PathBuf) -> Table { #[derive(Serialize, Deserialize)]
let mut path_table = Table::new(); pub struct StatusInfo {
pub paths: PathInfo,
if std::io::stdout().is_terminal() { pub compression: Vec<CompressionInfo>,
path_table.set_format(get_format_box_chars_no_border_line_separator()); pub meta_plugins: Vec<MetaPluginInfo>,
} 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 { #[derive(Serialize, Deserialize)]
let mut compression_table = Table::new(); pub struct PathInfo {
if std::io::stdout().is_terminal() { pub data: String,
compression_table.set_format(get_format_box_chars_no_border_line_separator()); pub database: String,
} 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();
// 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;
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("<INTERNAL>").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
} }
#[derive(Serialize, Deserialize)]
fn build_meta_plugin_table(enabled_meta_plugins: &Vec<MetaPluginType>) -> Table { pub struct CompressionInfo {
let mut meta_plugin_table = Table::new(); #[serde(rename = "type")]
if std::io::stdout().is_terminal() { pub compression_type: String,
meta_plugin_table.set_format(get_format_box_chars_no_border_line_separator()); pub found: bool,
} else { pub default: bool,
meta_plugin_table.set_format(*FORMAT_NO_BORDER_LINE_SEPARATOR); pub binary: String,
} pub compress: String,
pub decompress: String,
meta_plugin_table.set_titles(row!(
b->"Meta Name",
b->"Found",
b->"Enabled",
b->"Binary",
b->"Args"));
// 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);
// Determine what implementation will actually be used
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 {
// Get program info from the meta plugin itself
if let Some((program, args)) = meta_plugin.program_info() {
(program.to_string(), args.join(" "))
} else {
("<NOT FOUND>".to_string(), "".to_string())
}
}
};
meta_plugin_table.add_row(Row::new(vec![
Cell::new(&meta_plugin.meta_name()),
match is_supported {
true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)),
false => Cell::new("No").with_style(Attr::ForegroundColor(color::RED)),
},
match is_enabled {
true => Cell::new("Yes").with_style(Attr::ForegroundColor(color::GREEN)),
false => Cell::new("No"),
},
match binary_display.as_str() {
"<INTERNAL>" => Cell::new(&binary_display).with_style(Attr::ForegroundColor(color::BRIGHT_BLACK)),
"<NOT FOUND>" => Cell::new(&binary_display).with_style(Attr::ForegroundColor(color::RED)),
_ => Cell::new(&binary_display),
},
Cell::new(&args_display),
]));
}
meta_plugin_table
} }
fn show_status_structured( #[derive(Serialize, Deserialize)]
pub struct MetaPluginInfo {
pub meta_name: String,
pub found: bool,
pub enabled: bool,
pub binary: String,
pub args: String,
}
fn generate_status_info(
data_path: PathBuf, data_path: PathBuf,
db_path: PathBuf, db_path: PathBuf,
enabled_meta_plugins: &Vec<MetaPluginType>, enabled_meta_plugins: &Vec<MetaPluginType>,
output_format: OutputFormat, ) -> StatusInfo {
) -> Result<(), anyhow::Error> {
let path_info = PathInfo { let path_info = PathInfo {
data: data_path.into_os_string().into_string().expect("Unable to convert data path to string"), 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"), database: db_path.into_os_string().into_string().expect("Unable to convert DB path to string"),
@@ -267,61 +136,117 @@ fn show_status_structured(
}); });
} }
let status_info = StatusInfo { StatusInfo {
paths: path_info, paths: path_info,
compression: compression_info, compression: compression_info,
meta_plugins: meta_plugin_info, meta_plugins: meta_plugin_info,
}; }
}
match output_format { fn build_path_table(path_info: &PathInfo) -> Table {
OutputFormat::Json => { let mut path_table = Table::new();
println!("{}", serde_json::to_string_pretty(&status_info)?);
} if std::io::stdout().is_terminal() {
OutputFormat::Yaml => { path_table.set_format(get_format_box_chars_no_border_line_separator());
println!("{}", serde_yaml::to_string(&status_info)?); } else {
} path_table.set_format(*FORMAT_NO_BORDER_LINE_SEPARATOR);
OutputFormat::Table => unreachable!(),
} }
Ok(()) 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
} }
#[derive(Serialize, Deserialize)] fn build_compression_table(compression_info: &Vec<CompressionInfo>) -> Table {
pub struct StatusInfo { let mut compression_table = Table::new();
pub paths: PathInfo, if std::io::stdout().is_terminal() {
pub compression: Vec<CompressionInfo>, compression_table.set_format(get_format_box_chars_no_border_line_separator());
pub meta_plugins: Vec<MetaPluginInfo>, } 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
} }
#[derive(Serialize, Deserialize)] fn build_meta_plugin_table(meta_plugin_info: &Vec<MetaPluginInfo>) -> Table {
pub struct PathInfo { let mut meta_plugin_table = Table::new();
pub data: String, if std::io::stdout().is_terminal() {
pub database: String, 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
} }
#[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 mode_status( pub fn mode_status(
_cmd: &mut Command, _cmd: &mut Command,
args: &crate::Args, args: &crate::Args,
@@ -346,18 +271,27 @@ pub fn mode_status(
} }
let output_format = get_output_format(args); let output_format = get_output_format(args);
let status_info = generate_status_info(data_path, db_path, &meta_plugin_types);
if output_format != OutputFormat::Table { match output_format {
return show_status_structured(data_path, db_path, &meta_plugin_types, 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(())
}
} }
println!("PATHS:");
build_path_table(data_path, db_path).printstd();
println!();
println!("COMPRESSION:");
build_compression_table().printstd();
println!();
println!("META PLUGINS:");
build_meta_plugin_table(&meta_plugin_types).printstd();
Ok(())
} }