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
|
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
|
+
-
+
+
+
+
+
+
+
-
+
-
+
|
@try {
void *pool = objc_autoreleasePoolPush();
_length = length;
_resumedFrom = resumedFrom;
_startDate = [[OFDate alloc] init];
_lastReceivedDate = [[OFDate alloc] init];
_timer = [[OFTimer
_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;
}
return self;
}
- (void)dealloc
{
[self stop];
[_drawTimer release];
[_timer release];
[_BPSTimer release];
[super dealloc];
}
- (void)setReceived: (intmax_t)received
{
_received = received;
}
- (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;
[of_stdout writeString: @"\r ▕"];
|
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
|
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
|
-
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
-
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
+
|
for (i = 0; i < BAR_WIDTH - (uint_fast8_t)bars - 1; i++)
[of_stdout writeString: @" "];
}
[of_stdout writeFormat: @"▏ %6.2f%% ", percent];
if (percent == 100)
bps = (float)_received / -[_startDate timeIntervalSinceNow];
_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];
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];
[of_stdout writeFormat: @"%7.2f B/s ", _BPS];
_lastDrawn = [[OFDate date] timeIntervalSince1970];
_lastReceived = _received;
}
- (void)_drawReceived
{
float bps;
if (_resumedFrom + _received >= GIBIBYTE)
[of_stdout writeFormat:
@"\r %7.2f GiB ",
(float)(_resumedFrom + _received) / GIBIBYTE];
else if (_resumedFrom + _received >= MEBIBYTE)
[of_stdout writeFormat:
@"\r %7.2f MiB ",
(float)(_resumedFrom + _received) / MEBIBYTE];
else if (_resumedFrom + _received >= KIBIBYTE)
[of_stdout writeFormat:
@"\r %7.2f KiB ",
(float)(_resumedFrom + _received) / KIBIBYTE];
else
[of_stdout writeFormat:
@"\r %jd bytes ", _resumedFrom + _received];
if (_stopped)
bps = (float)_received / -[_startDate timeIntervalSinceNow];
_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];
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];
[of_stdout writeFormat: @"%7.2f B/s ", _BPS];
_lastDrawn = [[OFDate date] timeIntervalSince1970];
_lastReceived = _received;
}
- (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
{
[_drawTimer invalidate];
[_timer invalidate];
[_BPSTimer invalidate];
_stopped = true;
}
@end
|