ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Colorize `format --diff` output

Open MichaReiser opened this issue 1 year ago • 4 comments

Discussed in https://github.com/astral-sh/ruff/discussions/9983

We should colorize the diff output (added and removed lines).

The relevant code is in https://github.com/astral-sh/ruff/blob/a7d1f7e1ec6d2d4443eaec4bee5faebd97f95c66/crates/ruff_linter/src/source_kind.rs#L103-L111

We probably would need to extract the to_writer function and change it so that we set the right color depending on if it's an addition or deletion:

 let mut header = self.header.as_ref();
        for hunk in self.iter_hunks() {
            if let Some((old_file, new_file)) = header.take() {
                writeln!(w, "--- {}", old_file)?;  // <-- add color here
                writeln!(w, "+++ {}", new_file)?;
            }
            write!(w, "{}", hunk)?;
        }
        Ok(())

Originally posted by ssenior45 February 14, 2024 If I run ruff check on a file then the output is colored which leads me to believe that my terminal is configured to display colored output from the tool correctly.

However when I run ruff format --diff the output is not colored. Is this expected? If so is there anything that can be done about it?

Thanks in advance.

MichaReiser avatar Feb 14 '24 13:02 MichaReiser

Def a good first issue!

charliermarsh avatar Feb 14 '24 14:02 charliermarsh

Hey @MichaReiser, I'm interested on working on this

Edit: But I've also noticed you tagged OP in the original discussion

senadev42 avatar Feb 14 '24 14:02 senadev42

@senadev42 nice. I think the original OP doesn't mind if the feature gets implemented by someone else. I assign the issue to you. Let me know if you need any help.

Note: We use https://docs.rs/colored/latest/colored/ for colonizing the CLI output.

MichaReiser avatar Feb 14 '24 14:02 MichaReiser

Just catching up with this now. Thanks for moving it to an issue. I would like to see this implemented and am more than happy for @senadev42 to work on it.

ssenior45 avatar Feb 14 '24 23:02 ssenior45

@senadev42 is there something I can help you with or would you prefer if someone else takes a look at it?

MichaReiser avatar Feb 23 '24 21:02 MichaReiser

@MichaReiser Oh I've just been unexpectedly busy lately but I'm picking away at it, just kind of slowly since I assumed it wasn't an urgent/critical task.

I can let go if it is though, since I'm using this as an opportunity to dip my toes into rust.

senadev42 avatar Feb 23 '24 21:02 senadev42

@senadev42 it's not urgent. I just wanted to make sure you've all you need :)

MichaReiser avatar Feb 23 '24 21:02 MichaReiser

Hey @MichaReiser

So I've made a draft PR at #10110 and I'd like some help interpreting why and how some of the tests are failing

senadev42 avatar Feb 24 '24 13:02 senadev42

Closed by https://github.com/astral-sh/ruff/pull/10110

MichaReiser avatar Mar 01 '24 09:03 MichaReiser