Skip to content

Colorize format --diff output #9984

@MichaReiser

Description

@MichaReiser

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.

Metadata

Metadata

Assignees

Labels

cliRelated to the command-line interfacegood first issueGood for newcomershelp wantedContributions especially welcome

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions