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
mod span;
mod tag;
use crossterm::style::{ContentStyle, Print};
use crate::{
generator::{
helper::{flatten, CustomTagParser, GeneratorInfallible, NoopCustomTagParser},
Generator,
},
parser::ItemG,
};
pub use span::Span;
pub use tag::CrosstermTagConvertor;
#[cfg_attr(docsrs, doc(cfg(feature = "crossterm")))]
#[derive(Debug)]
pub struct CrosstermCommandsGenerator<P = NoopCustomTagParser<ContentStyle>> {
convertor: CrosstermTagConvertor<P>,
}
impl<P> Default for CrosstermCommandsGenerator<P> {
fn default() -> Self {
Self {
convertor: CrosstermTagConvertor::<P>::default(),
}
}
}
impl<P> CrosstermCommandsGenerator<P> {
pub fn new(p: P) -> Self {
Self {
convertor: CrosstermTagConvertor::new(p),
}
}
}
impl<'a, P> Generator<'a> for CrosstermCommandsGenerator<P>
where
P: CustomTagParser<Output = ContentStyle>,
{
type Convertor = CrosstermTagConvertor<P>;
type Output = Vec<Span<'a>>;
type Err = GeneratorInfallible;
fn convertor(&mut self) -> &mut Self::Convertor {
&mut self.convertor
}
fn generate(&mut self, items: Vec<Vec<ItemG<'a, Self>>>) -> Result<Self::Output, Self::Err> {
Ok(items.into_iter().map(flatten).fold(vec![], |mut acc, line| {
if !acc.is_empty() {
acc.push(Span::NoStyle(Print("\n")));
}
acc.extend(line);
acc
}))
}
}