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

Colorize format --diff output #9984

Closed
MichaReiser opened this issue Feb 14, 2024 Discussed in #9983 · 9 comments
Closed

Colorize format --diff output #9984

MichaReiser opened this issue Feb 14, 2024 Discussed in #9983 · 9 comments
Assignees
Labels
cli Related to the command-line interface good first issue Good for newcomers help wanted Contributions especially welcome

Comments

@MichaReiser
Copy link
Member

Discussed in #9983

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

The relevant code is in

unified_diff.header(&fs::relativize_path(path), &fs::relativize_path(path));
}
unified_diff.to_writer(&mut *writer)?;
writer.write_all(b"\n")?;
writer.flush()?;
Ok(())

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 MichaReiser added good first issue Good for newcomers cli Related to the command-line interface help wanted Contributions especially welcome labels Feb 14, 2024
@charliermarsh
Copy link
Member

Def a good first issue!

@senadev42
Copy link
Contributor

senadev42 commented Feb 14, 2024

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

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

@MichaReiser
Copy link
Member Author

MichaReiser commented Feb 14, 2024

@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.

@ssenior45
Copy link

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.

@MichaReiser
Copy link
Member Author

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

@senadev42
Copy link
Contributor

@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.

@MichaReiser
Copy link
Member Author

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

@senadev42
Copy link
Contributor

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

@MichaReiser
Copy link
Member Author

Closed by #10110

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Related to the command-line interface good first issue Good for newcomers help wanted Contributions especially welcome
Projects
None yet
Development

No branches or pull requests

4 participants