summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <curtis@hopper.com>2017-06-29 10:42:22 -0400
committerJune McEnroe <curtis@hopper.com>2017-06-29 10:42:22 -0400
commite0fbc8942173f5c73f01c7327ced5acb2f53ecb9 (patch)
treeaa0ad57e411a49c7a00bb0e73800bbaa86564d76
parentSet tmux copy-mode-vi bindings for Tarmak (diff)
downloadsrc-e0fbc8942173f5c73f01c7327ced5acb2f53ecb9.tar.gz
src-e0fbc8942173f5c73f01c7327ced5acb2f53ecb9.zip
Add color to xx
Diffstat (limited to '')
-rwxr-xr-xcurtis/.bin/xx.c26
1 files changed, 18 insertions, 8 deletions
diff --git a/curtis/.bin/xx.c b/curtis/.bin/xx.c
index 269c9d55..1c77fcd5 100755
--- a/curtis/.bin/xx.c
+++ b/curtis/.bin/xx.c
@@ -20,13 +20,18 @@ static bool allZero(const uint8_t *buf, size_t len) {
 }
 
 enum {
-    FLAG_ASCII = 1,
-    FLAG_OFFSET = 2,
-    FLAG_SKIP = 4,
-    FLAG_UNDUMP = 8,
+    FLAG_ASCII  = 1 << 0,
+    FLAG_COLOR  = 1 << 1,
+    FLAG_OFFSET = 1 << 2,
+    FLAG_SKIP   = 1 << 3,
+    FLAG_UNDUMP = 1 << 4,
 };
 
-void dump(size_t cols, size_t group, uint8_t flags, FILE *file) {
+static void color(size_t i) {
+    printf("\x1b[%c%cm", (i % 8) ? '3' : '9', (char)(i % 8 + '0'));
+}
+
+static void dump(size_t cols, size_t group, uint8_t flags, FILE *file) {
     uint8_t buf[cols];
     size_t offset = 0, len = 0;
     for (;;) {
@@ -34,6 +39,8 @@ void dump(size_t cols, size_t group, uint8_t flags, FILE *file) {
         len = fread(buf, 1, sizeof(buf), file);
         if (!len) break;
 
+        if (flags & FLAG_COLOR) printf("\x1b[39m");
+
         if ((flags & FLAG_SKIP) && len == sizeof(buf)) {
             static bool skip;
             if (allZero(buf, len)) {
@@ -50,6 +57,7 @@ void dump(size_t cols, size_t group, uint8_t flags, FILE *file) {
 
         for (size_t i = 0; i < len; ++i) {
             if (group && i && !(i % group)) printf(" ");
+            if (flags & FLAG_COLOR) color(i);
             printf("%02x ", buf[i]);
         }
 
@@ -59,6 +67,7 @@ void dump(size_t cols, size_t group, uint8_t flags, FILE *file) {
             }
             printf(" ");
             for (size_t i = 0; i < len; ++i) {
+                if (flags & FLAG_COLOR) color(i);
                 printf("%c", isprint(buf[i]) ? buf[i] : '.');
             }
         }
@@ -68,7 +77,7 @@ void dump(size_t cols, size_t group, uint8_t flags, FILE *file) {
     }
 }
 
-void undump(FILE *file) {
+static void undump(FILE *file) {
     uint8_t byte;
     int match;
     while (1 == (match = fscanf(file, " %hhx", &byte))) {
@@ -84,16 +93,17 @@ int main(int argc, char *argv[]) {
     char *path = NULL;
 
     int opt;
-    while (0 < (opt = getopt(argc, argv, "ac:fg:hku"))) {
+    while (0 < (opt = getopt(argc, argv, "aCc:fg:hku"))) {
         switch (opt) {
             case 'a': flags ^= FLAG_ASCII; break;
+            case 'C': flags ^= FLAG_COLOR; break;
             case 'f': flags ^= FLAG_OFFSET; break;
             case 'k': flags ^= FLAG_SKIP; break;
             case 'u': flags ^= FLAG_UNDUMP; break;
             case 'c': cols = strtoul(optarg, NULL, 10); break;
             case 'g': group = strtoul(optarg, NULL, 10); break;
             default:
-                fprintf(stderr, "usage: xx [-afku] [-c cols] [-g group] [file]\n");
+                fprintf(stderr, "usage: xx [-aCfku] [-c cols] [-g group] [file]\n");
                 return (opt == 'h') ? EX_OK : EX_USAGE;
         }
     }