firasuke / cross / slibtool

Forked from cross/slibtool 5 months ago
Clone

f1dd4b argv.h: usage screen generator: add colors.

Authored and Committed by midipix 8 years ago
    argv.h: usage screen generator: add colors.
    
        
file modified
+21 -0
src/internal/argv/argv.h CHANGED
@@ -15,6 +15,7 @@
15
15
#include <string.h>
16
16
#include <stdlib.h>
17
17
#include <stdio.h>
18
+ #include <unistd.h>
18
19
19
20
#define ARGV_VERBOSITY_NONE 0x00
20
21
#define ARGV_VERBOSITY_ERRORS 0x01
@@ -748,10 +749,21 @@ static void argv_usage(
748
749
const size_t optcap = 64;
749
750
const size_t width = 80;
750
751
const char indent[] = " ";
752
+ const char creset[] = "\x1b[0m";
753
+ const char cbold [] = "\x1b[1m";
754
+ const char cgreen[] = "\x1b[32m";
755
+ const char cblue [] = "\x1b[34m";
756
+ const char ccyan [] = "\x1b[36m";
757
+ const char * color = ccyan;
758
+ bool fcolor;
751
759
752
760
fshort = mode ? !strcmp(mode,"short") : 0;
753
761
flong = fshort ? 0 : mode && !strcmp(mode,"long");
754
762
fboth = !fshort && !flong;
763
+ fcolor = isatty(STDOUT_FILENO);
764
+
765
+ if (fcolor)
766
+ fprintf(stdout,"%s%s",cbold,cgreen);
755
767
756
768
if (header)
757
769
fprintf(stdout,"%s",header);
@@ -791,6 +803,12 @@ static void argv_usage(
791
803
desclen = (optlen < width / 2) ? width - optlen : optlen;
792
804
793
805
for (option=options; option->short_name || option->long_name; option++) {
806
+ /* color */
807
+ if (fcolor) {
808
+ color = (color == ccyan) ? cblue : ccyan;
809
+ fputs(color,stdout);
810
+ }
811
+
794
812
/* description, using either paradigm or argname if applicable */
795
813
snprintf(description,sizeof(description),option->description,
796
814
option->paradigm
@@ -870,6 +888,9 @@ static void argv_usage(
870
888
}
871
889
}
872
890
}
891
+
892
+ if (fcolor)
893
+ fputs(creset,stdout);
873
894
}
874
895
875
896
#endif