1
0
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:
2023-12-10 12:37:03 +01:00
parent 8ff43775aa
commit fea1d3f650
6 changed files with 161 additions and 26 deletions

View File

@ -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,

View File

@ -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());
};
}

View File

@ -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 {