From 83ff13a035e39ca3cbefcaa5b06a2d270f025c2d Mon Sep 17 00:00:00 2001 From: Capucho Date: Mon, 27 Jul 2020 22:49:34 +0100 Subject: [PATCH] Coloring done very badly --- server-cli/src/main.rs | 55 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/server-cli/src/main.rs b/server-cli/src/main.rs index 70f44f65ce..e5849560d4 100644 --- a/server-cli/src/main.rs +++ b/server-cli/src/main.rs @@ -46,28 +46,69 @@ impl<'a> TuiLog<'a> { impl<'a> Write for TuiLog<'a> { fn write(&mut self, buf: &[u8]) -> io::Result { use ansi_parser::{AnsiParser, AnsiSequence, Output}; - use tui::text::{Span,Spans}; + use tui::{ + style::{Color, Modifier}, + text::{Span, Spans}, + }; let line = String::from_utf8(buf.into()) .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; let mut spans = Vec::new(); + let mut span = Span::raw(""); for out in line.ansi_parse() { match out { - Output::TextBlock(text) => spans.push(text.to_string().into()), - Output::Escape(seq) => info!("{:?}",seq) + Output::TextBlock(text) => { + span.content = format!("{}{}", span.content.to_owned(), text).into() + }, + Output::Escape(seq) => { + if span.content.len() != 0 { + spans.push(span); + + span = Span::raw(""); + } + + match seq { + AnsiSequence::SetGraphicsMode(values) => { + const COLOR_TABLE: [Color; 8] = [ + Color::Black, + Color::Red, + Color::Green, + Color::Yellow, + Color::Blue, + Color::Magenta, + Color::Cyan, + Color::White, + ]; + + let mut iter = values.iter(); + + match iter.next().unwrap() { + 0 => {}, + 2 => span.style.add_modifier = Modifier::DIM, + idx @ 30..=37 => { + span.style.fg = Some(COLOR_TABLE[(idx - 30) as usize]) + }, + _ => println!("{:#?}", values), + } + }, + _ => println!("{:#?}", seq), + } + }, } } + if span.content.len() != 0 { + spans.push(span); + } + self.inner.lock().unwrap().lines.push(Spans(spans)); Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { - Ok(()) - } + fn flush(&mut self) -> io::Result<()> { Ok(()) } } #[derive(Debug, Copy, Clone)] @@ -148,8 +189,6 @@ fn main() -> io::Result<()> { use crossterm::event::{KeyModifiers, *}; if poll(Duration::from_millis(10)).unwrap() { - // It's guaranteed that the `read()` won't block when the `poll()` - // function returns `true` match read().unwrap() { Event::Key(event) => match event.code { KeyCode::Char('c') => {