refactor: move compression table creation to build_compression_table function

This commit is contained in:
Andrew Phillips (aider)
2025-05-13 16:26:45 -03:00
parent 0189f3c273
commit aea254ae62

View File

@@ -16,6 +16,60 @@ use prettytable::color;
use prettytable::row;
use prettytable::{Attr, Cell, Row, Table};
fn build_compression_table(args: crate::Args, default_type: CompressionType) -> 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"));
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("<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
}
pub fn mode_status(
_cmd: &mut Command,
args: crate::Args,
@@ -55,61 +109,7 @@ pub fn mode_status(
),
]));
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) => FromStr::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.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(" ")),
]));
}
let compression_table = build_compression_table(args, default_type);
println!("PATHS:");
path_table.printstd();