Page MenuHomeFreeBSD

D30545.id94573.diff
No OneTemporary

D30545.id94573.diff

Index: usr.bin/diff/diff.h
===================================================================
--- usr.bin/diff/diff.h
+++ usr.bin/diff/diff.h
@@ -85,17 +85,25 @@
#define D_SKIPPED2 6 /* path2 was a special file */
#define D_ERROR 7 /* A file access error occurred */
+/*
+ * Color options
+ */
+#define COLORFLAG_NEVER 0
+#define COLORFLAG_AUTO 1
+#define COLORFLAG_ALWAYS 2
+
struct excludes {
char *pattern;
struct excludes *next;
};
-extern int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag;
+extern int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag, color;
extern int diff_format, diff_context, status, ignore_file_case;
extern int suppress_common;
extern int tabsize, width;
extern char *start, *ifdefname, *diffargs, *label[2], *ignore_pats;
extern char *group_format;
+extern const char *add_code, *del_code;
extern struct stat stb1, stb2;
extern struct excludes *excludes_list;
extern regex_t ignore_re;
Index: usr.bin/diff/diff.1
===================================================================
--- usr.bin/diff/diff.1
+++ usr.bin/diff/diff.1
@@ -44,6 +44,7 @@
.Fl n | q | u | y
.Oc
.Op Fl -brief
+.Op Fl -color Ns = Ns Ar when
.Op Fl -changed-group-format Ar GFMT
.Op Fl -ed
.Op Fl -expand-tabs
@@ -71,6 +72,7 @@
.Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern
.Op Fl L Ar label | Fl -label Ar label
.Op Fl -brief
+.Op Fl -color Ns = Ns Ar when
.Op Fl -changed-group-format Ar GFMT
.Op Fl -ed
.Op Fl -expand-tabs
@@ -96,6 +98,7 @@
.Op Fl aBbdiltw
.Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern
.Op Fl -brief
+.Op Fl -color Ns = Ns Ar when
.Op Fl -changed-group-format Ar GFMT
.Op Fl -ed
.Op Fl -expand-tabs
@@ -122,6 +125,7 @@
.Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern
.Op Fl L Ar label | Fl -label Ar label
.Op Fl -brief
+.Op Fl -color Ns = Ns Ar when
.Op Fl -changed-group-format Ar GFMT
.Op Fl -ed
.Op Fl -expand-tabs
@@ -150,6 +154,7 @@
.Fl n | q | u
.Oc
.Op Fl -brief
+.Op Fl -color Ns = Ns Ar when
.Op Fl -changed-group-format Ar GFMT
.Op Fl -context
.Op Fl -ed
@@ -184,6 +189,7 @@
.Ar dir1 dir2
.Nm diff
.Op Fl aBbditwW
+.Op Fl -color Ns = Ns Ar when
.Op Fl -expand-tabs
.Op Fl -ignore-all-blanks
.Op Fl -ignore-blank-lines
@@ -332,6 +338,21 @@
.It Fl b -ignore-space-change
Causes trailing blanks (spaces and tabs) to be ignored, and other
strings of blanks to compare equal.
+.It Fl -color= Ns Oo Ar when Oc
+Color the additions green, and removals red, or the value in the
+.Ev DIFFCOLORS
+environment variable.
+The possible values of
+.Ar when
+are
+.Dq Cm never ,
+.Dq Cm always
+and
+.Dq Cm auto .
+.Cm auto
+will use color if the output is a tty and the
+.Ev COLORTERM
+environment variable is set to a non-empty string.
.It Fl d -minimal
Try very hard to produce a diff as small as possible.
This may consume a lot of processing power and memory when processing
@@ -592,6 +613,20 @@
identical
pairs (where num1 = num2) are abbreviated as a single
number.
+.Sh ENVIRONMENT
+.Bl -tag -width DIFFCOLORS
+.It Ev DIFFCOLORS
+The value of this variable is the form
+.Ar add Ns : Ns Ar rm ,
+where
+.Ar add
+is the ASCII escape sequence for additions and
+.Ar rm
+is the ASCII escape sequence for deletions.
+If this is unset,
+.Nm
+uses green for additions and red for removals.
+.El
.Sh FILES
.Bl -tag -width /tmp/diff.XXXXXXXX -compact
.It Pa /tmp/diff.XXXXXXXX
Index: usr.bin/diff/diff.c
===================================================================
--- usr.bin/diff/diff.c
+++ usr.bin/diff/diff.c
@@ -38,11 +38,13 @@
#include "diff.h"
#include "xmalloc.h"
-int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag;
+int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag, color;
int diff_format, diff_context, status, ignore_file_case, suppress_common;
int tabsize = 8, width = 130;
+static int colorflag = COLORFLAG_NEVER;
char *start, *ifdefname, *diffargs, *label[2], *ignore_pats;
char *group_format = NULL;
+const char *add_code, *del_code;
struct stat stb1, stb2;
struct excludes *excludes_list;
regex_t ignore_re;
@@ -57,6 +59,7 @@
OPT_HORIZON_LINES,
OPT_CHANGED_GROUP_FORMAT,
OPT_SUPPRESS_COMMON,
+ OPT_COLOR,
};
static struct option longopts[] = {
@@ -97,6 +100,7 @@
{ "tabsize", required_argument, NULL, OPT_TSIZE },
{ "changed-group-format", required_argument, NULL, OPT_CHANGED_GROUP_FORMAT},
{ "suppress-common-lines", no_argument, NULL, OPT_SUPPRESS_COMMON },
+ { "color", optional_argument, NULL, OPT_COLOR },
{ NULL, 0, 0, '\0'}
};
@@ -106,6 +110,8 @@
void push_ignore_pats(char *);
void read_excludes_file(char *file);
void set_argstr(char **, char **);
+static const char *init_code(int, const char *);
+static int do_color(void);
int
main(int argc, char **argv)
@@ -301,6 +307,17 @@
case OPT_SUPPRESS_COMMON:
suppress_common = 1;
break;
+ case OPT_COLOR:
+ if (optarg == NULL || strncmp(optarg, "auto", 4) == 0)
+ colorflag = COLORFLAG_AUTO;
+ else if (strncmp(optarg, "always", 6) == 0)
+ colorflag = COLORFLAG_ALWAYS;
+ else if (strncmp(optarg, "never", 5) == 0)
+ colorflag = COLORFLAG_NEVER;
+ else
+ errx(2, "unsupported --color value '%s' (must be always, auto, or never)",
+ optarg);
+ break;
default:
usage();
break;
@@ -316,6 +333,12 @@
argc -= optind;
argv += optind;
+ if (do_color()) {
+ color = 1;
+ add_code = init_code(1, "32");
+ del_code = init_code(2, "31");
+ }
+
#ifdef __OpenBSD__
if (pledge("stdio rpath tmppath", NULL) == -1)
err(2, "pledge");
@@ -550,3 +573,45 @@
fprintf(stderr, "error: conflicting output format options.\n");
usage();
}
+
+static int
+do_color(void)
+{
+ const char *p, *p2;
+ int ret = 0;
+
+ p = getenv("CLICOLOR");
+ p2 = getenv("COLORTERM");
+
+ switch (colorflag) {
+ case COLORFLAG_AUTO:
+ if ((p != NULL && *p != '\0') || (p2 != NULL && *p2 != '\0'))
+ ret = isatty(STDOUT_FILENO);
+ break;
+ case COLORFLAG_ALWAYS:
+ ret = 1;
+ break;
+ case COLORFLAG_NEVER:
+ ret = 0;
+ break;
+ }
+
+ return (ret);
+}
+
+static const char *
+init_code(int i, const char *rv)
+{
+ char *p, *env;
+ int j;
+
+ env = getenv("DIFFCOLORS");
+ if (env != NULL && *env != '\0') {
+ p = strdup(env);
+ for (j = 0; j < i; j++)
+ strsep(&p, ":");
+ if (p != NULL)
+ return (p);
+ }
+ return (rv);
+}
Index: usr.bin/diff/diffreg.c
===================================================================
--- usr.bin/diff/diffreg.c
+++ usr.bin/diff/diffreg.c
@@ -1196,13 +1196,23 @@
}
}
if (diff_format == D_SIDEBYSIDE) {
+ if (color == 1 && (a > b))
+ printf("\033[%sm", add_code);
+ else if (color == 1 && (c > d))
+ printf("\033[%sm", del_code);
if (a > b) {
print_space(0, hw + padding , *pflags);
} else {
nc = fetch(ixold, a, b, f1, '\0', 1, *pflags);
print_space(nc, hw - nc + padding, *pflags);
}
- printf("%c", (a>b)? '>' : ((c>d)? '<' : '|'));
+ if (color == 1 && (a > b))
+ printf("\033[%sm", add_code);
+ else if (color == 1 && (c > d))
+ printf("\033[%sm", del_code);
+ printf("%c", (a > b) ? '>' : ((c > d) ? '<' : '|'));
+ if (color == 1 && (c > d))
+ printf("\033[m");
print_space(hw + padding + 1 , padding, *pflags);
fetch(ixnew, c, d, f2, '\0', 0, *pflags);
printf("\n");
@@ -1276,6 +1286,10 @@
nc = hw;
if ((diff_format != D_IFDEF && diff_format != D_GFORMAT) &&
ch != '\0') {
+ if (color == 1 && (ch == '>' || ch == '+'))
+ printf("\033[%sm", add_code);
+ else if (color == 1 && (ch == '<' || ch == '-'))
+ printf("\033[%sm", del_code);
printf("%c", ch);
if (Tflag && (diff_format == D_NORMAL ||
diff_format == D_CONTEXT ||
@@ -1357,6 +1371,8 @@
}
}
}
+ if (color == 1)
+ printf("\033[m");
return col;
}

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 15, 9:40 PM (20 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
14648388
Default Alt Text
D30545.id94573.diff (7 KB)

Event Timeline