term_transcript/term/
mod.rs1use termcolor::NoColor;
2
3use std::{borrow::Cow, fmt::Write as WriteStr};
4
5#[cfg(feature = "svg")]
6use crate::write::{SvgLine, SvgWriter};
7use crate::{
8 utils::{normalize_newlines, WriteAdapter},
9 write::HtmlWriter,
10 TermError,
11};
12
13mod parser;
14#[cfg(test)]
15mod tests;
16
17pub(crate) use self::parser::TermOutputParser;
18
19pub trait TermOutput: Clone + Send + Sync + 'static {}
21
22#[derive(Debug, Clone)]
24pub struct Captured(String);
25
26impl AsRef<str> for Captured {
27 fn as_ref(&self) -> &str {
28 &self.0
29 }
30}
31
32impl Captured {
33 pub(crate) fn new(term_output: String) -> Self {
34 Self(match normalize_newlines(&term_output) {
36 Cow::Owned(normalized) => normalized,
37 Cow::Borrowed(_) => term_output,
38 })
39 }
40
41 pub(crate) fn write_as_html(
42 &self,
43 output: &mut dyn WriteStr,
44 wrap_width: Option<usize>,
45 ) -> Result<(), TermError> {
46 let mut html_writer = HtmlWriter::new(output, wrap_width);
47 TermOutputParser::new(&mut html_writer).parse(self.0.as_bytes())
48 }
49
50 #[cfg(feature = "svg")]
51 pub(crate) fn write_as_svg(
52 &self,
53 wrap_width: Option<usize>,
54 ) -> Result<Vec<SvgLine>, TermError> {
55 let mut svg_writer = SvgWriter::new(wrap_width);
56 TermOutputParser::new(&mut svg_writer).parse(self.0.as_bytes())?;
57 Ok(svg_writer.into_lines())
58 }
59
60 pub fn to_html(&self) -> Result<String, TermError> {
85 let mut output = String::with_capacity(self.0.len());
86 self.write_as_html(&mut output, None)?;
87 Ok(output)
88 }
89
90 fn write_as_plaintext(&self, output: &mut dyn WriteStr) -> Result<(), TermError> {
91 let mut plaintext_writer = NoColor::new(WriteAdapter::new(output));
92 TermOutputParser::new(&mut plaintext_writer).parse(self.0.as_bytes())
93 }
94
95 pub fn to_plaintext(&self) -> Result<String, TermError> {
101 let mut output = String::with_capacity(self.0.len());
102 self.write_as_plaintext(&mut output)?;
103 Ok(output)
104 }
105}
106
107impl TermOutput for Captured {}