1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use termcolor::NoColor;
use std::{borrow::Cow, fmt::Write as WriteStr};
use crate::{
html::HtmlWriter,
utils::{normalize_newlines, WriteAdapter},
TermError,
};
mod parser;
#[cfg(test)]
mod tests;
pub(crate) use self::parser::TermOutputParser;
pub trait TermOutput: Clone + Send + Sync + 'static {}
#[derive(Debug, Clone)]
pub struct Captured(String);
impl AsRef<str> for Captured {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Captured {
pub(crate) fn new(term_output: String) -> Self {
Self(match normalize_newlines(&term_output) {
Cow::Owned(normalized) => normalized,
Cow::Borrowed(_) => term_output,
})
}
pub(crate) fn write_as_html(
&self,
output: &mut dyn WriteStr,
wrap_width: Option<usize>,
) -> Result<(), TermError> {
let mut html_writer = HtmlWriter::new(output, wrap_width);
TermOutputParser::new(&mut html_writer).parse(self.0.as_bytes())
}
pub fn to_html(&self) -> Result<String, TermError> {
let mut output = String::with_capacity(self.0.len());
self.write_as_html(&mut output, None)?;
Ok(output)
}
fn write_as_plaintext(&self, output: &mut dyn WriteStr) -> Result<(), TermError> {
let mut plaintext_writer = NoColor::new(WriteAdapter::new(output));
TermOutputParser::new(&mut plaintext_writer).parse(self.0.as_bytes())
}
pub fn to_plaintext(&self) -> Result<String, TermError> {
let mut output = String::with_capacity(self.0.len());
self.write_as_plaintext(&mut output)?;
Ok(output)
}
}
impl TermOutput for Captured {}