Streaming log page with colors

Using the ansi-to-html crate.
This commit is contained in:
2023-06-06 21:14:29 -04:00
parent 6713a7a440
commit 786f3a194f
7 changed files with 50 additions and 36 deletions

View File

@@ -1,13 +1,15 @@
use std::convert::Infallible;
use std::str::from_utf8;
use std::time::Duration;
use ansi_to_html::convert_escaped;
use axum::extract::State;
use axum::response::{
sse::{Event, Sse},
Response,
};
use bytes::Bytes;
use maud::html;
use maud::{html, PreEscaped};
use tokio::sync::watch::Receiver;
use tokio_stream::wrappers::WatchStream;
use tokio_stream::Stream;
@@ -21,7 +23,7 @@ pub async fn get(layout: Layout) -> Result<Response> {
let mem_buf = MEM_LOG.lock().unwrap();
Ok(layout.render(html! {
turbo-stream-source src="/log/stream" {}
pre id="log" { (std::str::from_utf8(mem_buf.as_slices().0).unwrap()) }
pre id="log" { (PreEscaped(convert_escaped(from_utf8(mem_buf.as_slices().0).unwrap()).unwrap())) }
}))
}
@@ -30,13 +32,16 @@ pub async fn stream(
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
let log_stream = WatchStream::new(log_receiver);
let log_stream = log_stream.map(|line| {
Ok(Event::default().data(html! {
turbo-stream action="append" target="log" {
template {
(std::str::from_utf8(&line).unwrap())
Ok(Event::default().data(
html! {
turbo-stream action="append" target="log" {
template {
(PreEscaped(convert_escaped(from_utf8(&line).unwrap()).unwrap()))
}
}
}
}.into_string()))
.into_string(),
))
});
Sse::new(log_stream).keep_alive(
axum::response::sse::KeepAlive::new()