-
-
Notifications
You must be signed in to change notification settings - Fork 562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Syntax highlighting for fenced code blocks, in command help output, on macOS works #7472
Comments
Hey I can take a look at this one if it's still free |
@YorickdeJong That's awesome, this is all yours. Please let us know if you have any questions as you explore. You can also ask questions on the contributors discord https://discord.ockam.io |
Cool thanks! I checked out the podcasts online and I really like the project! Combining rust, swift and microservices, awesome |
Quick question, it seems that a few years ago this project stopped using the main branch. can I assume that the developer branch is the production branch? |
@YorickdeJong That's right. The |
Quick update, I ran a test and debugged the FencedCodeBlockHighlighter struct. It seems that the problem arises from the THEME variable. On MacOS it cannot determine the terminal background and sets it to None. In turn, this causes the process_line() function to panic, as the static THEME: Lazy<Option<Theme>> = Lazy::new(|| {
let theme_name = match TerminalBackground::detect_background_color() {
TerminalBackground::Light => "base16-ocean.light",
TerminalBackground::Dark => "base16-ocean.dark",
TerminalBackground::Unknown => "base16-ocean.dark" //return None,
};
let mut theme_set = ThemeSet::load_defaults();
let theme = theme_set.themes.remove(theme_name).unwrap();
Some(theme)
}); When I ran the test with:
instead of
I did get colors to show on macOS I also managed to get the correct coloring for the issue at hand in the test: However, it seems that running if is_markdown() {
Box::leak(body.to_string().into_boxed_str())
} else {
let syntax_highlighted = process_terminal_docs(body.to_string());
Box::leak(syntax_highlighted.into_boxed_str())
}
} |
@YorickdeJong Good job examining the code 👍🏽 .
|
Hi @nazmulidris, Thank you for the detailed feedback. I've reintroduced the call to /// Use a shell syntax highlighter to render the fenced code blocks in terminals
fn process_terminal_docs(input: String) -> String {
let mut output: Vec<String> = Vec::new();
let mut code_highlighter = FencedCodeBlockHighlighter::new();
for line in LinesWithEndings::from(&input) {
// Check if the current line is a code block start/end or content.
let is_code_line = code_highlighter.process_line(line, &mut output);
if !is_code_line {
// The line was not part of a code block, so process normally.
// Replace headers with bold and underline text
if HEADER_RE.is_match(line) {
output.push(line.to_string().bold().underlined().to_string());
}
// Replace subheaders with underlined text
else if line.starts_with("#### ") {
output.push(line.replace("#### ", "").underlined().to_string());
}
// Catch all other lines
else {
output.push(line.to_string());
}
}
}
output.join("")
} Such that my tests are able to include color into the output for shell commands. I should have mentioned that, apologies. is_markdown(): I think the reason we aren't seeing any colors for the markdown menu is that syntax_highlight isn't configured for it static THEME: I agree with your suggestion that we should default Manual ANSI Sequences: I plan to explore the use of the r3bl_ansi_color crate to manually generate ANSI escape sequences for syntax highlighting tomorrow. Will keep you posted. |
@YorickdeJong Thanks for the clarification 🙏🏽 . That's great that you are calling Re: is_markdown()We actually do not want those ANSI escape sequences to be generated for Markdown generation. We only want them to be generated for stdout display output. So the Re: static THEMEThat's great that you agree with that too 👏🏽 . Manual ANSI sequencesPlease let me know if you have any questions about this. The example I shared with you is from the r3bl_tui crate which does not output to r3bl_ansi_color.
|
Hi @nazmulidris, Thanks for your suggestions. Re: Manual ANSI sequencesI implemented the #[allow(dead_code)]
pub fn process_line(&mut self, line: &str, output: &mut Vec<String>) -> bool {
if let Some(highlighter) = &mut self.inner {
if line == "```sh\n" {
self.in_fenced_block = true;
return true;
}
if !self.in_fenced_block {
return false;
}
if line == "```\n" {
// Push a reset to clear the coloring.
output.push("\x1b[0m".to_string());
self.in_fenced_block = false;
return true;
}
// Highlight the code line
let ranges: Vec<(Style, &str)> = highlighter
.highlight_line(line, &SYNTAX_SET)
.unwrap_or_default();
// Convert each syntect range to an ANSI styled string
convert_syntect_style_to_ansi(output, &ranges);
true
} else {
false
}
}
} With a converted function: pub fn convert_syntect_style_to_ansi(output: &mut Vec<String>, ranges: &Vec<(Style, &str)>) {
for (style, text) in ranges {
let ansi_styled_text = AnsiStyledText {
text: text,
style: &[
StyleAnsi::Foreground(Color::Rgb(style.foreground.r, style.foreground.g, style.foreground.b)),
],
};
output.push(ansi_styled_text.to_string());
}
} My tests come back positively. It should be noted that I currently only add Running ockam project ticket --helpI'm still having problems coloring the results from running the command Would you happen to know why the color is still missing in the normal mode? Thanks! |
@YorickdeJong The code using the conversion to AnsiStyledText looks good! We can incorporate italic, bold, underline as well if we need. How did you run command in debug mode? I wasn't sure how you were able to run it in debug vs normal mode. Also if you can raise a PR with the code changes that you've made, that will allow me to take a look at the code that you've created, and I can take a look at why the colorization isn't making it to the output of
|
Hi @nazmulidris, Thanks! I have created a pull request: #7495. In this PR I still included the tests that I ran for the coloring functions, if needed I can remove those. The commands that I ran to enter debug mode and run the ockam command are:
I think found the silly reason for the difference between the two. When running I am looking forward to your review, let me know if any changes need to be made. |
Current behavior
Currently, for Ockam Command CLI help output, the syntax highlighting for fenced code blocks has been disabled. It didn't work properly on macOS.
Here's an example on macOS of the current behavior. Run
ockam project ticket --help
. It produces the following output in the Examples section. Note that it does not have any syntax highlighting.Desired behavior
With syntax highlighting via syntect crate, it should be whatever syntect produces for
sh
syntax set.Here are some places to get started in the code.
process_terminal_docs()
- This function is responsible for taking the CLAP help text output and formatting it just before displaying it to stdout.FencedCodeBlockHighlighter::process_line()
- This is the function that is generating the unexpected and undefined color spans and newlines in the output.The text was updated successfully, but these errors were encountered: