added mod list version support

print_table! macro now takes argument for width of placeholders
This commit is contained in:
LordMZTE 2020-10-09 15:50:21 +02:00
parent 1779273659
commit 2673036cb0
3 changed files with 41 additions and 18 deletions

View File

@ -29,6 +29,10 @@ args:
- mods:
short: m
help: "if supplied, a mod list will be printed"
- modversions:
short: v
help: "if supplied, mods will also have their version info printed"
requires: "mods"
# IMAGE ARGS
# TODO due to a bug in clap, the image argument is always required because size has a default value

View File

@ -12,31 +12,31 @@ use asciify::AsciiBuilder;
#[macro_export]
macro_rules! print_table {
//list entry
(l $l:expr => $k:expr) => {
println!("{: <20} | {}", $l, $k);
($width:expr; l $l:expr => $k:expr) => {
println!("{: <width$} | {}", $l, $k, width = $width);
};
//block
(b $l:expr => $k:expr) => {
println!("{:=^25}\n{}\n=========================", $l, $k);
($width:expr; b $l:expr => $k:expr) => {
println!("{:=^width$}\n{}\n{:=<width$}", $l, $k, "", width = $width);
};
//list entry option
(lo $l:expr => $k:expr) => {
($width:expr; lo $l:expr => $k:expr) => {
if let Some(txt) = $k {
println!("{: <20} | {}", $l, txt);
println!("{: <width$} | {}", $l, txt, width = $width);
}
};
//block option
(bo $l:expr => $k:expr) => {
($width:expr; bo $l:expr => $k:expr) => {
if let Some(txt) = $k {
println!("{:=^25}\n{}\n=========================", $l, txt);
println!("{:=^width$}\n{}\n{:=<width$}", $l, txt, "", width = $width);
}
};
($($t:tt $l:expr => $k:expr),+ $(,)?) => {
$(print_table!($t $l => $k);)*
($width:expr; $($t:tt $l:expr => $k:expr),+ $(,)?) => {
$(print_table!($width; $t $l => $k);)*
};
}

View File

@ -105,6 +105,7 @@ async fn main() -> Result<()> {
.collect::<String>();
print_table! {
40;
bo "Raw Json" => if matches.is_present("raw") {Some(raw_response)} else {None},
bo "Description" => none_if_empty!(remove_formatting(&response.description.get_text())),
bo "Extra Description" => {
@ -124,7 +125,7 @@ async fn main() -> Result<()> {
l "Online Players" => response.players.online,
l "Max Players" => response.players.max,
bo "Mods" => if let (Some(mods), true) = (response.modinfo, matches.is_present("mods")) {
Some(get_modlist(mods))
Some(get_modlist(mods, matches.is_present("modversions")))
} else {
None
},
@ -172,12 +173,30 @@ async fn asciify_base64_image(favicon: String, config: AsciiConfig) -> Result<St
Ok(out)
}
fn get_modlist(list: ModInfo) -> String {
match list {
fn get_modlist(list: ModInfo, version_info: bool) -> String {
let infos = match list {
ModInfo::Forge { mod_list: l } => l,
}
.into_iter()
.map(|m| m.modid)
.intersperse("\n".to_owned())
.collect()
};
let max_width = if version_info {
infos
.iter()
.map(|m| m.modid.len())
.max()
.unwrap_or_default()
} else {
0
};
infos
.into_iter()
.map(|m| {
if version_info {
format!("{: <width$} | {}", m.modid, m.version, width = max_width)
} else {
m.modid
}
})
.intersperse("\n".to_owned())
.collect()
}