use anstyle::Style;
mod span;
mod tag;
pub use span::{StyledSpan, StyledText};
pub use tag::ANSITagConvertor;
use super::{
Generator,
helper::{CustomTagParser, GeneratorInfallible, NoopCustomTagParser, flatten},
};
#[derive(Debug)]
pub struct ANSIStringsGenerator<P = NoopCustomTagParser<Style>> {
convertor: ANSITagConvertor<P>,
}
impl<P> Default for ANSIStringsGenerator<P> {
fn default() -> Self {
Self {
convertor: ANSITagConvertor::<P>::default(),
}
}
}
impl<P> ANSIStringsGenerator<P> {
pub fn new(p: P) -> Self {
Self {
convertor: ANSITagConvertor::new(p),
}
}
}
impl<'a, P> Generator<'a> for ANSIStringsGenerator<P>
where
P: CustomTagParser<Output = Style>,
{
type Convertor = ANSITagConvertor<P>;
type Err = GeneratorInfallible;
type Output = StyledText<'a>;
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> {
let mut spans = Vec::with_capacity(markup.len());
for (i, line) in markup.into_iter().enumerate() {
if i > 0 {
spans.push(StyledSpan::new(Style::new(), "\n"));
}
spans.extend(flatten(line));
}
Ok(StyledText::new(spans))
}
}