From fea1d3f6505795d4506d387d47693ed6e084cb2d Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Sun, 10 Dec 2023 12:37:03 +0100 Subject: [PATCH] Use ANSI Escape sequences to produce a terminal-output if running in terminal --- Cargo.lock | 130 +++++++++++++++++++++++++++++++++++++++------ Cargo.toml | 1 + README.adoc | 4 ++ src/checker/mod.rs | 13 +++++ src/lib.rs | 18 ++++++- src/main.rs | 21 +++++--- 6 files changed, 161 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 801e554..b1607bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -124,6 +124,7 @@ version = "0.1.0" dependencies = [ "async-trait", "chrono", + "console", "dirs", "http", "parse_duration", @@ -148,7 +149,20 @@ dependencies = [ "num-traits", "serde", "wasm-bindgen", - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "console" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "unicode-width", + "windows-sys 0.45.0", ] [[package]] @@ -185,9 +199,15 @@ dependencies = [ "libc", "option-ext", "redox_users", - "windows-sys", + "windows-sys 0.48.0", ] +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "encoding_rs" version = "0.8.33" @@ -441,6 +461,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.150" @@ -493,7 +519,7 @@ checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09" dependencies = [ "libc", "wasi", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -679,7 +705,7 @@ dependencies = [ "libc", "spin", "untrusted", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -824,7 +850,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -914,7 +940,7 @@ dependencies = [ "pin-project-lite", "socket2 0.5.5", "tokio-macros", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -1038,6 +1064,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-width" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" + [[package]] name = "untrusted" version = "0.9.0" @@ -1180,7 +1212,16 @@ version = "0.51.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" dependencies = [ - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets 0.42.2", ] [[package]] @@ -1189,7 +1230,22 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", ] [[package]] @@ -1198,51 +1254,93 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + [[package]] name = "windows_i686_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + [[package]] name = "windows_i686_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" @@ -1265,5 +1363,5 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" dependencies = [ "cfg-if", - "windows-sys", + "windows-sys 0.48.0", ] diff --git a/Cargo.toml b/Cargo.toml index 746ebf9..27fe06c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ members = ["parse_duration"] [dependencies] async-trait = "0.1" chrono = { version = "0.4", features = ["serde"] } +console = "0.15" dirs = "5.0" reqwest = { version = "0.11", features = ["json", "rustls-tls"], default-features = false } serde = { version = "1.0", features = ["derive"] } diff --git a/README.adoc b/README.adoc index 9720337..0e83020 100644 --- a/README.adoc +++ b/README.adoc @@ -87,11 +87,15 @@ down = "#FF0000" The color configuration is optional. If used, all colors must be specified. +Colors are not available if running in terminal mode. + === Execute commands You can also specify a command to be executed when a mouse click occurs on a checked host. Use `click_cmd` to specify the command to be executed, e.g. +This is not available if running in terminal mode. + ---- ... [[checks]] diff --git a/src/checker/mod.rs b/src/checker/mod.rs index 0d8f389..267de6b 100644 --- a/src/checker/mod.rs +++ b/src/checker/mod.rs @@ -3,6 +3,7 @@ mod http; mod tcp; use async_trait::async_trait; +use console::{style, Term}; use std::fmt::{Display, Formatter, Result}; use reqwest::Response; @@ -28,6 +29,18 @@ pub struct CheckResult { impl Display for CheckResult { fn fmt(&self, f: &mut Formatter<'_>) -> Result { + if Term::stdout().is_term() { + return write!( + f, + "{}", + match &self.state { + CheckState::Up => style(&self.name).green(), + CheckState::Warn => style(&self.name).yellow(), + CheckState::Down => style(&self.name).red(), + } + ); + } + let color_config = Config::read().colors; let color = match &self.state { CheckState::Up => color_config.up, diff --git a/src/lib.rs b/src/lib.rs index f539abd..0e7bb7b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ mod checker; mod config; +use console::{style, Term}; +use std::io::Write; use std::process; pub use config::{CheckConfig, Config}; @@ -25,8 +27,21 @@ pub struct ClickEvent { } pub async fn print_states(config: &Config) { - print!("["); let mut entries = vec![]; + if Term::stdout().is_term() { + for check_config in &config.checks { + entries.push(format!("{}", check_host(check_config).await)); + } + entries.push( + chrono::Local::now() + .format(config.time_format.as_str()) + .to_string(), + ); + println!("{}", entries.join(&style(" | ").black().to_string())); + return; + } + + print!("["); for check_config in &config.checks { entries.push(format!("{}", check_host(check_config).await)); } @@ -53,7 +68,6 @@ pub async fn run_click_cmd(cmd: String) { .stdin(process::Stdio::piped()) .spawn() { - use std::io::Write; let _ = child.stdin.as_mut().unwrap().write_all(cmd.as_bytes()); }; } diff --git a/src/main.rs b/src/main.rs index b1796b3..79672a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,23 @@ use checkbar::{get_click_cmd, print_states, read_click_event, run_click_cmd, Config, MouseButton}; +use console::Term; use serde_json::json; use tokio::task; use tokio::time::sleep; #[tokio::main(flavor = "multi_thread", worker_threads = 2)] async fn main() { - println!( - "{}", - json!({ - "version": 1, - "click_events": true - }) - ); - println!("["); + if Term::stdout().is_term() { + let _ = Term::stdout().hide_cursor(); + } else { + println!( + "{}", + json!({ + "version": 1, + "click_events": true + }) + ); + println!("["); + } let inputs = task::spawn(async { loop {