-
Notifications
You must be signed in to change notification settings - Fork 2
/
out.m
executable file
·132 lines (118 loc) · 3.1 KB
/
out.m
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#import "out.h"
int determine_screen_width (void) {
int fd;
struct winsize wsz;
fd = fileno (stderr);
if (ioctl (fd, TIOCGWINSZ, &wsz) < 0)
return 0;
return wsz.ws_col;
}
// mode of bar, 0 = inactive, 1 = active
int bar_mode = 0;
// percent of bar, negative is no bar
int bar_percent = -1;
char *bar_msg = NULL;
/**
progress_message places an "active" message for what Clutch is currently doing
*/
void progress_message(char *msg) {
if ([[ClutchConfiguration getValue:@"ProgressBar"] isEqualToString:@"NO"]) {
return;
}
if (bar_msg != NULL)
free(bar_msg);
bar_msg = malloc(strlen(msg) + 1);
strcpy(bar_msg, msg);
print_bar();
}
/**
progress_percent places a percentage for the task being done by Clutch
*/
void progress_percent(int percent) {
if ([[ClutchConfiguration getValue:@"ProgressBar"] isEqualToString:@"NO"]) {
return;
}
if ((bar_percent < percent - 5) || (percent == 100) || (percent < 0)) {
bar_percent = percent;
print_bar();
}
}
/**
progress_event places an event in the log
*/
void progress_event(char *text) {
if ([[ClutchConfiguration getValue:@"ProgressBar"] isEqualToString:@"NO"]) {
printf("%s\n", text);
return;
}
if (bar_mode == 1) {
// if the bar is there, we need to remove it, print the event, and then print the bar again
printf("\033[0G\033[J");
printf("%s\n", text);
bar_mode = 0;
print_bar();
} else {
printf("%s\n", text);
}
fflush(stdout);
}
/**
print the progress bar
*/
void print_bar(void) {
if (bar_mode == 1) {
printf("\033[0G\033[J");
}
bar_mode = 1;
int width = determine_screen_width();
if (bar_percent < 0) {
// do not draw the percentage
if (strlen(bar_msg) > (width - 5)) {
strncpy(bar_msg + width - 5, "...", 4);
}
printf("%s", bar_msg);
fflush(stdout);
} else {
// draw the percentage as half of the width
int pstart = floor(width / 2);
int pwidth = width - pstart;
int barwidth = ceil((pwidth - 7) * (((double) bar_percent) / 100));
// printf("bar percent: %f\n", ((double) bar_percent) / 100); exit(0);
int spacewidth = (pwidth - 7) - barwidth;
if (strlen(bar_msg) > (pstart - 5)) {
strncpy(bar_msg + pstart - 5, "...", 4);
}
printf("%s [", bar_msg);
for (int i=0;i<barwidth;i++) {
printf("=");
}
for (int i=0;i<spacewidth;i++) {
printf(" ");
}
printf("] %d%%", bar_percent);
fflush(stdout);
}
}
/**
* pause the bar (leave data intact so we can re-render later)
*/
void pause_bar(void) {
if (bar_mode == 1) {
printf("\033[0G\033[J");
fflush(stdout);
}
bar_mode = 0;
}
/**
stop the progress bar
*/
void stop_bar(void) {
if (bar_mode == 1) {
printf("\033[0G\033[J");
fflush(stdout);
bar_mode = 0;
free(bar_msg);
bar_msg = NULL;
bar_percent = -1;
}
}