MCPcopy Create free account
hub / github.com/GCWing/BitFun / highlight_bash_command

Function highlight_bash_command

src/apps/cli/src/ui/syntax_highlight.rs:86–115  ·  view source on GitHub ↗

Highlight a single bash command line (e.g. "ls -la --color"). Returns a Line with syntax-highlighted spans.

(command: &str, hl_theme: HighlightTheme)

Source from the content-addressed store, hash-verified

84/// Highlight a single bash command line (e.g. "ls -la --color").
85/// Returns a Line with syntax-highlighted spans.
86pub 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.

Callers 1

render_bash_blockFunction · 0.85

Calls 4

syntect_to_ratatui_styleFunction · 0.85
syntect_theme_nameMethod · 0.80
collectMethod · 0.80
is_emptyMethod · 0.45

Tested by

no test coverage detected