mcstat: added support for mod lists

use fork of async-minecraft-ping
implement support for extra description
all servers with motd should have it shown now
This commit is contained in:
LordMZTE 2020-10-09 15:21:43 +02:00
parent fe5dc29941
commit 1779273659
5 changed files with 41 additions and 7 deletions

View File

@ -11,7 +11,7 @@ clap = { version = "^2.33", features = ["yaml"] }
image-base64 = "0.1.0"
image = "0.23.9"
asciify = "0.1.6"
async-minecraft-ping = "0.2.1"
async-minecraft-ping = { git="https://github.com/LordMZTE/async-minecraft-ping.git", tag="v0.2.2" }
tokio = { version = "0.2.22", features = ["full"] }
itertools = "0.9.0"
termcolor = "1"

View File

@ -9,4 +9,4 @@
- favicon shown as ascii art
## TODO
---
- [ ] fork async-minecraft-ping in order to fix some bugs and implement mod list response (dev is not responding to [issue](https://github.com/jsvana/async-minecraft-ping/issues/3))
- [x] fork async-minecraft-ping in order to fix some bugs and implement mod list response (dev is not responding to [issue](https://github.com/jsvana/async-minecraft-ping/issues/3))

View File

@ -23,6 +23,12 @@ args:
help: "the time before the server ping times out in milliseconds"
takes_value: true
default_value: "500"
- raw:
short: r
help: "if supplied, the raw json response from the server will be printed"
- mods:
short: m
help: "if supplied, a mod list will be printed"
# IMAGE ARGS
# TODO due to a bug in clap, the image argument is always required because size has a default value

View File

@ -18,7 +18,7 @@ macro_rules! print_table {
//block
(b $l:expr => $k:expr) => {
println!("{:=^25}\n{}\n=========================\n", $l, $k);
println!("{:=^25}\n{}\n=========================", $l, $k);
};
//list entry option
@ -31,7 +31,7 @@ macro_rules! print_table {
//block option
(bo $l:expr => $k:expr) => {
if let Some(txt) = $k {
println!("{:=^25}\n{}\n=========================\n", $l, txt);
println!("{:=^25}\n{}\n=========================", $l, txt);
}
};

View File

@ -11,7 +11,7 @@ use tokio::time;
use anyhow::{Context, Result};
use asciify::AsciiBuilder;
use async_minecraft_ping::ConnectionConfig;
use async_minecraft_ping::{ConnectionConfig, ModInfo, ServerDescription};
use clap::App;
use image::ImageFormat;
use itertools::Itertools;
@ -63,7 +63,7 @@ async fn main() -> Result<()> {
.context("timeout is invalid value")?,
));
let response = tokio::select! {
let (response, raw_response) = tokio::select! {
_ = &mut timeout => Err(anyhow!("Connection to server timed out")),
r = async {
let mut con = config.connect().await?;
@ -105,11 +105,29 @@ async fn main() -> Result<()> {
.collect::<String>();
print_table! {
bo "Description" => none_if_empty!(remove_formatting(&response.description.text)),
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" => {
if let ServerDescription::Big(big_desc) = response.description {
let desc = big_desc.extra;
if desc.is_empty() {
None
} else {
Some(desc.into_iter().map(|p| p.text).collect::<String>())
}
} else {
None
}
},
bo "Player Sample" => none_if_empty!(remove_formatting(&player_sample)),
lo "Server Version" => none_if_empty!(remove_formatting(&response.version.name)),
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))
} else {
None
},
l "Server Protocol" => response.version.protocol,
};
@ -153,3 +171,13 @@ async fn asciify_base64_image(favicon: String, config: AsciiConfig) -> Result<St
Ok(out)
}
fn get_modlist(list: ModInfo) -> String {
match list {
ModInfo::Forge { mod_list: l } => l,
}
.into_iter()
.map(|m| m.modid)
.intersperse("\n".to_owned())
.collect()
}