diff --git a/server/Cargo.toml b/server/Cargo.toml index e49667c..e246a30 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -14,6 +14,8 @@ uuid = {version = "0.8.1", features = ["v4"]} tokio = {version = "0.2", features = ["full"]} server_client = {git = "https://github.com/Mr-Llama-s-Wonderful-Soundboard/server_client.git", branch = "main", features = ["tokio2"]} fallible-iterator = "0.2.0" +regex = "1.4.2" +lazy_static = "1.4.0" # Game loading rhai = {version = "0.19.4", features = ["serde"]} diff --git a/server/src/logger/color_message.rs b/server/src/logger/color_message.rs index bc3a5b1..02a727f 100644 --- a/server/src/logger/color_message.rs +++ b/server/src/logger/color_message.rs @@ -1,19 +1,31 @@ +use lazy_static::lazy_static; +use regex::Regex; use crossterm::{queue, style::Print}; use std::io::Write; +pub const ANSI_RE: &str = + r"[\x1b\x9b]\[[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]"; + +lazy_static! { + pub static ref ANSI_REGEX: Regex = Regex::new(ANSI_RE).unwrap(); +} + pub fn print_line(message: &String, use_colors: bool, stdout: &mut std::io::Stdout) { if use_colors { let mut s = String::new(); let mut highlight = false; + let mut i = 0; + let mut current = "\x1B[0m".to_string(); for c in message.chars() { if c == '`' { // queue!(stdout, Print(format!("{}`", highlight))).unwrap(); if highlight { let styled = format!( - "\x1B[{}m{}\x1B[0m", + "\x1B[{}m{}{}", fern::colors::Color::Blue.to_fg_str(), - s + s, + current ); queue!(stdout, Print(styled)).unwrap(); s = String::new(); @@ -23,8 +35,16 @@ pub fn print_line(message: &String, use_colors: bool, stdout: &mut std::io::Stdo } highlight = !highlight; continue; + }else{ + if !highlight { + if let Some(m) = ANSI_REGEX.find_at(message.as_str(), i) { + if i == m.start() { + current = m.as_str().to_string(); + } + } + } } - + i += 1; s.push(c); } queue!(stdout, Print(&s)).unwrap(); diff --git a/server/src/main.rs b/server/src/main.rs index e554bdd..1b73483 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -12,7 +12,7 @@ async fn main() { server_properties::setup(); let p = server_properties::ServerProperties::load(); let (close, _stdin) = logger::setup(&p).unwrap(); - + info!(target: "setup", "Server name: `{}`", p.name); info!(target: "setup", "Serving on address `{}`", p.addr); // for i in 0..1000 {