| 1 | //! 8051 assembly syntax highlighting for the TUI. |
| 2 | |
| 3 | use bench51_core::syntax::{Tok, tokens}; |
| 4 | use ratatui::style::{Color, Modifier, Style}; |
| 5 | use ratatui::text::Span; |
| 6 | |
| 7 | fn style(t: Tok) -> Style { |
| 8 | match t { |
| 9 | Tok::Plain => Style::new(), |
| 10 | Tok::Comment => Style::new().fg(Color::DarkGray), |
| 11 | Tok::Label => Style::new().fg(Color::Yellow), |
| 12 | Tok::Mnemonic => Style::new().fg(Color::Cyan).add_modifier(Modifier::BOLD), |
| 13 | Tok::Directive => Style::new().fg(Color::Magenta), |
| 14 | Tok::Register => Style::new().fg(Color::LightBlue), |
| 15 | Tok::Number => Style::new().fg(Color::LightGreen), |
| 16 | Tok::Str => Style::new().fg(Color::Green), |
| 17 | Tok::Sfr => Style::new().fg(Color::LightMagenta), |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | /// Splits a source line into styled spans, applying `base` underneath. |
| 22 | pub fn highlight(line: &str, base: Style) -> Vec<Span<'static>> { |
| 23 | tokens(line) |
| 24 | .into_iter() |
| 25 | .map(|(r, t)| Span::styled(line[r].to_string(), base.patch(style(t)))) |
| 26 | .collect() |
| 27 | } |