-
Notifications
You must be signed in to change notification settings - Fork 61
/
output.c
89 lines (77 loc) · 1.71 KB
/
output.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2022 Luca Ceresoli <[email protected]>
*
* Output routines to conditionally show testing status.
*
* out_initialize() must be called at program startup and disabled status
* output if stdout is not a tty. The other functions print test progress,
* but only if on a tty.
*/
#include <stdio.h>
#include <unistd.h>
#include "output.h"
static int show_progress = 1;
static int wheel_pos;
void out_initialize()
{
show_progress = isatty(STDOUT_FILENO);
}
void out_test_start()
{
if (show_progress) {
printf(" ");
fflush(stdout);
}
}
void out_test_setting(unsigned int j)
{
if (show_progress) {
printf("\b\b\b\b\b\b\b\b\b\b\b");
printf("setting %3u", j);
fflush(stdout);
}
}
void out_test_testing(unsigned int j)
{
if (show_progress) {
printf("\b\b\b\b\b\b\b\b\b\b\b");
printf("testing %3u", j);
fflush(stdout);
}
}
void out_test_end()
{
if (show_progress) {
printf("\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b");
fflush(stdout);
}
}
void out_wheel_start()
{
if (show_progress) {
putchar(' ');
fflush(stdout);
wheel_pos = 0;
}
}
void out_wheel_advance(unsigned int i)
{
static const unsigned int wheel_often = 2500;
static const unsigned int n_chars = 4;
char wheel_char[4] = {'-', '\\', '|', '/'};
if (show_progress) {
if (!(i % wheel_often)) {
putchar('\b');
putchar(wheel_char[++wheel_pos % n_chars]);
fflush(stdout);
}
}
}
void out_wheel_end()
{
if (show_progress) {
printf("\b \b");
fflush(stdout);
}
}