summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMustafa Quraish <mustafaq9@gmail.com>2021-09-23 14:32:20 -0400
committerAndreas Kling <kling@serenityos.org>2021-09-24 14:32:52 +0200
commit07419b89314d73ee5f10ab1b3a1aadb16f311cf9 (patch)
treecf9ebe6bd7aaf81f8de883d3ecd824565b2b922e
parent6f423ed26e0e1c3221e61f52a4e279d5a65efa53 (diff)
downloadserenity-07419b89314d73ee5f10ab1b3a1aadb16f311cf9.zip
diff: Only color output when stdout is a tty
If we're redirecting the output somewhere, we likely don't want to have ANSI codes in the middle of our diff output.
-rw-r--r--Userland/Utilities/diff.cpp18
1 files changed, 14 insertions, 4 deletions
diff --git a/Userland/Utilities/diff.cpp b/Userland/Utilities/diff.cpp
index 2ea0d7b8ee..cfbaf02d49 100644
--- a/Userland/Utilities/diff.cpp
+++ b/Userland/Utilities/diff.cpp
@@ -61,13 +61,23 @@ int main(int argc, char** argv)
if (num_added > 1)
sb.appendff(",{}", target_start + num_added - 1);
+ bool color_output = isatty(STDOUT_FILENO);
+
outln("Hunk: {}", sb.build());
- for (const auto& line : hunk.removed_lines)
- outln("\033[31;1m< {}\033[0m", line);
+ for (const auto& line : hunk.removed_lines) {
+ if (color_output)
+ outln("\033[31;1m< {}\033[0m", line);
+ else
+ outln("< {}", line);
+ }
if (num_added > 0 && num_removed > 0)
outln("---");
- for (const auto& line : hunk.added_lines)
- outln("\033[32;1m> {}\033[0m", line);
+ for (const auto& line : hunk.added_lines) {
+ if (color_output)
+ outln("\033[32;1m> {}\033[0m", line);
+ else
+ outln("> {}", line);
+ }
}
return hunks.is_empty() ? 0 : 1;