Browse Source

Fix highlighting issues

new_protocol
ThePerkinrex 5 years ago
parent
commit
0f05d97edf
No known key found for this signature in database GPG Key ID: 1F45A7C4BFB41607
  1. 2
      server/Cargo.toml
  2. 26
      server/src/logger/color_message.rs

2
server/Cargo.toml

@ -14,6 +14,8 @@ uuid = {version = "0.8.1", features = ["v4"]}
tokio = {version = "0.2", features = ["full"]} tokio = {version = "0.2", features = ["full"]}
server_client = {git = "https://github.com/Mr-Llama-s-Wonderful-Soundboard/server_client.git", branch = "main", features = ["tokio2"]} server_client = {git = "https://github.com/Mr-Llama-s-Wonderful-Soundboard/server_client.git", branch = "main", features = ["tokio2"]}
fallible-iterator = "0.2.0" fallible-iterator = "0.2.0"
regex = "1.4.2"
lazy_static = "1.4.0"
# Game loading # Game loading
rhai = {version = "0.19.4", features = ["serde"]} rhai = {version = "0.19.4", features = ["serde"]}

26
server/src/logger/color_message.rs

@ -1,19 +1,31 @@
use lazy_static::lazy_static;
use regex::Regex;
use crossterm::{queue, style::Print}; use crossterm::{queue, style::Print};
use std::io::Write; 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) { pub fn print_line(message: &String, use_colors: bool, stdout: &mut std::io::Stdout) {
if use_colors { if use_colors {
let mut s = String::new(); let mut s = String::new();
let mut highlight = false; let mut highlight = false;
let mut i = 0;
let mut current = "\x1B[0m".to_string();
for c in message.chars() { for c in message.chars() {
if c == '`' { if c == '`' {
// queue!(stdout, Print(format!("{}`", highlight))).unwrap(); // queue!(stdout, Print(format!("{}`", highlight))).unwrap();
if highlight { if highlight {
let styled = format!( let styled = format!(
"\x1B[{}m{}\x1B[0m", "\x1B[{}m{}{}",
fern::colors::Color::Blue.to_fg_str(), fern::colors::Color::Blue.to_fg_str(),
s s,
current
); );
queue!(stdout, Print(styled)).unwrap(); queue!(stdout, Print(styled)).unwrap();
s = String::new(); s = String::new();
@ -23,8 +35,16 @@ pub fn print_line(message: &String, use_colors: bool, stdout: &mut std::io::Stdo
} }
highlight = !highlight; highlight = !highlight;
continue; 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); s.push(c);
} }
queue!(stdout, Print(&s)).unwrap(); queue!(stdout, Print(&s)).unwrap();

Loading…
Cancel
Save