Skip to content

ASCII color codes are displayed inline for editors #3

Description

@lindhe

I'm using Rocket (which is awesome, BTW) and noticed that sometimes error messages in my editor would be mangled with ASCII color codes, making the error messages very hard to read.

In terminal:

Screenshot from 2023-08-11 16-16-58

In editor:

Screenshot from 2023-08-11 10-51-47

I thought this was an issue with rust-analyzer, since I do not see the ASCII color codes printed if I pipe cargo build into cat, so I posted an issue there. But the people over there say that it is caused by your implementation of Display here:

impl fmt::Display for Line<'_> {
#[cfg(all(feature = "colors", not(nightly_diagnostics)))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use yansi::{Paint, Color};
let style = match self.level {
Level::Error => Color::Red.bold(),
Level::Warning => Color::Yellow.bold(),
Level::Note => Color::Green.bold(),
Level::Help => Color::Cyan.bold(),
};
let ((prefix, suffix), msg) = (self.kind.split(), self.msg.primary());
write!(f, "{}{}{}{}", prefix, self.level.paint(style), suffix, msg.bold())
}
#[cfg(not(all(feature = "colors", not(nightly_diagnostics))))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (prefix, suffix) = self.kind.split();
write!(f, "{}{}{}{}", prefix, self.level, suffix, self.msg)
}
}

You should really read rust-lang/rust-analyzer#15443 first to get all of the context.

I have not yet wrapped my head around all the parts involved here, since I am pretty new to Rust. But I thought I'd post this here to see what you think.

Steps to reproduce

  1. Create the following crate:
cargo new -q foo
cd foo
cargo add -q rocket@0.5.0-rc.3
cat <<EOF > src/main.rs
use rocket::UriDisplayPath;

fn main() {
    println!("Hello, world!");
}

#[derive(UriDisplayPath)]
pub enum Foo {
    Bar,
}
EOF
  1. Optionally run cargo check.
  2. Open src/main.rs with Vim.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions