Skip to content

Instantly share code, notes, and snippets.

@gvanem
Last active February 9, 2017 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvanem/b28fe02889e05005bba5 to your computer and use it in GitHub Desktop.
Save gvanem/b28fe02889e05005bba5 to your computer and use it in GitHub Desktop.
[Patch] Make GNU-diff context header more readable. A simple patch to get rid of the \tab in the diff header and aligning the '+++' and '---' lines on the timestamp. Thus making it more readable to see which file is newer or older.
--- a/src/context.c 2015-03-09 22:54:10
+++ b/context.c 2015-10-12 21:21:56
@@ -41,7 +41,8 @@
print_context_label (char const *mark,
struct file_data *inf,
char const *name,
- char const *label)
+ char const *label,
+ int alignment)
{
if (label)
fprintf (outfile, "%s %s\n", mark, label);
@@ -71,7 +72,7 @@
sprintf (buf, "%"PRIuMAX".%.9d", sec, nsec);
}
}
- fprintf (outfile, "%s %s\t%s\n", mark, name, buf);
+ fprintf (outfile, "%s %s %*s%s\n", mark, name, alignment, "", buf);
}
}
@@ -80,16 +81,40 @@
void
print_context_header (struct file_data inf[], char const *const *names, bool unidiff)
{
+ size_t len0 = strlen(names[0]) + (file_label[0] ? strlen(file_label[0]) : 0);
+ size_t len1 = strlen(names[1]) + (file_label[1] ? strlen(file_label[1]) : 0);
+ int align = 1;
+
if (unidiff)
{
- print_context_label ("---", &inf[0], names[0], file_label[0]);
- print_context_label ("+++", &inf[1], names[1], file_label[1]);
+ if (len0 >= len1)
+ align = 0;
+ else
+ align = len1 - len0;
+ print_context_label ("---", &inf[0], names[0], file_label[0], align);
+
+ if (len1 >= len0)
+ align = 0;
+ else
+ align = len0 - len1;
+ print_context_label ("+++", &inf[1], names[1], file_label[1], align);
}
else
{
- print_context_label ("***", &inf[0], names[0], file_label[0]);
- print_context_label ("---", &inf[1], names[1], file_label[1]);
+ if (len0 >= len1)
+ align = 0;
+ else
+ align = len1 - len0;
+ print_context_label ("***", &inf[0], names[0], file_label[0], align);
+
+ if (len1 >= len0)
+ align = 0;
+ else
+ align = len0 - len1;
+ print_context_label ("---", &inf[1], names[1], file_label[1], align);
}
}
@gvanem
Copy link
Author

gvanem commented Oct 12, 2015

More description here. The GNU-diff header typically looks like (with the unified option; -u3):

--- a/src/context.c \t 2015-03-09 22:54:10
+++ b/context.c \t 2015-10-12 21:21:56
@@ -41,7 +41,8 @@
...

The \t in the output is an Tabulator (ASCII 9, visible in some editors as 📑, ➡️ or whatever).

Depending on your terminal or editor and it's settings, it can be a pain to quickly see which of the 2 file-timestamps are oldest or newest. The context.diff was meant to fix that. See the 2 first lines above to see the improvement.

You'll need to apply the .diff and rebuild GNU-diff yourself. Good luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment