mirror of
https://github.com/LordMZTE/mcstat.git
synced 2024-05-05 22:21:11 +02:00
add message when the server has mods
and the user doesnt provide the -m arg
This commit is contained in:
parent
739b113e9d
commit
60df88dee1
28
src/main.rs
28
src/main.rs
|
@ -18,6 +18,7 @@ use itertools::Itertools;
|
||||||
use mcstat::{remove_formatting, AsciiConfig};
|
use mcstat::{remove_formatting, AsciiConfig};
|
||||||
use termcolor::{Buffer, BufferWriter, ColorChoice, WriteColor};
|
use termcolor::{Buffer, BufferWriter, ColorChoice, WriteColor};
|
||||||
|
|
||||||
|
/// this message is used if getting a value from the arguments fails
|
||||||
const ARGUMENT_FAIL_MESSAGE: &str = "failed to get value from args";
|
const ARGUMENT_FAIL_MESSAGE: &str = "failed to get value from args";
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
|
@ -38,10 +39,12 @@ async fn main() -> Result<()> {
|
||||||
.parse()
|
.parse()
|
||||||
.context("invalid port")
|
.context("invalid port")
|
||||||
.and_then(|p| {
|
.and_then(|p| {
|
||||||
if p > 0 && p < u16::MAX {
|
// the port must be above 0
|
||||||
|
if p > 0 {
|
||||||
Ok(p)
|
Ok(p)
|
||||||
} else {
|
} else {
|
||||||
Err(anyhow!(ARGUMENT_FAIL_MESSAGE))
|
// this error will be overriden anyways
|
||||||
|
Err(anyhow!(""))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.context("invalid port")?,
|
.context("invalid port")?,
|
||||||
|
@ -102,6 +105,12 @@ async fn main() -> Result<()> {
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region printing
|
// region printing
|
||||||
|
// if the server has mods, and the user hasn't used the -m argument, notify
|
||||||
|
// that.
|
||||||
|
if let (false, Some(_)) = (matches.is_present("mods"), &response.modinfo) {
|
||||||
|
println!("This server has mods. To show them use the -m argument\n")
|
||||||
|
}
|
||||||
|
|
||||||
let player_sample = response
|
let player_sample = response
|
||||||
.players
|
.players
|
||||||
.sample
|
.sample
|
||||||
|
@ -119,7 +128,7 @@ async fn main() -> Result<()> {
|
||||||
let desc = big_desc.extra;
|
let desc = big_desc.extra;
|
||||||
if desc.is_empty() {
|
if desc.is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(desc.into_iter().map(|p| p.text).collect::<String>())
|
Some(desc.into_iter().map(|p| p.text).collect::<String>())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -149,6 +158,9 @@ async fn main() -> Result<()> {
|
||||||
/// returns Err if the base64 image is invalid
|
/// returns Err if the base64 image is invalid
|
||||||
async fn asciify_base64_image(favicon: String, config: AsciiConfig) -> Result<String> {
|
async fn asciify_base64_image(favicon: String, config: AsciiConfig) -> Result<String> {
|
||||||
let img = image_base64::from_base64(favicon);
|
let img = image_base64::from_base64(favicon);
|
||||||
|
// TODO for some reason, image_base64 returns the format as string (and using
|
||||||
|
// regex!) which is useless and also inefficient, so Png is temporarily
|
||||||
|
// hardcoded. we should probably stop using this library
|
||||||
let image =
|
let image =
|
||||||
image::load(Cursor::new(img), ImageFormat::Png).context("image has invalid format")?;
|
image::load(Cursor::new(img), ImageFormat::Png).context("image has invalid format")?;
|
||||||
|
|
||||||
|
@ -173,18 +185,23 @@ async fn asciify_base64_image(favicon: String, config: AsciiConfig) -> Result<St
|
||||||
// only check utf8 format in debug mode
|
// only check utf8 format in debug mode
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
let out = String::from_utf8(bytes).expect("asciifyed image is invalid utf8");
|
let out = String::from_utf8(bytes).expect("asciifyed image is invalid utf8");
|
||||||
#[cfg(not(debug_assertions))]
|
|
||||||
// bytes should always be valid utf8
|
// bytes should always be valid utf8
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
let out = unsafe { String::from_utf8_unchecked(bytes) };
|
let out = unsafe { String::from_utf8_unchecked(bytes) };
|
||||||
|
|
||||||
Ok(out)
|
Ok(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// formats a ModInfo to a readable list of mods
|
||||||
|
///
|
||||||
|
/// if `version_info`, the version of the mods will also be displayed
|
||||||
fn get_modlist(list: ModInfo, version_info: bool) -> String {
|
fn get_modlist(list: ModInfo, version_info: bool) -> String {
|
||||||
let infos = match list {
|
let infos = match list {
|
||||||
ModInfo::Forge { mod_list: l } => l,
|
ModInfo::Forge { mod_list: l } => l,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// the width at which | characters should be placed this is the length of the
|
||||||
|
// longest modid
|
||||||
let max_width = if version_info {
|
let max_width = if version_info {
|
||||||
infos
|
infos
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -192,10 +209,13 @@ fn get_modlist(list: ModInfo, version_info: bool) -> String {
|
||||||
.max()
|
.max()
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
} else {
|
} else {
|
||||||
|
// this will not be used in case version_info is off so we just use 0
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
infos
|
infos
|
||||||
|
// we use into_iter instead of iter because a String cannot be collected from &String
|
||||||
|
// and since we don't need infos again
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|m| {
|
.map(|m| {
|
||||||
if version_info {
|
if version_info {
|
||||||
|
|
Loading…
Reference in a new issue