void welcome(void){ // TODO make a center function size_t margin, msglen; char *spaces; const char welcome_msg[] = "WELCOME TO TIC TAC TOE"; msglen = strlen(welcome_msg); if(get_term_width()<msglen) margin = 0; else margin = get_term_width() - strlen(welcome_msg) / 2; spaces = malloc(margin + 1); memset(spaces, ' ', margin); spaces[margin]=0; printf("%s%s\n\n", spaces, welcome_msg); }
void update_ics_width () { static int old_width = 0; int new_width = get_term_width(); if (old_width != new_width) ics_printf("set width %d\n", new_width); old_width = new_width; }
static void print_alternate_output(axel_t *axel) { long long int done=axel->bytes_done; long long int total=axel->size; int i,j=0,offset,end; double now = gettime(); int width = get_term_width() - 30; char progress[width+1]; for(i=0;i<width;i++) progress[i] = '.'; for(i=0;i<axel->conf->num_connections;i++) { offset = ((double)(axel->conn[i].currentbyte)/(total+1)*(width+1)); if(axel->conn[i].currentbyte<axel->conn[i].lastbyte) { if(now <= axel->conn[i].last_transfer + axel->conf->connection_timeout/2 ) progress[offset] = i+'0'; else progress[offset] = '#'; } end = ((double)(axel->conn[i].lastbyte)/(total+1)*(width+1)); for(j=offset+1;j<end;j++) progress[j] = ' '; } progress[width] = '\0'; printf("\r[%3ld%%] [%s", min(100,(long)(done*100./total+.5) ), progress ); if(axel->bytes_per_second > 1048576) printf( "] [%6.1fMB/s]", (double) axel->bytes_per_second / (1024*1024) ); else if(axel->bytes_per_second > 1024) printf( "] [%6.1fKB/s]", (double) axel->bytes_per_second / 1024 ); else printf( "] [%6.1fB/s]", (double) axel->bytes_per_second ); if(done<total) { int seconds,minutes,hours,days; seconds=axel->finish_time - now; minutes=seconds/60;seconds-=minutes*60; hours=minutes/60;minutes-=hours*60; days=hours/24;hours-=days*24; if(days) printf(" [%2dd%2d]",days,hours); else if(hours) printf(" [%2dh%02d]",hours,minutes); else printf(" [%02d:%02d]",minutes,seconds); } fflush( stdout ); }
void write_status_line(struct MPContext *mpctx, const char *line) { struct MPOpts *opts = mpctx->opts; if (opts->slave_mode) { MP_MSG(mpctx, MSGL_STATUS, "%s\n", line); } else if (erase_to_end_of_line) { MP_MSG(mpctx, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line); } else { int pos = strlen(line); int width = get_term_width() - pos; MP_MSG(mpctx, MSGL_STATUS, "%s%*s\r", line, width, ""); } }
progressbar::progressbar(const file& f, const quvi::media& m) : _update_interval(1), _expected_bytes(m.content_length()), _initial_bytes(f.initial_length()), _time_started(0), _last_update(0), _term_width(0), _dot_count(0), _count(0), _width(0), _file(f), _done(false), _mode(normal) { if (_initial_bytes > _expected_bytes) _expected_bytes = _initial_bytes; #ifdef WITH_RESIZE signal(SIGWINCH, handle_sigwinch); if (!_term_width || recv_sigwinch) { _term_width = get_term_width(); if (!_term_width) _term_width = default_term_width; } #else _term_width = default_term_width; #endif _width = _term_width; const po::variables_map map = cc::opts.map(); time(&_time_started); if (opts.flags.background) _mode = dotline; else { const std::string s = map["progressbar"].as<std::string>(); if (s == "simple") _mode = simple; else if (s == "dotline") _mode = dotline; } _update_interval = fabs(map["update-interval"].as<double>()); }
int OutputToProcess (ProcRef pr, char *message, int count, int *outError) { static int line = 0; ChildProc *cp = (ChildProc *) pr; int outCount; if (pr == NoProc) { if (appData.noJoin || !appData.useInternalWrap) outCount = fwrite(message, 1, count, stdout); else { int width = get_term_width(); int len = wrap(NULL, message, count, width, &line); char *msg = malloc(len); int dbgchk; if (!msg) outCount = fwrite(message, 1, count, stdout); else { dbgchk = wrap(msg, message, count, width, &line); if (dbgchk != len && appData.debugMode) fprintf(debugFP, "wrap(): dbgchk(%d) != len(%d)\n", dbgchk, len); outCount = fwrite(msg, 1, dbgchk, stdout); free(msg); } } } else outCount = write(cp->fdTo, message, count); if (outCount == -1) *outError = errno; else *outError = 0; return outCount; }
int main( int argc, char *argv[] ) { char fn[MAX_STRING] = ""; int do_search = 0; search_t *search; conf_t conf[1]; axel_t *axel; int i, j, cur_head = 0; char *s; /* Set up internationalization (i18n) */ setlocale( LC_ALL, "" ); bindtextdomain( PACKAGE, LOCALEDIR ); textdomain( PACKAGE ); if( !conf_init( conf ) ) { return( 1 ); } opterr = 0; j = -1; while( 1 ) { int option; option = getopt_long( argc, argv, "s:n:o:S::NqvhVakH:U:", axel_options, NULL ); if( option == -1 ) break; switch( option ) { case 'U': strncpy( conf->user_agent, optarg, MAX_STRING); break; case 'H': strncpy( conf->add_header[cur_head++], optarg, MAX_STRING ); break; case 's': if( !sscanf( optarg, "%i", &conf->max_speed ) ) { print_help(); return( 1 ); } break; case 'n': if( !sscanf( optarg, "%i", &conf->num_connections ) ) { print_help(); return( 1 ); } break; case 'o': strncpy( fn, optarg, MAX_STRING ); break; case 'S': do_search = 1; if( optarg != NULL ) if( !sscanf( optarg, "%i", &conf->search_top ) ) { print_help(); return( 1 ); } break; case 'a': conf->alternate_output = 1; break; case 'k': conf->insecure = 1; break; case 'N': *conf->http_proxy = 0; break; case 'h': print_help(); return( 0 ); case 'v': if( j == -1 ) j = 1; else j ++; break; case 'V': print_version(); return( 0 ); case 'q': close( 1 ); conf->verbose = -1; if( open( "/dev/null", O_WRONLY ) != 1 ) { fprintf( stderr, _("Can't redirect stdout to /dev/null.\n") ); return( 1 ); } break; default: print_help(); return( 1 ); } } conf->add_header_count = cur_head; if( j > -1 ) conf->verbose = j; if ( conf->num_connections < 1) { print_help(); return( 1 ); } #ifdef HAVE_OPENSSL ssl_init( conf ); #endif /* HAVE_OPENSSL */ if( argc - optind == 0 ) { print_help(); return( 1 ); } else if( strcmp( argv[optind], "-" ) == 0 ) { s = malloc( MAX_STRING ); if (scanf( "%1024[^\n]s", s) != 1) { fprintf( stderr, _("Error when trying to read URL (Too long?).\n") ); return( 1 ); } } else { s = argv[optind]; if( strlen( s ) > MAX_STRING ) { fprintf( stderr, _("Can't handle URLs of length over %d\n" ), MAX_STRING ); return( 1 ); } } printf( _("Initializing download: %s\n"), s ); if( do_search ) { search = malloc( sizeof( search_t ) * ( conf->search_amount + 1 ) ); memset( search, 0, sizeof( search_t ) * ( conf->search_amount + 1 ) ); search[0].conf = conf; if( conf->verbose ) printf( _("Doing search...\n") ); i = search_makelist( search, s ); if( i < 0 ) { fprintf( stderr, _("File not found\n" ) ); return( 1 ); } if( conf->verbose ) printf( _("Testing speeds, this can take a while...\n") ); j = search_getspeeds( search, i ); search_sortlist( search, i ); if( conf->verbose ) { printf( _("%i usable servers found, will use these URLs:\n"), j ); j = min( j, conf->search_top ); printf( "%-60s %15s\n", "URL", "Speed" ); for( i = 0; i < j; i ++ ) printf( "%-70.70s %5i\n", search[i].url, search[i].speed ); printf( "\n" ); } axel = axel_new( conf, j, search ); free( search ); if( axel->ready == -1 ) { print_messages( axel ); axel_close( axel ); return( 1 ); } } else if( argc - optind == 1 ) { axel = axel_new( conf, 0, s ); if( axel->ready == -1 ) { print_messages( axel ); axel_close( axel ); return( 1 ); } } else { search = malloc( sizeof( search_t ) * ( argc - optind ) ); memset( search, 0, sizeof( search_t ) * ( argc - optind ) ); for( i = 0; i < ( argc - optind ); i ++ ) strncpy( search[i].url, argv[optind+i], MAX_STRING ); axel = axel_new( conf, argc - optind, search ); free( search ); if( axel->ready == -1 ) { print_messages( axel ); axel_close( axel ); return( 1 ); } } print_messages( axel ); if( s != argv[optind] ) { free( s ); } if( *fn ) { struct stat buf; if( stat( fn, &buf ) == 0 ) { if( S_ISDIR( buf.st_mode ) ) { size_t fnlen = strlen(fn); size_t axelfnlen = strlen(axel->filename); if (fnlen + 1 + axelfnlen + 1 > MAX_STRING) { fprintf( stderr, _("Filename too long!\n")); return ( 1 ); } fn[fnlen] = '/'; memcpy(fn+fnlen+1, axel->filename, axelfnlen); fn[fnlen + 1 + axelfnlen] = '\0'; } } sprintf( string, "%s.st", fn ); if( access( fn, F_OK ) == 0 && access( string, F_OK ) != 0 ) { fprintf( stderr, _("No state file, cannot resume!\n") ); return( 1 ); } if( access( string, F_OK ) == 0 && access( fn, F_OK ) != 0 ) { printf( _("State file found, but no downloaded data. Starting from scratch.\n" ) ); unlink( string ); } strcpy( axel->filename, fn ); } else { /* Local file existence check */ i = 0; s = axel->filename + strlen( axel->filename ); while( 1 ) { sprintf( string, "%s.st", axel->filename ); if( access( axel->filename, F_OK ) == 0 ) { if( axel->conn[0].supported ) { if( access( string, F_OK ) == 0 ) break; } } else { if( access( string, F_OK ) ) break; } sprintf( s, ".%i", i ); i ++; } } if( !axel_open( axel ) ) { print_messages( axel ); return( 1 ); } print_messages( axel ); axel_start( axel ); print_messages( axel ); if( conf->alternate_output ) { putchar('\n'); } else { if( axel->bytes_done > 0 ) /* Print first dots if resuming */ { putchar( '\n' ); print_commas( axel->bytes_done ); } } axel->start_byte = axel->bytes_done; /* Install save_state signal handler for resuming support */ signal( SIGINT, stop ); signal( SIGTERM, stop ); while( !axel->ready && run ) { long long int prev, done; prev = axel->bytes_done; axel_do( axel ); if( conf->alternate_output ) { if( !axel->message && prev != axel->bytes_done ) print_alternate_output( axel ); } else { /* The infamous wget-like 'interface'.. ;) */ done = ( axel->bytes_done / 1024 ) - ( prev / 1024 ); if( done && conf->verbose > -1 ) { for( i = 0; i < done; i ++ ) { i += ( prev / 1024 ); if( ( i % 50 ) == 0 ) { if( prev >= 1024 ) printf( " [%6.1fKB/s]", (double) axel->bytes_per_second / 1024 ); if( axel->size < 10240000 ) printf( "\n[%3lld%%] ", min( 100, 102400 * i / axel->size ) ); else printf( "\n[%3lld%%] ", min( 100, i / ( axel->size / 102400 ) ) ); } else if( ( i % 10 ) == 0 ) { putchar( ' ' ); } putchar( '.' ); i -= ( prev / 1024 ); } fflush( stdout ); } } if( axel->message ) { if(conf->alternate_output==1) { /* clreol-simulation */ putchar( '\r' ); for( i = get_term_width(); i > 0; i--) putchar( ' ' ); putchar( '\r' ); } else { putchar( '\n' ); } print_messages( axel ); if( !axel->ready ) { if(conf->alternate_output!=1) print_commas( axel->bytes_done ); else print_alternate_output(axel); } } else if( axel->ready ) { putchar( '\n' ); } } strcpy( string + MAX_STRING / 2, size_human( axel->bytes_done - axel->start_byte ) ); printf( _("\nDownloaded %s in %s. (%.2f KB/s)\n"), string + MAX_STRING / 2, time_human( gettime() - axel->start_time ), (double) axel->bytes_per_second / 1024 ); i = axel->ready ? 0 : 2; axel_close( axel ); return( i ); }
int progressbar::update(double now) { time_t tnow; time(&tnow); const time_t elapsed = tnow - _time_started; bool force_update = false; #ifdef WITH_RESIZE if (recv_sigwinch && _mode == normal) { const size_t old_term_width = _term_width; _term_width = get_term_width(); if (!_term_width) _term_width = default_term_width; if (_term_width != old_term_width) { _width = _term_width; force_update = true; } recv_sigwinch = 0; } #endif // WITH_RESIZE const bool inactive = now == 0; if (!_done) { if ((elapsed - _last_update) < _update_interval && !force_update) { return 0; } } else now = _expected_bytes; // Current size. const double size = (!_done) ? _initial_bytes + now : now; std::stringstream size_s; size_s.setf(std::ios::fixed); size_s << std::setprecision(1) << to_mb(size) << "M"; // Rate. double rate = elapsed ? (now/elapsed):0; std::stringstream rate_s, eta_s; rate_s.setf(std::ios::fixed); eta_s.setf(std::ios::fixed); if (!inactive) { // ETA. std::string eta; if (!_done) { const double left = (_expected_bytes - (now + _initial_bytes)) / rate; eta = to_s(static_cast<int>(left+0.5)); } else { rate = (_expected_bytes - _initial_bytes) / elapsed; eta = to_s(elapsed); } std::string unit = to_unit(rate); rate_s << std::setw(4) << std::setprecision(1) << rate << unit; eta_s << std::setw(6) << eta; } else // ETA: inactive (default). { rate_s << "--.-K/s"; eta_s << "--:--:--"; } // Percent. std::stringstream percent_s; int percent = 0; if (_expected_bytes > 0) { percent = static_cast<int>(100.0*size/_expected_bytes); if (percent < 100) percent_s << std::setw(2) << percent << "%"; else percent_s << "100%"; } // Filename. fs::path p = fs::system_complete(_file.path()); #if BOOST_FILESYSTEM_VERSION > 2 std::string fname = p.filename().string(); #else std::string fname = p.filename(); #endif switch (_mode) { default: case normal: _normal(size_s, rate_s, eta_s, percent, percent_s); break; case dotline: _dotline(size_s, rate_s, eta_s, percent_s); break; case simple: _simple(size_s, percent_s); break; } _last_update = elapsed; _count = now; return 0; }