mirror of
https://github.com/pcvolkmer/checkbar.git
synced 2025-04-19 19:16:50 +00:00
feat: add terminal feature based display implementation
This commit is contained in:
parent
5015459966
commit
c2358d5ce8
@ -1,11 +1,7 @@
|
|||||||
mod actuator;
|
use std::fmt::{Display, Formatter, Result};
|
||||||
mod http;
|
|
||||||
mod tcp;
|
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use console::{style, Term};
|
use console::{style, Term};
|
||||||
use std::fmt::{Display, Formatter, Result};
|
|
||||||
|
|
||||||
use reqwest::Response;
|
use reqwest::Response;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
@ -14,6 +10,10 @@ pub use crate::checker::http::Checker as HttpChecker;
|
|||||||
pub use crate::checker::tcp::Checker as TcpChecker;
|
pub use crate::checker::tcp::Checker as TcpChecker;
|
||||||
use crate::config::{CheckConfig, CheckType, Config};
|
use crate::config::{CheckConfig, CheckType, Config};
|
||||||
|
|
||||||
|
mod actuator;
|
||||||
|
mod http;
|
||||||
|
mod tcp;
|
||||||
|
|
||||||
pub async fn check_host(check_config: &CheckConfig) -> CheckResult {
|
pub async fn check_host(check_config: &CheckConfig) -> CheckResult {
|
||||||
match check_config.check_type {
|
match check_config.check_type {
|
||||||
Some(CheckType::Actuator) => ActuatorChecker::new(check_config).check().await,
|
Some(CheckType::Actuator) => ActuatorChecker::new(check_config).check().await,
|
||||||
@ -22,25 +22,25 @@ pub async fn check_host(check_config: &CheckConfig) -> CheckResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait ToNonTerminalString: ToString {
|
||||||
|
fn to_string(&self) -> String;
|
||||||
|
}
|
||||||
|
trait ToNonColoredTerminalString: ToString {
|
||||||
|
fn to_string(&self) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
|
trait ToColoredTerminalString: ToString {
|
||||||
|
fn to_string(&self) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
pub struct CheckResult {
|
pub struct CheckResult {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub state: CheckState,
|
pub state: CheckState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for CheckResult {
|
impl ToNonTerminalString for CheckResult {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
#[inline]
|
||||||
if Term::stdout().is_term() {
|
fn to_string(&self) -> String {
|
||||||
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_config = Config::read().colors;
|
||||||
let color = match &self.state {
|
let color = match &self.state {
|
||||||
CheckState::Up => color_config.up,
|
CheckState::Up => color_config.up,
|
||||||
@ -48,8 +48,7 @@ impl Display for CheckResult {
|
|||||||
CheckState::Down => color_config.down,
|
CheckState::Down => color_config.down,
|
||||||
};
|
};
|
||||||
|
|
||||||
write!(
|
format!(
|
||||||
f,
|
|
||||||
"{}",
|
"{}",
|
||||||
json!({
|
json!({
|
||||||
"full_text": self.name,
|
"full_text": self.name,
|
||||||
@ -61,6 +60,39 @@ impl Display for CheckResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ToNonColoredTerminalString for CheckResult {
|
||||||
|
#[inline]
|
||||||
|
fn to_string(&self) -> String {
|
||||||
|
self.name.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToColoredTerminalString for CheckResult {
|
||||||
|
#[inline]
|
||||||
|
fn to_string(&self) -> String {
|
||||||
|
format!(
|
||||||
|
"{}",
|
||||||
|
match &self.state {
|
||||||
|
CheckState::Up => style(&self.name).green().force_styling(true),
|
||||||
|
CheckState::Warn => style(&self.name).yellow().force_styling(true),
|
||||||
|
CheckState::Down => style(&self.name).red().force_styling(true),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for CheckResult {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
||||||
|
let term = Term::stdout();
|
||||||
|
if term.is_term() && term.features().colors_supported() {
|
||||||
|
return write!(f, "{}", ToColoredTerminalString::to_string(self));
|
||||||
|
} else if term.is_term() && !term.features().colors_supported() {
|
||||||
|
return write!(f, "{}", ToNonColoredTerminalString::to_string(self));
|
||||||
|
}
|
||||||
|
write!(f, "{}", ToNonTerminalString::to_string(self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum CheckState {
|
pub enum CheckState {
|
||||||
Up,
|
Up,
|
||||||
@ -89,6 +121,75 @@ pub trait HttpBasedChecker {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use crate::checker::*;
|
use crate::checker::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_should_display_check_result_up_in_term() {
|
||||||
|
let check_result = CheckResult {
|
||||||
|
name: "test".to_string(),
|
||||||
|
state: CheckState::Up,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(ToNonColoredTerminalString::to_string(&check_result), "test")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_should_display_check_result_warn_in_term() {
|
||||||
|
let check_result = CheckResult {
|
||||||
|
name: "test".to_string(),
|
||||||
|
state: CheckState::Warn,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(ToNonColoredTerminalString::to_string(&check_result), "test")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_should_display_check_result_down_in_term() {
|
||||||
|
let check_result = CheckResult {
|
||||||
|
name: "test".to_string(),
|
||||||
|
state: CheckState::Down,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(ToNonColoredTerminalString::to_string(&check_result), "test")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_should_display_check_result_up_in_colored_term() {
|
||||||
|
let check_result = CheckResult {
|
||||||
|
name: "test".to_string(),
|
||||||
|
state: CheckState::Up,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
ToColoredTerminalString::to_string(&check_result),
|
||||||
|
"\u{1b}[32mtest\u{1b}[0m"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_should_display_check_result_warn_in_colored_term() {
|
||||||
|
let check_result = CheckResult {
|
||||||
|
name: "test".to_string(),
|
||||||
|
state: CheckState::Warn,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
ToColoredTerminalString::to_string(&check_result),
|
||||||
|
"\u{1b}[33mtest\u{1b}[0m"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_should_display_check_result_down_in_colored_term() {
|
||||||
|
let check_result = CheckResult {
|
||||||
|
name: "test".to_string(),
|
||||||
|
state: CheckState::Down,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
ToColoredTerminalString::to_string(&check_result),
|
||||||
|
"\u{1b}[31mtest\u{1b}[0m"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_should_display_check_result_up() {
|
fn test_should_display_check_result_up() {
|
||||||
let check_result = CheckResult {
|
let check_result = CheckResult {
|
||||||
@ -96,14 +197,10 @@ mod tests {
|
|||||||
state: CheckState::Up,
|
state: CheckState::Up,
|
||||||
};
|
};
|
||||||
|
|
||||||
if Term::stdout().is_term() {
|
assert_eq!(
|
||||||
assert_eq!(check_result.to_string(), "\u{1b}[32mtest\u{1b}[0m")
|
ToNonTerminalString::to_string(&check_result),
|
||||||
} else {
|
r##"{"color":"#00FF00","full_text":"test","name":"test","separator_block_width":16}"##
|
||||||
assert_eq!(
|
)
|
||||||
check_result.to_string(),
|
|
||||||
r##"{"color":"#00FF00","full_text":"test","name":"test","separator_block_width":16}"##
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -113,14 +210,10 @@ mod tests {
|
|||||||
state: CheckState::Warn,
|
state: CheckState::Warn,
|
||||||
};
|
};
|
||||||
|
|
||||||
if Term::stdout().is_term() {
|
assert_eq!(
|
||||||
assert_eq!(check_result.to_string(), "\u{1b}[33mtest\u{1b}[0m")
|
ToNonTerminalString::to_string(&check_result),
|
||||||
} else {
|
r##"{"color":"#FFFF00","full_text":"test","name":"test","separator_block_width":16}"##
|
||||||
assert_eq!(
|
)
|
||||||
check_result.to_string(),
|
|
||||||
r##"{"color":"#FFFF00","full_text":"test","name":"test","separator_block_width":16}"##
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -130,13 +223,9 @@ mod tests {
|
|||||||
state: CheckState::Down,
|
state: CheckState::Down,
|
||||||
};
|
};
|
||||||
|
|
||||||
if Term::stdout().is_term() {
|
assert_eq!(
|
||||||
assert_eq!(check_result.to_string(), "\u{1b}[31mtest\u{1b}[0m")
|
ToNonTerminalString::to_string(&check_result),
|
||||||
} else {
|
r##"{"color":"#FF0000","full_text":"test","name":"test","separator_block_width":16}"##
|
||||||
assert_eq!(
|
)
|
||||||
check_result.to_string(),
|
|
||||||
r##"{"color":"#FF0000","full_text":"test","name":"test","separator_block_width":16}"##
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user