Skip to content

Commit 76ecd74

Browse files
committed
Fancify output
This detects if stdout is not a tty or is a dumb terminal, if so we don't spin. While here introduce colors for `ok` and `fail`, I hear it's hip these days, and haesbaert is nothing but hip.
1 parent 585769f commit 76ecd74

File tree

1 file changed

+66
-6
lines changed

1 file changed

+66
-6
lines changed

quark-test.c

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,53 @@ static int kflag; /* run kprobe tests */
2121

2222
#define msleep(_x) usleep((uint64_t)_x * 1000ULL)
2323

24+
enum {
25+
SANE,
26+
RED,
27+
GREEN
28+
};
29+
30+
static int
31+
fancy_tty(void)
32+
{
33+
char *term = getenv("TERM");
34+
35+
if (term == NULL || !strcmp(term, "dumb"))
36+
return (0);
37+
38+
return (isatty(STDOUT_FILENO) == 1);
39+
}
40+
41+
static int
42+
color(int color)
43+
{
44+
static int old;
45+
int ret;
46+
47+
if (!fancy_tty())
48+
return (SANE);
49+
50+
ret = old;
51+
52+
switch (color) {
53+
case SANE:
54+
printf("\033[0m");
55+
break;
56+
case RED:
57+
printf("\033[31m");
58+
break;
59+
case GREEN:
60+
printf("\033[32m");
61+
break;
62+
default:
63+
errx(1, "bad color %d", color);
64+
}
65+
66+
old = color;
67+
68+
return (ret);
69+
}
70+
2471
static char *
2572
binpath(void)
2673
{
@@ -53,6 +100,10 @@ static void
53100
spin(void)
54101
{
55102
static int ch;
103+
104+
if (!fancy_tty())
105+
return;
106+
56107
/* -\|/ */
57108
switch (ch) {
58109
case 0: /* FALLTHROUGH */
@@ -458,7 +509,7 @@ static int
458509
run_test(const struct test *t, struct quark_queue_attr *qa)
459510
{
460511
pid_t child;
461-
int status;
512+
int status, x, linepos;
462513
const char *be;
463514
int child_stderr[2];
464515
FILE *child_stream;
@@ -476,7 +527,9 @@ run_test(const struct test *t, struct quark_queue_attr *qa)
476527
else
477528
errx(1, "bad backend");
478529

479-
printf("%s @ %s: ", t->name, be);
530+
linepos = printf("%s @ %s", t->name, be);
531+
while (++linepos < 30)
532+
putchar('.');
480533
fflush(stdout);
481534

482535
/*
@@ -560,10 +613,15 @@ run_test(const struct test *t, struct quark_queue_attr *qa)
560613
if (waitpid(child, &status, 0) == -1)
561614
err(1, "waitpid");
562615

563-
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
616+
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
617+
x = color(GREEN);
564618
printf("ok\n");
565-
else
619+
color(x);
620+
} else {
621+
x = color(RED);
566622
printf("failed\n");
623+
color(x);
624+
}
567625
fflush(stdout);
568626

569627
/*
@@ -632,7 +690,7 @@ run_tests(int argc, char *argv[])
632690
int
633691
main(int argc, char *argv[])
634692
{
635-
int ch, failed;
693+
int ch, x, failed;
636694

637695
while ((ch = getopt(argc, argv, "bklNvV")) != -1) {
638696
switch (ch) {
@@ -667,7 +725,9 @@ main(int argc, char *argv[])
667725

668726
failed = run_tests(argc, argv);
669727

670-
printf("failed tests %d\n", failed);
728+
x = failed == 0 ? color(GREEN) : color(RED);
729+
printf("%d failures\n", failed);
730+
color(x);
671731

672732
return (failed);
673733
}

0 commit comments

Comments
 (0)