html terminal
Diffstat (limited to 'src/process.rs')
| -rw-r--r-- | src/process.rs | 51 |
1 files changed, 37 insertions, 14 deletions
diff --git a/src/process.rs b/src/process.rs index de30047..80113db 100644 --- a/src/process.rs +++ b/src/process.rs @@ -1,15 +1,19 @@ use ansi_to_html::convert_escaped; use std::process::Stdio; +use std::sync::Arc; use std::{ffi::OsString, time::Duration}; use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader}; use tokio::process::{Child, ChildStdin, ChildStdout, Command}; use tokio::sync::broadcast; use tokio::sync::broadcast::error::TryRecvError; use tokio::task::JoinHandle; + +use crate::server::State; pub struct Process { _inner: Child, input: Option<broadcast::Receiver<String>>, - output: Option<broadcast::Sender<String>>, + html_output: Option<broadcast::Sender<String>>, + plain_output: Option<broadcast::Sender<String>>, stdout: BufReader<ChildStdout>, stdin: ChildStdin, } @@ -32,37 +36,48 @@ impl Process { stdin: p.stdin.take().unwrap(), _inner: p, input: None, - output: None, + html_output: None, + plain_output: None, } } pub fn input(mut self, input: broadcast::Receiver<String>) -> Self { self.input = Some(input); - return self; + self + } + + pub fn html_output(mut self, output: broadcast::Sender<String>) -> Self { + self.html_output = Some(output); + self } - pub fn output(mut self, output: broadcast::Sender<String>) -> Self { - self.output = Some(output); - return self; + pub fn plain_output(mut self, output: broadcast::Sender<String>) -> Self { + self.plain_output = Some(output); + self + } + + pub fn with_state(self, state: &Arc<State>) -> Self { + self.html_output(state.stdout_html.clone()) + .plain_output(state.stdout_plain.clone()) } pub fn link(mut self) -> JoinHandle<()> { define_print!("process"); let mut input = self.input.unwrap(); - let output = self.output.unwrap(); + let html_output = self.html_output.unwrap(); + let plain_output = self.plain_output.unwrap(); tokio::spawn(async move { let mut stdout = [0; 4096]; loop { - nextiter!(); - if output.receiver_count() == 0 { + if html_output.receiver_count() + plain_output.receiver_count() == 0 { async_std::task::sleep(Duration::from_millis(500)).await; - cont!(); + continue; } match input.try_recv() { Err(e) => match e { - TryRecvError::Empty => noinput!(), TryRecvError::Closed => fail!("closed"), - TryRecvError::Lagged(_) => noinput!("lagged"), + TryRecvError::Lagged(_) => continue, + TryRecvError::Empty => {} }, Ok(mut s) => { input!("{s}"); @@ -75,13 +90,21 @@ impl Process { let string = { let n = tokio::select! { n = {self.stdout.read(&mut stdout)} => n.unwrap(), - _ = async_std::task::sleep(Duration::from_millis(500)) => cont!() + _ = async_std::task::sleep(Duration::from_millis(500)) => continue, }; String::from_utf8_lossy(&stdout[..n]).into_owned() }; for line in string.lines() { output!("{line}"); - output.send(ansi2html(&line)).unwrap(); + } + if plain_output.receiver_count() > 0 { + let stripped = + String::from_utf8_lossy(&strip_ansi_escapes::strip(&string).unwrap()) + .into_owned(); + plain_output.send(stripped).unwrap(); + } + if html_output.receiver_count() > 0 { + html_output.send(ansi2html(&string)).unwrap(); } nooutput!(); async_std::task::sleep(Duration::from_millis(500)).await; |