Skip to content
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

Render ANSI color codes in playground stderr output #2651

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

08volt
Copy link

@08volt 08volt commented Feb 20, 2025

This PR implements a function styleStderrOutput that parses ANSI color codes from the Rust compiler's stderr output and converts them into inline HTML <span> elements with corresponding CSS styles. This allows the embedded playground to display colored compiler error messages, improving the user experience and more accurately reflecting the Rust compiler's output.

Motivation

  • The Rust compiler's colorful error messages are a key feature that helps developers quickly identify and fix issues.
  • The embedded playground currently displays stderr output in monochrome, losing this important information.
  • This change addresses the functionality requested in rust-lang/rust-playground#900 and enhances the educational value of the playground.
  • It also addresses the problem described in the provided description, where the lack of color in the playground output requires the course instructor to show their terminal, which slows down the course.

Implementation Details

  • The styleStderrOutput function uses a regular expression to split the stderr output into text and ANSI color code sequences.
  • It maps ANSI color codes to corresponding CSS styles using a predefined object.
  • It wraps colored text segments in <span> elements with inline styles.
  • It handles the closing and opening of span elements to prevent issues.
  • It also html encodes the text to prevent XSS.

Related Issues

@08volt 08volt marked this pull request as draft February 20, 2025 10:57
Copy link
Collaborator

@djmitche djmitche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fantastic!

Is there some other trick required to get the stderr to contain the ANSI escapes? I tried running this with mdbook serve and I still see plain-text errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants