29
29
#include < iomanip>
30
30
#include < regex>
31
31
#include < sstream>
32
+ #include < sys/ioctl.h>
33
+ #include < unistd.h>
32
34
33
35
#include < curl/curl.h>
34
36
#include < sys/stat.h>
@@ -47,7 +49,7 @@ void log_debug(const std::string &message) {
47
49
if (!log_verbose) {
48
50
return ;
49
51
}
50
- fprintf (stderr, " [ DEBUG] %s\n " , message.c_str ());
52
+ fprintf (stderr, " \036 [31m[ DEBUG] %s\033 [0m \n " , message.c_str ());
51
53
fflush (stderr);
52
54
}
53
55
@@ -63,7 +65,7 @@ void log_info_with_carriage_return(const std::string &message) {
63
65
}
64
66
65
67
void log_error (const std::string &message) {
66
- fprintf (stderr, " [ ERROR] %s\n " , message.c_str ());
68
+ fprintf (stderr, " \033 [31m[ ERROR] %s\033 [0m \n " , message.c_str ());
67
69
fflush (stderr);
68
70
}
69
71
@@ -212,6 +214,18 @@ get_model_metadata_from_hf(const std::string &repo, const std::string &file) {
212
214
return metadata;
213
215
}
214
216
217
+ int get_terminal_width () {
218
+ struct winsize w;
219
+ if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &w) == 0 ) {
220
+ return w.ws_col ;
221
+ } else {
222
+ return 80 ; // Default terminal width
223
+ }
224
+ }
225
+
226
+ std::chrono::steady_clock::time_point last_print_time =
227
+ std::chrono::steady_clock::now ();
228
+
215
229
// Progress bar function
216
230
int progress_callback (void *userdata, curl_off_t total, curl_off_t now,
217
231
curl_off_t , curl_off_t ) {
@@ -220,9 +234,15 @@ int progress_callback(void *userdata, curl_off_t total, curl_off_t now,
220
234
uint64_t size = metadata->size ;
221
235
uint64_t byte_offset = total - size;
222
236
uint64_t downloaded = now - byte_offset;
237
+ int terminal_width = get_terminal_width ();
238
+ auto elapsed = std::chrono::steady_clock::now () - last_print_time;
239
+
240
+ if (total > 0 && (now == downloaded ||
241
+ std::chrono::duration<double >(elapsed).count () > 0.08 )) {
242
+ last_print_time = std::chrono::steady_clock::now ();
223
243
224
- if (total > 0 ) {
225
- int width = 30 ; // Progress bar width
244
+ bool show_speed = terminal_width > 65 ;
245
+ int width = terminal_width - 65 + (show_speed ? 0 : 10 );
226
246
float percent = static_cast <float >(downloaded) / size;
227
247
int filled = static_cast <int >(percent * width);
228
248
@@ -231,22 +251,27 @@ int progress_callback(void *userdata, curl_off_t total, curl_off_t now,
231
251
1e-6 ); // Avoid division by zero
232
252
double remaining = (total - now) / speed;
233
253
234
- // Print progress bar
235
254
std::ostringstream progress;
236
- progress << " [" ;
237
- for (int i = 0 ; i < filled; ++i)
238
- progress << " #" ;
239
- for (int i = filled; i < width; ++i)
240
- progress << " " ;
241
- progress << " ] " << std::fixed << std::setprecision (2 ) << (percent * 100 )
242
- << " %" ;
255
+
256
+ // Print progress bar
257
+ if (terminal_width > 50 ) {
258
+ progress << " [" ;
259
+ for (int i = 0 ; i < filled; ++i)
260
+ progress << " #" ;
261
+ for (int i = filled; i < width; ++i)
262
+ progress << " " ;
263
+ progress << " ] " ;
264
+ }
265
+ progress << std::fixed << std::setprecision (2 ) << (percent * 100 ) << " %" ;
243
266
244
267
progress << " " << downloaded / 1024 / 1024 << " MB / "
245
- << size / 1024 / 1024 << " MB" ;
268
+ << size / 1024 / 1024 << " MB " ;
246
269
247
- // Print speed and ETA
248
- progress << " " << (speed / 1024 / 1024 )
249
- << " MB/s | ETA: " << static_cast <int >(remaining) << " s " ;
270
+ if (show_speed) {
271
+ progress << " " << (speed / 1024 / 1024 ) << " MB/s " ;
272
+ }
273
+ progress << " | ETA: " << std::fixed << std::setprecision (1 ) << remaining
274
+ << " s" ;
250
275
log_info_with_carriage_return (progress.str ());
251
276
}
252
277
if (stop_download) {
0 commit comments