Repository navigation
Expand file tree
/
Copy pathprogress.c
More file actions
192 lines (156 loc) · 6.28 KB
/
Copy pathprogress.c
File metadata and controls
192 lines (156 loc) · 6.28 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <ncurses.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/utsname.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/time.h>
#include <ctype.h>
#include "types.h"
#include "globals.h"
WINDOW *progress;
// Function to create a dialog window with a title and two progress bars
void create_progress_dialog(int title_lines) {
int max_y, max_x;
getmaxyx(stdscr, max_y, max_x);
int width = max_x / 2;
int height = 9 + title_lines;
int start_y = (max_y - height) / 2;
int start_x = (max_x - width) / 2;
progress = newwin(height, width, start_y, start_x);
wbkgd(progress, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
wattron(progress, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
show_shadow(progress);
// Draw the borders and title
mvwaddch(progress, 1, 1, '+'); // Top left corner
mvwaddch(progress, 1, width - 2, '+'); // Top right corner
mvwaddch(progress, height - 2, 1, '+'); // Bottom left corner
mvwaddch(progress, height - 2, width - 2, '+'); // Bottom right corner
mvwhline(progress, 1, 2, '-', width - 4); // Top border
mvwhline(progress, height - 2, 2, '-', width - 4); // Bottom border
mvwvline(progress, 2, 1, '|', height - 4); // Left border
mvwvline(progress, 2, width - 2, '|', height - 4); // Right border
mvwhline(progress, height - 4, 2, '-', width - 4); // Horizontal line above buttons
mvwaddch(progress, height - 4, 1, '+'); // Left intersection
mvwaddch(progress, height - 4, width - 2, '+'); // Right intersection
wrefresh(progress);
}
int update_progress_dialog(char *title, int current_progress, int total_progress, char *infotext) {
int width, height;
getmaxyx(progress, height, width);
if (current_progress > 100) current_progress = 100;
if (total_progress > 100) total_progress = 100;
static int active_button = 0;
<
8000
div id="LC61" class="react-file-line html-div" data-testid="code-cell" data-line-number="61" style="position:relative"> int title_lines = lines(title);
if (title_lines < 1) title_lines = 1;
static int previous_title_lines = 1;
if (previous_title_lines < title_lines) {
delwin(progress);
create_progress_dialog(title_lines);
previous_title_lines = title_lines;
}
// cleanup stastic var for another dialog, when calling with empty or zero arguments
if (title == NULL && current_progress == 0 && total_progress == 0 && infotext == NULL) {
previous_title_lines = 1;
return -1;
}
// print title if provided
if (title != NULL) {
int title_line = 2;
char * title_copy = strdup(title);
char * line = strtok(title_copy, "\n");
while (line) {
mvwprintw(progress, title_line, 3, "%s", SHORTEN(line, width - 6));
line = strtok(NULL, "\n");
title_line++;
}
free(title_copy);
}
// Draw the progress bars if progress is provideed
if (infotext == NULL) {
mvwaddch(progress, 3 + title_lines, 3, '[');
mvwaddch(progress, 3 + title_lines, width - 8, ']');
mvwaddch(progress, 4 + title_lines, 3, '[');
mvwaddch(progress, 4 + title_lines, width - 8, ']');
mvwhline(progress, 3 + title_lines, 4, '.', width - 12);
mvwhline(progress, 4 + title_lines, 4, '.', width - 12);
mvwprintw(progress, 3 + title_lines, width - 7, "%3d%%", current_progress);
mvwprintw(progress, 4 + title_lines, width - 7, "%3d%%", total_progress);
int bar_width = width - 12;
int current_fill = (current_progress * bar_width) / 100;
int total_fill = (total_progress * bar_width) / 100;
for (int i = 0; i < current_fill; i++) { mvwaddch(progress, 3 + title_lines, 4 + i, '#'); }
for (int i = 0; i < total_fill; i++) { mvwaddch(progress, 4 + title_lines, 4 + i, '#'); }
} else {
int info_line = 2 + title_lines;
char * info_copy = strdup(infotext);
char * line = strtok(info_copy, "\n");
while (line) {
mvwprintw(progress, info_line, 3, "%s", SHORTEN(line, width - 6));
line = strtok(NULL, "\n");
info_line++;
}
free(info_copy);
}
char * buttons[] = {"Skip", "Abort", NULL};
int move_cursor_pos = 0;
int total_buttons_width = 0;
int i = 0;
while (buttons[i] != NULL) {
total_buttons_width += strlen(buttons[i]) + 4;
i++;
}
total_buttons_width += 2 * (i - 1);
int cursor_pos = (width - total_buttons_width) / 2;
int y_pos = 6;
i = 0;
while (buttons[i] != NULL) {
if (i == active_button) {
wattron(progress, COLOR_PAIR(COLOR_BLACK_ON_CYAN));
move_cursor_pos = cursor_pos + 2;
}
mvwprintw(progress, y_pos + title_lines, cursor_pos, "[ %s ]", buttons[i]);
if (i == active_button) {
wattron(progress, COLOR_PAIR(COLOR_BLACK_ON_WHITE));
}
cursor_pos += strlen(buttons[i]) + 6;
i++;
}
if (move_cursor_pos > 0) {
wmove(progress, y_pos + title_lines, move_cursor_pos);
}
wrefresh(progress);
timeout(0);
int ch = getch();
timeout(-1);
if (ch == KEY_LEFT) active_button--;
if (ch == KEY_RIGHT) active_button++;
if (active_button > 1) active_button = 0;
if (active_button < 0) active_button = 1;
if (ch == '\n') return active_button + 1;
return -1;
}
int update_progress_dialog_delta(char *title, int current_progress, int total_progress, char *infotext) {
static struct timeval last_time = {0};
struct timeval current_time;
gettimeofday(¤t_time, NULL);
if (title == NULL && current_progress == 0 && total_progress == 0 && infotext == NULL) {
last_time.tv_sec = 0;
last_time.tv_usec = 0;
update_progress_dialog(title, current_progress, total_progress, infotext);
return -1;
}
// Calculate the elapsed time in milliseconds
long elapsed_ms = (current_time.tv_sec - last_time.tv_sec) * 1000 +
(current_time.tv_usec - last_time.tv_usec) / 1000;
if ( current_progress == 100 || (last_time.tv_sec == 0 && last_time.tv_usec == 0) || elapsed_ms > 200) {
int t = update_progress_dialog(title, current_progress, total_progress, infotext);
if (t != -1) return t;
last_time = current_time; // update the last_time to curr
3173
ent_time
}
return -1;
}