Highlight a single bash command line (e.g. "ls -la --color"). Returns a Line with syntax-highlighted spans.
(command: &str, hl_theme: HighlightTheme)
| 84 | /// Highlight a single bash command line (e.g. "ls -la --color"). |
| 85 | /// Returns a Line with syntax-highlighted spans. |
| 86 | pub fn highlight_bash_command<'a>(command: &str, hl_theme: HighlightTheme) -> Line<'a> { |
| 87 | let syntax = SYNTAX_SET |
| 88 | .find_syntax_by_extension("sh") |
| 89 | .unwrap_or_else(|| SYNTAX_SET.find_syntax_plain_text()); |
| 90 | |
| 91 | let theme = &THEME_SET.themes[hl_theme.syntect_theme_name()]; |
| 92 | let mut highlighter = HighlightLines::new(syntax, theme); |
| 93 | |
| 94 | // Highlight the command as a single line |
| 95 | let input = if command.ends_with('\n') { |
| 96 | command.to_string() |
| 97 | } else { |
| 98 | format!("{}\n", command) |
| 99 | }; |
| 100 | |
| 101 | match highlighter.highlight_line(&input, &SYNTAX_SET) { |
| 102 | Ok(ranges) => { |
| 103 | let spans: Vec<Span<'a>> = ranges |
| 104 | .into_iter() |
| 105 | .map(|(style, text)| { |
| 106 | let clean = text.trim_end_matches('\n').to_string(); |
| 107 | Span::styled(clean, syntect_to_ratatui_style(&style)) |
| 108 | }) |
| 109 | .filter(|s| !s.content.is_empty()) |
| 110 | .collect(); |
| 111 | Line::from(spans) |
| 112 | } |
| 113 | Err(_) => Line::from(Span::raw(command.to_string())), |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /// Highlight bash output text (uses plain text / console syntax). |
| 118 | /// Returns lines with minimal styling. |
no test coverage detected