Index: utils/ofhttp/ProgressBar.h ================================================================== --- utils/ofhttp/ProgressBar.h +++ utils/ofhttp/ProgressBar.h @@ -20,17 +20,18 @@ @class OFTimer; @interface ProgressBar: OFObject { intmax_t _received, _lastReceived, _length, _resumedFrom; - OFDate *_startDate; - double _lastDrawn; - OFTimer *_timer; + OFDate *_startDate, *_lastReceivedDate; + OFTimer *_drawTimer, *_BPSTimer; bool _stopped; + float _BPS; } - initWithLength: (intmax_t)length resumedFrom: (intmax_t)resumedFrom; - (void)setReceived: (intmax_t)received; - (void)draw; +- (void)calculateBPS; - (void)stop; @end Index: utils/ofhttp/ProgressBar.m ================================================================== --- utils/ofhttp/ProgressBar.m +++ utils/ofhttp/ProgressBar.m @@ -41,15 +41,21 @@ void *pool = objc_autoreleasePoolPush(); _length = length; _resumedFrom = resumedFrom; _startDate = [[OFDate alloc] init]; - _timer = [[OFTimer + _lastReceivedDate = [[OFDate alloc] init]; + _drawTimer = [[OFTimer scheduledTimerWithTimeInterval: UPDATE_INTERVAL target: self selector: @selector(draw) repeats: true] retain]; + _BPSTimer = [[OFTimer + scheduledTimerWithTimeInterval: 1.0 + target: self + selector: @selector(calculateBPS) + repeats: true] retain]; objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; @@ -60,11 +66,12 @@ - (void)dealloc { [self stop]; - [_timer release]; + [_drawTimer release]; + [_BPSTimer release]; [super dealloc]; } - (void)setReceived: (intmax_t)received @@ -73,11 +80,11 @@ } - (void)_drawProgress { uint_fast8_t i; - float bars, percent, bps; + float bars, percent; bars = (float)(_resumedFrom + _received) / (_resumedFrom + _length) * BAR_WIDTH; percent = (float)(_resumedFrom + _received) / (_resumedFrom + _length) * 100; @@ -111,31 +118,24 @@ } [of_stdout writeFormat: @"▏ %6.2f%% ", percent]; if (percent == 100) - bps = (float)_received / -[_startDate timeIntervalSinceNow]; - else - bps = (float)(_received - _lastReceived) / UPDATE_INTERVAL; - - if (bps >= GIBIBYTE) - [of_stdout writeFormat: @"%7.2f GiB/s", bps / GIBIBYTE]; - else if (bps >= MEBIBYTE) - [of_stdout writeFormat: @"%7.2f MiB/s", bps / MEBIBYTE]; - else if (bps >= KIBIBYTE) - [of_stdout writeFormat: @"%7.2f KiB/s", bps / KIBIBYTE]; - else - [of_stdout writeFormat: @"%7.2f B/s ", bps]; - - _lastDrawn = [[OFDate date] timeIntervalSince1970]; - _lastReceived = _received; + _BPS = (float)_received / -[_startDate timeIntervalSinceNow]; + + if (_BPS >= GIBIBYTE) + [of_stdout writeFormat: @"%7.2f GiB/s", _BPS / GIBIBYTE]; + else if (_BPS >= MEBIBYTE) + [of_stdout writeFormat: @"%7.2f MiB/s", _BPS / MEBIBYTE]; + else if (_BPS >= KIBIBYTE) + [of_stdout writeFormat: @"%7.2f KiB/s", _BPS / KIBIBYTE]; + else + [of_stdout writeFormat: @"%7.2f B/s ", _BPS]; } - (void)_drawReceived { - float bps; - if (_resumedFrom + _received >= GIBIBYTE) [of_stdout writeFormat: @"\r %7.2f GiB ", (float)(_resumedFrom + _received) / GIBIBYTE]; else if (_resumedFrom + _received >= MEBIBYTE) @@ -149,37 +149,43 @@ else [of_stdout writeFormat: @"\r %jd bytes ", _resumedFrom + _received]; if (_stopped) - bps = (float)_received / -[_startDate timeIntervalSinceNow]; - else - bps = (float)(_received - _lastReceived) / UPDATE_INTERVAL; - - if (bps >= GIBIBYTE) - [of_stdout writeFormat: @"%7.2f GiB/s", bps / GIBIBYTE]; - else if (bps >= MEBIBYTE) - [of_stdout writeFormat: @"%7.2f MiB/s", bps / MEBIBYTE]; - else if (bps >= KIBIBYTE) - [of_stdout writeFormat: @"%7.2f KiB/s", bps / KIBIBYTE]; - else - [of_stdout writeFormat: @"%7.2f B/s ", bps]; - - _lastDrawn = [[OFDate date] timeIntervalSince1970]; - _lastReceived = _received; + _BPS = (float)_received / -[_startDate timeIntervalSinceNow]; + + if (_BPS >= GIBIBYTE) + [of_stdout writeFormat: @"%7.2f GiB/s", _BPS / GIBIBYTE]; + else if (_BPS >= MEBIBYTE) + [of_stdout writeFormat: @"%7.2f MiB/s", _BPS / MEBIBYTE]; + else if (_BPS >= KIBIBYTE) + [of_stdout writeFormat: @"%7.2f KiB/s", _BPS / KIBIBYTE]; + else + [of_stdout writeFormat: @"%7.2f B/s ", _BPS]; } - (void)draw { if (_length > 0) [self _drawProgress]; else [self _drawReceived]; } + +- (void)calculateBPS +{ + _BPS = (float)(_received - _lastReceived) / + -[_lastReceivedDate timeIntervalSinceNow];; + + _lastReceived = _received; + [_lastReceivedDate release]; + _lastReceivedDate = [[OFDate alloc] init]; +} - (void)stop { - [_timer invalidate]; + [_drawTimer invalidate]; + [_BPSTimer invalidate]; _stopped = true; } @end