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
96
97
mod span;
mod tag;
use ansi_term::{ANSIString, Style};
use super::{
helper::{flatten, CustomTagParser, GeneratorInfallible, NoopCustomTagParser},
Generator,
};
pub use tag::ANSITermTagConvertor;
#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
#[derive(Debug)]
pub struct ANSIStringsGenerator<P = NoopCustomTagParser<Style>> {
convertor: ANSITermTagConvertor<P>,
}
impl<P> Default for ANSIStringsGenerator<P> {
fn default() -> Self {
Self {
convertor: Default::default(),
}
}
}
impl<P> ANSIStringsGenerator<P> {
pub fn new(p: P) -> Self {
Self {
convertor: ANSITermTagConvertor::new(p),
}
}
}
impl<'a, P> Generator<'a> for ANSIStringsGenerator<P>
where
P: CustomTagParser<Output = Style>,
{
type Convertor = ANSITermTagConvertor<P>;
type Output = Vec<ANSIString<'a>>;
type Err = GeneratorInfallible;
fn convertor(&mut self) -> &mut Self::Convertor {
&mut self.convertor
}
fn generate(&mut self, markup: Vec<Vec<crate::parser::ItemG<'a, Self>>>) -> Result<Self::Output, Self::Err> {
Ok(markup.into_iter().map(flatten).fold(vec![], |mut acc, line| {
if !acc.is_empty() {
acc.push(Style::default().paint("\n"));
}
acc.extend(line);
acc
}))
}
}