mirror of
https://github.com/pcvolkmer/checkbar.git
synced 2025-07-02 06:22:53 +00:00
Use ANSI Escape sequences to produce a terminal-output if running in terminal
This commit is contained in:
@ -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,
|
||||
|
18
src/lib.rs
18
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());
|
||||
};
|
||||
}
|
||||
|
21
src/main.rs
21
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 {
|
||||
|
Reference in New Issue
Block a user