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
}))
}
}