ObjFW  Diff

Differences From Artifact [3a96d93eb6]:

To Artifact [55b889a4e0]:


20
21
22
23
24
25
26

27
28


29
30
31

32
33
34
35
36
37
38
20
21
22
23
24
25
26
27


28
29

30

31
32
33
34
35
36
37
38







+
-
-
+
+
-

-
+







#import "OFDate.h"
#import "OFStdIOStream.h"
#import "OFTimer.h"
#import "OFLocale.h"

#import "ProgressBar.h"

static const float oneKibibyte = 1024;
#define GIBIBYTE (1024 * 1024 * 1024)
#define MEBIBYTE (1024 * 1024)
static const float oneMebibyte = 1024 * 1024;
static const float oneGibibyte = 1024 * 1024 * 1024;
#define KIBIBYTE (1024)

#define UPDATE_INTERVAL 0.1
static const OFTimeInterval updateInterval = 0.1;

#ifndef HAVE_TRUNCF
# define truncf(x) trunc(x)
#endif

@implementation ProgressBar
- (instancetype)initWithLength: (unsigned long long)length
46
47
48
49
50
51
52
53

54
55
56
57
58
59
60
46
47
48
49
50
51
52

53
54
55
56
57
58
59
60







-
+








		_useUnicode = useUnicode;
		_length = length;
		_resumedFrom = resumedFrom;
		_startDate = [[OFDate alloc] init];
		_lastReceivedDate = [[OFDate alloc] init];
		_drawTimer = [[OFTimer
		    scheduledTimerWithTimeInterval: UPDATE_INTERVAL
		    scheduledTimerWithTimeInterval: updateInterval
					    target: self
					  selector: @selector(draw)
					   repeats: true] retain];
		_BPSTimer = [[OFTimer
		    scheduledTimerWithTimeInterval: 1.0
					    target: self
					  selector: @selector(
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


192
193
194

195
196
197


198
199
200
201
202
203

204
205
206
207
208
209
210
211

212
213

214
215
216


217
218
219

220
221
222


223
224
225

226
227
228


229
230
231
232
233
234

235
236
237
238
239
240
241
242
243
244

245
246
247
248
249
250

251
252
253


254
255
256

257
258
259


260
261
262

263
264
265


266
267
268
269
270
271

272
273
274
275
276
277
278
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
192
193

194
195


196
197
198
199
200
201
202

203
204
205
206
207
208
209
210

211
212

213
214


215
216
217
218

219
220


221
222
223
224

225
226


227
228
229
230
231
232
233

234
235
236
237
238
239
240
241
242
243

244
245
246
247
248
249

250
251


252
253
254
255

256
257


258
259
260
261

262
263


264
265
266
267
268
269
270

271
272
273
274
275
276
277
278







-
+













-
+


-
+




-
+

-
+

-
+

-
+

-
+

-
+

-
+

-
+


-
+


-
+

-
+


-
+




-
+

-
+

-
+

-
+


-
+


-
+










-
+



-
+



-
+



-
+

-
-
+
+


-
+

-
-
+
+


-
+

-
-
+
+





-
+







-
+

-
+

-
-
+
+


-
+

-
-
+
+


-
+

-
-
+
+





-
+









-
+





-
+

-
-
+
+


-
+

-
-
+
+


-
+

-
-
+
+





-
+







}

- (void)_drawProgress
{
	float bars, percent;
	int columns, barWidth;

	if ((columns = of_stdout.columns) >= 0) {
	if ((columns = OFStdOut.columns) >= 0) {
		if (columns > 37)
			barWidth = columns - 37;
		else
			barWidth = 0;
	} else
		barWidth = 43;

	bars = (float)(_resumedFrom + _received) /
	    (float)(_resumedFrom + _length) * barWidth;
	percent = (float)(_resumedFrom + _received) /
	    (float)(_resumedFrom + _length) * 100;

	if (_useUnicode) {
		[of_stdout writeString: @"\r  ▕"];
		[OFStdOut writeString: @"\r  ▕"];

		for (size_t i = 0; i < (size_t)bars; i++)
			[of_stdout writeString: @"█"];
			[OFStdOut writeString: @"█"];
		if (bars < barWidth) {
			float rem = bars - truncf(bars);

			if (rem >= 0.875)
				[of_stdout writeString: @"▉"];
				[OFStdOut writeString: @"▉"];
			else if (rem >= 0.75)
				[of_stdout writeString: @"▊"];
				[OFStdOut writeString: @"▊"];
			else if (rem >= 0.625)
				[of_stdout writeString: @"▋"];
				[OFStdOut writeString: @"▋"];
			else if (rem >= 0.5)
				[of_stdout writeString: @"▌"];
				[OFStdOut writeString: @"▌"];
			else if (rem >= 0.375)
				[of_stdout writeString: @"▍"];
				[OFStdOut writeString: @"▍"];
			else if (rem >= 0.25)
				[of_stdout writeString: @"▎"];
				[OFStdOut writeString: @"▎"];
			else if (rem >= 0.125)
				[of_stdout writeString: @"▏"];
				[OFStdOut writeString: @"▏"];
			else
				[of_stdout writeString: @" "];
				[OFStdOut writeString: @" "];

			for (size_t i = 0; i < barWidth - (size_t)bars - 1; i++)
				[of_stdout writeString: @" "];
				[OFStdOut writeString: @" "];
		}

		[of_stdout writeFormat: @"▏ %,6.2f%% ", percent];
		[OFStdOut writeFormat: @"▏ %,6.2f%% ", percent];
	} else {
		[of_stdout writeString: @"\r  ["];
		[OFStdOut writeString: @"\r  ["];

		for (size_t i = 0; i < (size_t)bars; i++)
			[of_stdout writeString: @"#"];
			[OFStdOut writeString: @"#"];
		if (bars < barWidth) {
			float rem = bars - truncf(bars);

			if (rem >= 0.75)
				[of_stdout writeString: @"O"];
				[OFStdOut writeString: @"O"];
			else if (rem >= 0.5)
				[of_stdout writeString: @"o"];
				[OFStdOut writeString: @"o"];
			else if (rem >= 0.25)
				[of_stdout writeString: @"."];
				[OFStdOut writeString: @"."];
			else
				[of_stdout writeString: @" "];
				[OFStdOut writeString: @" "];

			for (size_t i = 0; i < barWidth - (size_t)bars - 1; i++)
				[of_stdout writeString: @" "];
				[OFStdOut writeString: @" "];
		}

		[of_stdout writeFormat: @"] %,6.2f%% ", percent];
		[OFStdOut writeFormat: @"] %,6.2f%% ", percent];
	}

	if (percent == 100) {
		double timeInterval = -_startDate.timeIntervalSinceNow;

		_BPS = (float)_received / (float)timeInterval;
		_ETA = timeInterval;
	}

	if (isinf(_ETA))
		[of_stdout writeString: @"--:--:-- "];
		[OFStdOut writeString: @"--:--:-- "];
	else if (_ETA >= 99 * 3600) {
		OFString *num = [OFString stringWithFormat:
		    @"%,4.2f", _ETA / (24 * 3600)];
		[of_stdout writeString: OF_LOCALIZED(@"eta_days",
		[OFStdOut writeString: OF_LOCALIZED(@"eta_days",
		    @"%[num] d ",
		    @"num", num)];
	} else
		[of_stdout writeFormat: @"%2u:%02u:%02u ",
		[OFStdOut writeFormat: @"%2u:%02u:%02u ",
		    (uint8_t)(_ETA / 3600), (uint8_t)(_ETA / 60) % 60,
		    (uint8_t)_ETA % 60];

	if (_BPS >= GIBIBYTE) {
	if (_BPS >= oneGibibyte) {
		OFString *num = [OFString stringWithFormat:
		    @"%,7.2f", _BPS / GIBIBYTE];
		[of_stdout writeString: OF_LOCALIZED(@"progress_gibs",
		    @"%,7.2f", _BPS / oneGibibyte];
		[OFStdOut writeString: OF_LOCALIZED(@"progress_gibs",
		    @"%[num] GiB/s",
		    @"num", num)];
	} else if (_BPS >= MEBIBYTE) {
	} else if (_BPS >= oneMebibyte) {
		OFString *num = [OFString stringWithFormat:
		    @"%,7.2f", _BPS / MEBIBYTE];
		[of_stdout writeString: OF_LOCALIZED(@"progress_mibs",
		    @"%,7.2f", _BPS / oneMebibyte];
		[OFStdOut writeString: OF_LOCALIZED(@"progress_mibs",
		    @"%[num] MiB/s",
		    @"num", num)];
	} else if (_BPS >= KIBIBYTE) {
	} else if (_BPS >= oneKibibyte) {
		OFString *num = [OFString stringWithFormat:
		    @"%,7.2f", _BPS / KIBIBYTE];
		[of_stdout writeString: OF_LOCALIZED(@"progress_kibs",
		    @"%,7.2f", _BPS / oneKibibyte];
		[OFStdOut writeString: OF_LOCALIZED(@"progress_kibs",
		    @"%[num] KiB/s",
		    @"num", num)];
	} else {
		OFString *num = [OFString stringWithFormat:
		    @"%,7.2f", _BPS];
		[of_stdout writeString: OF_LOCALIZED(@"progress_bps",
		[OFStdOut writeString: OF_LOCALIZED(@"progress_bps",
		    @"%[num] B/s  ",
		    @"num", num)];
	}
}

- (void)_drawReceived
{
	[of_stdout writeString: @"\r  "];
	[OFStdOut writeString: @"\r  "];

	if (_resumedFrom + _received >= GIBIBYTE) {
	if (_resumedFrom + _received >= oneGibibyte) {
		OFString *num = [OFString stringWithFormat:
		    @"%,7.2f", (float)(_resumedFrom + _received) / GIBIBYTE];
		[of_stdout writeString: OF_LOCALIZED(@"progress_gib",
		    @"%,7.2f", (float)(_resumedFrom + _received) / oneGibibyte];
		[OFStdOut writeString: OF_LOCALIZED(@"progress_gib",
		    @"%[num] GiB",
		    @"num", num)];
	} else if (_resumedFrom + _received >= MEBIBYTE) {
	} else if (_resumedFrom + _received >= oneMebibyte) {
		OFString *num = [OFString stringWithFormat:
		    @"%,7.2f", (float)(_resumedFrom + _received) / MEBIBYTE];
		[of_stdout writeString: OF_LOCALIZED(@"progress_mib",
		    @"%,7.2f", (float)(_resumedFrom + _received) / oneMebibyte];
		[OFStdOut writeString: OF_LOCALIZED(@"progress_mib",
		    @"%[num] MiB",
		    @"num", num)];
	} else if (_resumedFrom + _received >= KIBIBYTE) {
	} else if (_resumedFrom + _received >= oneKibibyte) {
		OFString *num = [OFString stringWithFormat:
		    @"%,7.2f", (float)(_resumedFrom + _received) / KIBIBYTE];
		[of_stdout writeString: OF_LOCALIZED(@"progress_kib",
		    @"%,7.2f", (float)(_resumedFrom + _received) / oneKibibyte];
		[OFStdOut writeString: OF_LOCALIZED(@"progress_kib",
		    @"%[num] KiB",
		    @"num", num)];
	} else {
		OFString *num = [OFString stringWithFormat:
		    @"%jd", _resumedFrom + _received];
		[of_stdout writeString: OF_LOCALIZED(@"progress_bytes",
		[OFStdOut writeString: OF_LOCALIZED(@"progress_bytes",
		    @"["
		    @"    ["
		    @"        {'num == 1': '1 byte '},"
		    @"        {'': '%[num] bytes'}"
		    @"    ]"
		    @"]".objectByParsingJSON,
		    @"num", num)];
	}

	[of_stdout writeString: @" "];
	[OFStdOut writeString: @" "];

	if (_stopped)
		_BPS = (float)_received /
		    -(float)_startDate.timeIntervalSinceNow;

	if (_BPS >= GIBIBYTE) {
	if (_BPS >= oneGibibyte) {
		OFString *num = [OFString stringWithFormat:
		    @"%,7.2f", _BPS / GIBIBYTE];
		[of_stdout writeString: OF_LOCALIZED(@"progress_gibs",
		    @"%,7.2f", _BPS / oneGibibyte];
		[OFStdOut writeString: OF_LOCALIZED(@"progress_gibs",
		    @"%[num] GiB/s",
		    @"num", num)];
	} else if (_BPS >= MEBIBYTE) {
	} else if (_BPS >= oneMebibyte) {
		OFString *num = [OFString stringWithFormat:
		    @"%,7.2f", _BPS / MEBIBYTE];
		[of_stdout writeString: OF_LOCALIZED(@"progress_mibs",
		    @"%,7.2f", _BPS / oneMebibyte];
		[OFStdOut writeString: OF_LOCALIZED(@"progress_mibs",
		    @"%[num] MiB/s",
		    @"num", num)];
	} else if (_BPS >= KIBIBYTE) {
	} else if (_BPS >= oneKibibyte) {
		OFString *num = [OFString stringWithFormat:
		    @"%,7.2f", _BPS / KIBIBYTE];
		[of_stdout writeString: OF_LOCALIZED(@"progress_kibs",
		    @"%,7.2f", _BPS / oneKibibyte];
		[OFStdOut writeString: OF_LOCALIZED(@"progress_kibs",
		    @"%[num] KiB/s",
		    @"num", num)];
	} else {
		OFString *num = [OFString stringWithFormat:
		    @"%,7.2f", _BPS];
		[of_stdout writeString: OF_LOCALIZED(@"progress_bps",
		[OFStdOut writeString: OF_LOCALIZED(@"progress_bps",
		    @"%[num] B/s  ",
		    @"num", num)];
	}
}

- (void)draw
{