pub fn to_ansi(
    source: &[u8],
    highlights: impl Iterator<Item = Result<HighlightEvent, HighlightError>>,
) -> anyhow::Result<String> {
    let mut buf = Buffer::ansi();

    let mut keyword = ColorSpec::new();
    keyword.set_fg(Some(Color::Blue)).set_bold(true);
    let mut number = ColorSpec::new();
    number.set_fg(Some(Color::Yellow)).set_bold(true);
    let mut string = ColorSpec::new();
    string.set_fg(Some(Color::Magenta)).set_bold(true);
    let mut comment = ColorSpec::new();
    comment.set_fg(Some(Color::Green)).set_bold(true);

    for event in highlights {
        match event? {
            HighlightEvent::HighlightStart(Highlight(style)) => match style {
                0 => buf.set_color(&keyword)?,
                1 => buf.set_color(&number)?,
                2 => buf.set_color(&string)?,
                4 => buf.set_color(&comment)?,
                _ => (),
            },
            HighlightEvent::Source { start, end } => buf.write_all(&source[start..end])?,
            HighlightEvent::HighlightEnd => buf.reset()?,
        }
    }

    Ok(String::from_utf8(buf.into_inner())?)
}
/// Turn highlights into ANSI sequences. Accepts highlights in any language, but the name order
/// needs to match.
Turn highlights into ANSI sequences. Accepts highlights in any language, but the name order
needs to match.
Turn highlights into ANSI sequences . Accepts highlights in any language but the name order needs to match .
pub fn to_ansi(
    source: &[u8],
    highlights: impl Iterator<Item = Result<HighlightEvent, HighlightError>>,
) -> anyhow::Result<String> {
    let mut buf = Buffer::ansi();
    let mut keyword = ColorSpec::new();
    keyword.set_fg(Some(Color::Blue)).set_bold(true);
    let mut number = ColorSpec::new();
    number.set_fg(Some(Color::Yellow)).set_bold(true);
    let mut string = ColorSpec::new();
    string.set_fg(Some(Color::Magenta)).set_bold(true);
    let mut comment = ColorSpec::new();
    comment.set_fg(Some(Color::Green)).set_bold(true);
    for event in highlights {
        match event? {
            HighlightEvent::HighlightStart(Highlight(style)) => match style {
                0 => buf.set_color(&keyword)?,
                1 => buf.set_color(&number)?,
                2 => buf.set_color(&string)?,
                4 => buf.set_color(&comment)?,
                _ => (),
            },
            HighlightEvent::Source { start, end } => buf.write_all(&source[start..end])?,
            HighlightEvent::HighlightEnd => buf.reset()?,
        }
    }
    Ok(String::from_utf8(buf.into_inner())?)
}
pub fn to_ansi ( source : & [ u8 ] , highlights : impl Iterator < Item = Result < HighlightEvent , HighlightError > > , ) -> anyhow :: Result < String > { let mut buf = Buffer :: ansi ( ) ; let mut keyword = ColorSpec :: new ( ) ; keyword . set_fg ( Some ( Color :: Blue ) ) . set_bold ( true ) ; let mut number = ColorSpec :: new ( ) ; number . set_fg ( Some ( Color :: Yellow ) ) . set_bold ( true ) ; let mut string = ColorSpec :: new ( ) ; string . set_fg ( Some ( Color :: Magenta ) ) . set_bold ( true ) ; let mut comment = ColorSpec :: new ( ) ; comment . set_fg ( Some ( Color :: Green ) ) . set_bold ( true ) ; for event in highlights { match event ? { HighlightEvent :: HighlightStart ( Highlight ( style ) ) => match style { 0 => buf . set_color ( & keyword ) ? , 1 => buf . set_color ( & number ) ? , 2 => buf . set_color ( & string ) ? , 4 => buf . set_color ( & comment ) ? , _ => ( ) , } , HighlightEvent :: Source { start , end } => buf . write_all ( & source [ start .. end ] ) ? , HighlightEvent :: HighlightEnd => buf . reset ( ) ? , } } Ok ( String :: from_utf8 ( buf . into_inner ( ) ) ? ) }
Turn highlights into ANSI sequences.
Turn highlights into ANSI sequences .
source
&[u8]
highlights
impl Iterator<Item = Result<HighlightEvent, HighlightError>>
source
&[u8]
highlights
impl Iterator<Item = Result<HighlightEvent, HighlightError>>