raw output will now display nothing else

fmt
This commit is contained in:
LordMZTE 2020-10-09 19:56:37 +02:00
parent 2673036cb0
commit d16af9e11b
3 changed files with 29 additions and 20 deletions

View file

@ -11,8 +11,9 @@ clap = { version = "^2.33", features = ["yaml"] }
image-base64 = "0.1.0" image-base64 = "0.1.0"
image = "0.23.9" image = "0.23.9"
asciify = "0.1.6" asciify = "0.1.6"
async-minecraft-ping = { git="https://github.com/LordMZTE/async-minecraft-ping.git", tag="v0.2.2" } async-minecraft-ping = { path="../../../async-minecraft-ping/async-minecraft-ping" }
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" anyhow = "1.0.32"
serde_json = "1.0.58"

View file

@ -2,12 +2,12 @@ use asciify::AsciiBuilder;
/// prints a table with the entries supplied /// prints a table with the entries supplied
/// the identifier at the start of each entry sets the type /// the identifier at the start of each entry sets the type
/// ///
/// l = list entry /// l = list entry
/// b = block /// b = block
/// lo = list entry option /// lo = list entry option
/// bo = block option /// bo = block option
/// ///
/// options are checked if they are `Some` and won't be printed if they aren't /// options are checked if they are `Some` and won't be printed if they aren't
#[macro_export] #[macro_export]
macro_rules! print_table { macro_rules! print_table {
@ -42,7 +42,8 @@ macro_rules! print_table {
/// returns an `Option` of the expression passed in /// returns an `Option` of the expression passed in
/// `None` if the `is_empty` on the expression returns true, `Some(x)` otherwise /// `None` if the `is_empty` on the expression returns true, `Some(x)` otherwise
/// this is a macro and not a function because `is_empty` is not defined in any trait /// this is a macro and not a function because `is_empty` is not defined in any
/// trait
#[macro_export] #[macro_export]
macro_rules! none_if_empty { macro_rules! none_if_empty {
($x:expr) => { ($x:expr) => {

View file

@ -11,7 +11,7 @@ use tokio::time;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use asciify::AsciiBuilder; use asciify::AsciiBuilder;
use async_minecraft_ping::{ConnectionConfig, ModInfo, ServerDescription}; use async_minecraft_ping::{ConnectionConfig, ModInfo, ServerDescription, StatusResponse};
use clap::App; use clap::App;
use image::ImageFormat; use image::ImageFormat;
use itertools::Itertools; use itertools::Itertools;
@ -24,7 +24,7 @@ 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();
//region Network // region Network
let config = ConnectionConfig::build( let config = ConnectionConfig::build(
matches matches
.value_of("ip") .value_of("ip")
@ -54,7 +54,7 @@ async fn main() -> Result<()> {
.context("invalid protocol version")?, .context("invalid protocol version")?,
); );
//create timeout for server connection // create timeout for server connection
let mut timeout = time::delay_for(Duration::from_millis( let mut timeout = time::delay_for(Duration::from_millis(
matches matches
.value_of("timeout") .value_of("timeout")
@ -63,16 +63,23 @@ async fn main() -> Result<()> {
.context("timeout is invalid value")?, .context("timeout is invalid value")?,
)); ));
let (response, raw_response) = tokio::select! { let raw_response = tokio::select! {
_ = &mut timeout => Err(anyhow!("Connection to server timed out")), _ = &mut timeout => Err(anyhow!("Connection to server timed out")),
r = async { r = async {
let mut con = config.connect().await?; let mut con = config.connect().await?;
con.status().await con.status_raw().await
} => r, } => r,
}?; }?;
//endregion
//region Image if matches.is_present("raw") {
println!("{}", raw_response);
return Ok(());
}
let response = serde_json::from_str::<StatusResponse>(&raw_response)?;
// endregion
// region Image
let image_size: u32 = matches let image_size: u32 = matches
.value_of("size") .value_of("size")
.context("failed to get value from args")? .context("failed to get value from args")?
@ -81,7 +88,7 @@ async fn main() -> Result<()> {
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
image = Some(tokio::spawn(asciify_base64_image( image = Some(tokio::spawn(asciify_base64_image(
favicon, favicon,
AsciiConfig { AsciiConfig {
@ -92,9 +99,9 @@ async fn main() -> Result<()> {
}, },
))); )));
} }
//endregion // endregion
//region printing // region printing
let player_sample = response let player_sample = response
.players .players
.sample .sample
@ -106,7 +113,6 @@ async fn main() -> Result<()> {
print_table! { print_table! {
40; 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 "Description" => none_if_empty!(remove_formatting(&response.description.get_text())),
bo "Extra Description" => { bo "Extra Description" => {
if let ServerDescription::Big(big_desc) = response.description { if let ServerDescription::Big(big_desc) = response.description {
@ -135,7 +141,7 @@ async fn main() -> Result<()> {
if let Some(img) = image { if let Some(img) = image {
println!("\n{}", img.await??); println!("\n{}", img.await??);
} }
//endregion // endregion
Ok(()) Ok(())
} }
@ -149,7 +155,8 @@ async fn asciify_base64_image(favicon: String, config: AsciiConfig) -> Result<St
let builder = config.apply(AsciiBuilder::new_from_image(image)); let builder = config.apply(AsciiBuilder::new_from_image(image));
let mut buf = if config.colored { let mut buf = if config.colored {
//this does not write to stdout but just gets the correct color information for stdout // this does not write to stdout but just gets the correct color
// information for stdout
let mut buf = BufferWriter::stdout(ColorChoice::Always).buffer(); let mut buf = BufferWriter::stdout(ColorChoice::Always).buffer();
builder.to_stream_colored(&mut buf); builder.to_stream_colored(&mut buf);
buf buf
@ -158,16 +165,16 @@ async fn asciify_base64_image(favicon: String, config: AsciiConfig) -> Result<St
builder.to_stream(&mut buf); builder.to_stream(&mut buf);
buf buf
}; };
//reset color // reset color
buf.reset()?; buf.reset()?;
let bytes = buf.as_slice().to_vec(); let bytes = buf.as_slice().to_vec();
//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))] #[cfg(not(debug_assertions))]
//bytes should always be valid utf8 // bytes should always be valid utf8
let out = unsafe { String::from_utf8_unchecked(bytes) }; let out = unsafe { String::from_utf8_unchecked(bytes) };
Ok(out) Ok(out)