mod span;
mod tag;
use crossterm::style::{ContentStyle, Print};
pub use span::Span;
pub use tag::CrosstermTagConvertor;
use crate::{
generator::{
Generator,
helper::{CustomTagParser, GeneratorInfallible, NoopCustomTagParser, flatten},
},
parser::ItemG,
};
#[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 Err = GeneratorInfallible;
type Output = Vec<Span<'a>>;
fn convertor(&mut self) -> &mut Self::Convertor {
&mut self.convertor
}
fn generate(&mut self, markup: Vec<Vec<ItemG<'a, Self>>>) -> Result<Self::Output, Self::Err> {
let mut spans = Vec::with_capacity(markup.len());
for (i, line) in markup.into_iter().enumerate() {
if i > 0 {
spans.push(Span::NoStyle(Print("\n")));
}
spans.extend(flatten(line));
}
Ok(spans)
}
}