mirror of
https://github.com/LordMZTE/mcstat.git
synced 2024-05-13 04:29:45 +02:00
use anyhow for error handling
fix argument bug
This commit is contained in:
parent
f91ea5a845
commit
9b50328940
|
@ -15,3 +15,4 @@ async-minecraft-ping = "0.2.1"
|
||||||
tokio = { version = "0.2.22", features = ["full"] }
|
tokio = { version = "0.2.22", features = ["full"] }
|
||||||
itertools = "0.9.0"
|
itertools = "0.9.0"
|
||||||
termcolor = "1"
|
termcolor = "1"
|
||||||
|
anyhow = "1.0.32"
|
||||||
|
|
17
src/args.yml
17
src/args.yml
|
@ -19,28 +19,33 @@ args:
|
||||||
takes_value: true
|
takes_value: true
|
||||||
|
|
||||||
# IMAGE ARGS
|
# IMAGE ARGS
|
||||||
|
# TODO due to a bug in clap, the image argument is always required because size has a default value
|
||||||
- image:
|
- image:
|
||||||
short: i
|
short: "i"
|
||||||
help: "if the server's favicon should be printed as ASCII art"
|
help: "if the server's favicon should be printed as ASCII art"
|
||||||
|
required: false
|
||||||
- color:
|
- color:
|
||||||
short: c
|
short: "c"
|
||||||
help: "if the favicon image should be printed with ANSI color formatting or monochrome"
|
help: "if the favicon image should be printed with ANSI color formatting or monochrome"
|
||||||
- size:
|
- size:
|
||||||
short: s
|
short: "s"
|
||||||
help: "the size of the image"
|
help: "the size of the image"
|
||||||
takes_value: true
|
takes_value: true
|
||||||
default_value: "16"
|
default_value: "16"
|
||||||
- deep:
|
- deep:
|
||||||
short: d
|
short: "d"
|
||||||
help: "if provided the ascii image will have more different characters"
|
help: "if provided the ascii image will have more different characters"
|
||||||
- invert:
|
- invert:
|
||||||
short: n
|
short: "n"
|
||||||
help: "inverts the ascii image thickness"
|
help: "inverts the ascii image thickness"
|
||||||
|
|
||||||
groups:
|
groups:
|
||||||
- img-flags:
|
- img-flags:
|
||||||
requires: "image"
|
# TODO uncomment unce bug is fixed
|
||||||
|
# requires:
|
||||||
|
# - image
|
||||||
multiple: true
|
multiple: true
|
||||||
|
required: false
|
||||||
args:
|
args:
|
||||||
- color
|
- color
|
||||||
- size
|
- size
|
||||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -1,9 +1,9 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
|
|
||||||
use std::error::Error;
|
|
||||||
use std::io::{Cursor, Write};
|
use std::io::{Cursor, Write};
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
use asciify::AsciiBuilder;
|
use asciify::AsciiBuilder;
|
||||||
use async_minecraft_ping::ConnectionConfig;
|
use async_minecraft_ping::ConnectionConfig;
|
||||||
use clap::App;
|
use clap::App;
|
||||||
|
@ -12,7 +12,7 @@ use itertools::Itertools;
|
||||||
use termcolor::{Buffer, BufferWriter, ColorChoice, WriteColor};
|
use termcolor::{Buffer, BufferWriter, ColorChoice, WriteColor};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<()> {
|
||||||
let yaml = load_yaml!("args.yml");
|
let yaml = load_yaml!("args.yml");
|
||||||
let matches = App::from_yaml(yaml).get_matches();
|
let matches = App::from_yaml(yaml).get_matches();
|
||||||
|
|
||||||
|
@ -25,14 +25,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
.parse()
|
.parse()
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|p| if p > 0 && p < u16::MAX { Some(p) } else { None })
|
.and_then(|p| if p > 0 && p < u16::MAX { Some(p) } else { None })
|
||||||
.expect("invalid port"),
|
.context("invalid port")?,
|
||||||
)
|
)
|
||||||
.with_protocol_version(
|
.with_protocol_version(
|
||||||
matches
|
matches
|
||||||
.value_of("protocol-version")
|
.value_of("protocol-version")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.parse()
|
.parse()
|
||||||
.expect("invalid protocol version"),
|
.context("invalid protocol version")?,
|
||||||
);
|
);
|
||||||
let mut connection = config.connect().await?;
|
let mut connection = config.connect().await?;
|
||||||
let response = connection.status().await?;
|
let response = connection.status().await?;
|
||||||
|
@ -43,7 +43,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
.value_of("size")
|
.value_of("size")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.parse()
|
.parse()
|
||||||
.expect("image size must be number");
|
.with_context(|| "image size must be number")?;
|
||||||
let mut image = None;
|
let mut image = None;
|
||||||
if let (Some(favicon), true) = (response.favicon, matches.is_present("image")) {
|
if let (Some(favicon), true) = (response.favicon, matches.is_present("image")) {
|
||||||
//The image parsing and asciifying is done while the table is printing
|
//The image parsing and asciifying is done while the table is printing
|
||||||
|
@ -108,7 +108,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let stdout = std::io::stdout();
|
let stdout = std::io::stdout();
|
||||||
let mut handle = stdout.lock();
|
let mut handle = stdout.lock();
|
||||||
handle.write_all(&[b'\n'])?;
|
handle.write_all(&[b'\n'])?;
|
||||||
handle.write_all(&img.await?)?;
|
handle.write_all(&img.await??)?;
|
||||||
}
|
}
|
||||||
//endregion
|
//endregion
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -129,10 +129,10 @@ fn remove_formatting(s: &str) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// returns the asciifyed image as UTF-8 bytes
|
/// returns the asciifyed image as UTF-8 bytes
|
||||||
async fn get_image(favicon: String, config: AsciiConfig) -> Vec<u8> {
|
async fn get_image(favicon: String, config: AsciiConfig) -> Result<Vec<u8>> {
|
||||||
let img = image_base64::from_base64(favicon);
|
let img = image_base64::from_base64(favicon);
|
||||||
let image =
|
let image =
|
||||||
image::load(Cursor::new(img), ImageFormat::Png).expect("favicon has invalid format");
|
image::load(Cursor::new(img), ImageFormat::Png).context("favicon has invalid format")?;
|
||||||
|
|
||||||
let builder = config.apply(AsciiBuilder::new_from_image(image));
|
let builder = config.apply(AsciiBuilder::new_from_image(image));
|
||||||
|
|
||||||
|
@ -146,8 +146,8 @@ async fn get_image(favicon: String, config: AsciiConfig) -> Vec<u8> {
|
||||||
builder.to_stream(&mut buf);
|
builder.to_stream(&mut buf);
|
||||||
buf
|
buf
|
||||||
};
|
};
|
||||||
buf.reset().unwrap();
|
buf.reset()?;
|
||||||
buf.as_slice().to_vec()
|
Ok(buf.as_slice().to_vec())
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AsciiConfig {
|
struct AsciiConfig {
|
||||||
|
|
Loading…
Reference in a new issue