From 0f3717d0473d5f5326a2dde37d47939023f8b4f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=A4rtens?= Date: Sun, 9 May 2021 18:53:25 +0200 Subject: [PATCH] enable TUI to actually handle newline character. but we are not sure if NEWLINEs are wanted in logs as it could cause problems with automated parsing. --- server-cli/src/tuilog.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/server-cli/src/tuilog.rs b/server-cli/src/tuilog.rs index cb2bd0ea41..09b12e00e8 100644 --- a/server-cli/src/tuilog.rs +++ b/server-cli/src/tuilog.rs @@ -32,11 +32,24 @@ impl<'a> Write for TuiLog<'a> { let mut spans = Vec::new(); let mut span = Span::raw(""); + let mut lines = Vec::new(); for out in line.ansi_parse() { match out { - Output::TextBlock(text) => { - span.content = format!("{}{}", span.content.to_owned(), text).into() + Output::TextBlock(mut text) => { + // search for newlines (.lines() or .split doesn't work, as this block does not + // need to contain one) + while let Some(newline) = text.find('\n') { + span.content.to_mut().push_str(&text[..newline]); + if span.content.len() != 0 { + spans.push(span); + lines.push(spans); + spans = Vec::new(); + span = Span::raw(""); + } + text = &text[newline + 1..]; + } + span.content.to_mut().push_str(text); }, Output::Escape(seq) => { if span.content.len() != 0 { @@ -79,8 +92,12 @@ impl<'a> Write for TuiLog<'a> { if span.content.len() != 0 { spans.push(span); } + if !spans.is_empty() { + lines.push(spans); + } - self.inner.lock().unwrap().lines.push(Spans(spans)); + let mut lines = lines.into_iter().map(Spans).collect::>(); + self.inner.lock().unwrap().lines.append(&mut lines); Ok(buf.len()) }