-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspinner.c
87 lines (65 loc) · 2.07 KB
/
spinner.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
#include "spinner.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
// console handle
static HANDLE con = NULL;
// default color attributes
static WORD default_attributes = 0;
// windows uses this instead of ansi escape chars :sob:
static
const WORD colors[8] = {
0, // black
FOREGROUND_RED, // red
FOREGROUND_GREEN, // green
FOREGROUND_RED | FOREGROUND_GREEN, // yellow
FOREGROUND_BLUE, // blue
FOREGROUND_RED | FOREGROUND_BLUE, // magenta
FOREGROUND_GREEN | FOREGROUND_BLUE, // cyan
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE // white
};
static void init_handle(void) {
con = GetStdHandle(-11);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(con, & csbi);
default_attributes = csbi.wAttributes;
}
// function called by success(), error(), warn(), and info()
// after the thread was stopped
void print_post_exit(int prev_len,
const int color,
const int symbol_char_code,
const char * message,
const int message_len) {
if (con == NULL)
init_handle();
WriteConsoleA(con, "\r ", 2, NULL, NULL);
SetConsoleTextAttribute(con, colors[color - 48]);
WriteConsoleW(con, & symbol_char_code, 1, NULL, NULL);
SetConsoleTextAttribute(con, default_attributes);
printf(" %s", message);
while (message_len < prev_len) {
putchar(32);
prev_len--;
}
putchar('\n');
}
// function called every time the library is about to print a frame
void print_spinner_text(
const int color,
const char * frame,
const char * new_text,
const int new_text_size, int prev_len) {
if (con == NULL)
init_handle();
WriteConsoleA(con, "\r ", 2, NULL, NULL);
SetConsoleTextAttribute(con, colors[color - 48]);
printf("%s ", frame);
SetConsoleTextAttribute(con, default_attributes);
WriteConsole(con, new_text, new_text_size, NULL, NULL);
// this may seem tedious but it fixes a stdout glitch
while (new_text_size < prev_len) {
putchar(32);
prev_len--;
}
}