-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeter.c
433 lines (368 loc) · 11 KB
/
meter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// SPDX-FileCopyrightText: Steven Ward
// SPDX-License-Identifier: OSL-3.0
#include "meter.h"
#include "util.h"
// https://www.unicode.org/charts/PDF/U0000.pdf
static const wchar_t SPACE = 0x0020;
// https://www.unicode.org/charts/PDF/U2580.pdf
static const wchar_t UPPER_HALF_BLOCK = 0x2580;
static const wchar_t LOWER_ONE_EIGHTH_BLOCK = 0x2581;
static const wchar_t LOWER_ONE_QUARTER_BLOCK = 0x2582;
static const wchar_t LOWER_THREE_EIGHTHS_BLOCK = 0x2583;
static const wchar_t LOWER_HALF_BLOCK = 0x2584;
static const wchar_t LOWER_FIVE_EIGHTHS_BLOCK = 0x2585;
static const wchar_t LOWER_THREE_QUARTERS_BLOCK = 0x2586;
static const wchar_t LOWER_SEVEN_EIGHTHS_BLOCK = 0x2587;
static const wchar_t FULL_BLOCK = 0x2588;
static const wchar_t LEFT_SEVEN_EIGHTHS_BLOCK = 0x2589;
static const wchar_t LEFT_THREE_QUARTERS_BLOCK = 0x258A;
static const wchar_t LEFT_FIVE_EIGHTHS_BLOCK = 0x258B;
static const wchar_t LEFT_HALF_BLOCK = 0x258C;
static const wchar_t LEFT_THREE_EIGHTHS_BLOCK = 0x258D;
static const wchar_t LEFT_ONE_QUARTER_BLOCK = 0x258E;
static const wchar_t LEFT_ONE_EIGHTH_BLOCK = 0x258F;
static const wchar_t RIGHT_HALF_BLOCK = 0x2590;
static const wchar_t UPPER_ONE_EIGHTH_BLOCK = 0x2594;
static const wchar_t RIGHT_ONE_EIGHTH_BLOCK = 0x2595;
// https://www.unicode.org/charts/PDF/U1FB00.pdf
static const wchar_t VERTICAL_ONE_EIGHTH_BLOCK_2 = 0x1FB70;
static const wchar_t VERTICAL_ONE_EIGHTH_BLOCK_3 = 0x1FB71;
static const wchar_t VERTICAL_ONE_EIGHTH_BLOCK_4 = 0x1FB72;
static const wchar_t VERTICAL_ONE_EIGHTH_BLOCK_5 = 0x1FB73;
static const wchar_t VERTICAL_ONE_EIGHTH_BLOCK_6 = 0x1FB74;
static const wchar_t VERTICAL_ONE_EIGHTH_BLOCK_7 = 0x1FB75;
static const wchar_t HORIZONTAL_ONE_EIGHTH_BLOCK_2 = 0x1FB76;
static const wchar_t HORIZONTAL_ONE_EIGHTH_BLOCK_3 = 0x1FB77;
static const wchar_t HORIZONTAL_ONE_EIGHTH_BLOCK_4 = 0x1FB78;
static const wchar_t HORIZONTAL_ONE_EIGHTH_BLOCK_5 = 0x1FB79;
static const wchar_t HORIZONTAL_ONE_EIGHTH_BLOCK_6 = 0x1FB7A;
static const wchar_t HORIZONTAL_ONE_EIGHTH_BLOCK_7 = 0x1FB7B;
static const wchar_t UPPER_ONE_QUARTER_BLOCK = 0x1FB82;
static const wchar_t UPPER_THREE_EIGHTHS_BLOCK = 0x1FB83;
static const wchar_t UPPER_FIVE_EIGHTHS_BLOCK = 0x1FB84;
static const wchar_t UPPER_THREE_QUARTERS_BLOCK = 0x1FB85;
static const wchar_t UPPER_SEVEN_EIGHTHS_BLOCK = 0x1FB86;
static const wchar_t RIGHT_ONE_QUARTER_BLOCK = 0x1FB87;
static const wchar_t RIGHT_THREE_EIGHTHS_BLOCK = 0x1FB88;
static const wchar_t RIGHT_FIVE_EIGHTHS_BLOCK = 0x1FB89;
static const wchar_t RIGHT_THREE_QUARTERS_BLOCK = 0x1FB8A;
static const wchar_t RIGHT_SEVEN_EIGHTHS_BLOCK = 0x1FB8B;
static const wchar_t lower_blocks[] = {
SPACE,
LOWER_ONE_EIGHTH_BLOCK,
LOWER_ONE_QUARTER_BLOCK,
LOWER_THREE_EIGHTHS_BLOCK,
LOWER_HALF_BLOCK,
LOWER_FIVE_EIGHTHS_BLOCK,
LOWER_THREE_QUARTERS_BLOCK,
LOWER_SEVEN_EIGHTHS_BLOCK,
FULL_BLOCK,
};
static const size_t num_lower_blocks = LEN(lower_blocks);
static const wchar_t left_blocks[] = {
SPACE,
LEFT_ONE_EIGHTH_BLOCK,
LEFT_ONE_QUARTER_BLOCK,
LEFT_THREE_EIGHTHS_BLOCK,
LEFT_HALF_BLOCK,
LEFT_FIVE_EIGHTHS_BLOCK,
LEFT_THREE_QUARTERS_BLOCK,
LEFT_SEVEN_EIGHTHS_BLOCK,
FULL_BLOCK,
};
static const size_t num_left_blocks = LEN(left_blocks);
static const wchar_t ver_lines[] = {
LEFT_ONE_EIGHTH_BLOCK,
VERTICAL_ONE_EIGHTH_BLOCK_2,
VERTICAL_ONE_EIGHTH_BLOCK_3,
VERTICAL_ONE_EIGHTH_BLOCK_4,
VERTICAL_ONE_EIGHTH_BLOCK_5,
VERTICAL_ONE_EIGHTH_BLOCK_6,
VERTICAL_ONE_EIGHTH_BLOCK_7,
RIGHT_ONE_EIGHTH_BLOCK,
};
static const size_t num_ver_lines = LEN(ver_lines);
static const wchar_t hor_lines[] = {
LOWER_ONE_EIGHTH_BLOCK,
HORIZONTAL_ONE_EIGHTH_BLOCK_7,
HORIZONTAL_ONE_EIGHTH_BLOCK_6,
HORIZONTAL_ONE_EIGHTH_BLOCK_5,
HORIZONTAL_ONE_EIGHTH_BLOCK_4,
HORIZONTAL_ONE_EIGHTH_BLOCK_3,
HORIZONTAL_ONE_EIGHTH_BLOCK_2,
UPPER_ONE_EIGHTH_BLOCK,
};
static const size_t num_hor_lines = LEN(hor_lines);
static const wchar_t upper_blocks[] = {
SPACE,
UPPER_ONE_EIGHTH_BLOCK,
UPPER_ONE_QUARTER_BLOCK,
UPPER_THREE_EIGHTHS_BLOCK,
UPPER_HALF_BLOCK,
UPPER_FIVE_EIGHTHS_BLOCK,
UPPER_THREE_QUARTERS_BLOCK,
UPPER_SEVEN_EIGHTHS_BLOCK,
FULL_BLOCK,
};
static const size_t num_upper_blocks = LEN(upper_blocks);
static const wchar_t right_blocks[] = {
SPACE,
RIGHT_ONE_EIGHTH_BLOCK,
RIGHT_ONE_QUARTER_BLOCK,
RIGHT_THREE_EIGHTHS_BLOCK,
RIGHT_HALF_BLOCK,
RIGHT_FIVE_EIGHTHS_BLOCK,
RIGHT_THREE_QUARTERS_BLOCK,
RIGHT_SEVEN_EIGHTHS_BLOCK,
FULL_BLOCK,
};
static const size_t num_right_blocks = LEN(right_blocks);
static void
clamp(double* x)
{
if (*x < 0)
*x = 0;
else if (*x > 1)
*x = 1;
}
/** Map the real number \a x within the interval <code>[0, 1]</code> to an
* integer within the interval <code>[0, b)</code>.
*
* \pre \a b is at least \c 1.
*/
static size_t
map_to_uint(double x, size_t b)
{
clamp(&x);
// round to nearest int
return (size_t)(x * (b - 1U) + 0.5);
}
/** Calculate values of the meter segments.
*
* \pre \a x is within the interval <code>[0, 1]</code>.
*
* \pre \a blocks_len is at least \c 1.
*
* \a meter_width does not include the terminating null character.
*
* \a blocks_len is the length of the wide character array used for the meter.
*/
static void
calc_meter_segments(double x, size_t meter_width, size_t blocks_len,
size_t* left_width, size_t* blocks_index, size_t* right_width)
{
*left_width = 0;
*blocks_index = (size_t)-1;
*right_width = 0;
if (meter_width == 0 || blocks_len == 0)
return;
clamp(&x);
// truncate
*left_width = (size_t)(x * meter_width);
if (*left_width == meter_width)
return;
const double frac = x * meter_width - (double)((size_t)(x * meter_width));
// This produces a preferred distribution at the ends of the meter.
// round to nearest int
*blocks_index = (size_t)(frac * (blocks_len - 1U) + 0.5);
// Use this to get an alternative distribution.
// truncate
//*blocks_index = (size_t)(frac * blocks_len);
*right_width = meter_width - *left_width - 1U;
}
/** Return a Unicode lower block character (whose height is proportional to \a
* x).
*
* \pre \a x is within the interval <code>[0, 1]</code>.
*/
wchar_t
lower_blocks_1(double x)
{
return lower_blocks[map_to_uint(x, num_lower_blocks)];
}
/** Return a Unicode horizontal 1/8 block character (whose height is
* proportional to \a x).
*
* \pre \a x is within the interval <code>[0, 1]</code>.
*/
wchar_t
hor_lines_1(double x)
{
return hor_lines[map_to_uint(x, num_hor_lines)];
}
/** Return a Unicode upper block character (whose height is proportional to \a
* x).
*
* \pre \a x is within the interval <code>[0, 1]</code>.
*/
wchar_t
upper_blocks_1(double x)
{
return upper_blocks[map_to_uint(x, num_upper_blocks)];
}
/** Fill a meter with Unicode left block characters.
*
* The filled region is proportional to \a x, starting at the left.
*
* \pre \a x is within the interval [0, 1].
*
* \pre \a meter is a buffer capable of holding \a meter_width wide characters
* (not including the terminating null character).
*
* It is the caller's responsibility to null-terminate the \a meter buffer.
*/
void
left_blocks_meter(double x, wchar_t* meter, size_t meter_width)
{
size_t left_width, blocks_index, right_width, i;
if (meter_width == 0)
return;
calc_meter_segments(x, meter_width, num_left_blocks,
&left_width, &blocks_index, &right_width);
for (i = 0; i < left_width; ++i)
{
*meter++ = FULL_BLOCK;
}
if (blocks_index != (size_t)-1)
*meter++ = left_blocks[blocks_index];
for (i = 0; i < right_width; ++i)
{
*meter++ = SPACE;
}
}
/** Fill a meter with a Unicode vertical 1/8 block character.
*
* The position of the filled character is proportional to \a x, starting at the
* left.
*
* \pre \a x is within the interval [0, 1].
*
* \pre \a meter is a buffer capable of holding \a meter_width wide characters
* (not including the terminating null character).
*
* It is the caller's responsibility to null-terminate the \a meter buffer.
*/
void
ver_lines_meter(double x, wchar_t* meter, size_t meter_width)
{
size_t left_width, blocks_index, right_width, i;
if (meter_width == 0)
return;
calc_meter_segments(x, meter_width, num_ver_lines,
&left_width, &blocks_index, &right_width);
if (left_width == meter_width)
{
// Special case when the vertical line will be at the right end
for (i = 1; i < left_width; ++i)
{
*meter++ = SPACE;
}
*meter++ = RIGHT_ONE_EIGHTH_BLOCK;
}
else
{
for (i = 0; i < left_width; ++i)
{
*meter++ = SPACE;
}
if (blocks_index != (size_t)-1)
*meter++ = ver_lines[blocks_index];
for (i = 0; i < right_width; ++i)
{
*meter++ = SPACE;
}
}
}
/** Fill a meter with Unicode right block characters.
*
* The filled region is proportional to \a x, starting at the right.
*
* \pre \a x is within the interval [0, 1].
*
* \pre \a meter is a buffer capable of holding \a meter_width wide characters
* (not including the terminating null character).
*
* It is the caller's responsibility to null-terminate the \a meter buffer.
*/
void
right_blocks_meter(double x, wchar_t* meter, size_t meter_width)
{
size_t left_width, blocks_index, right_width, i;
if (meter_width == 0)
return;
calc_meter_segments(x, meter_width, num_right_blocks,
&left_width, &blocks_index, &right_width);
// left_width and right_width are swapped
for (i = 0; i < right_width; ++i)
{
*meter++ = SPACE;
}
if (blocks_index != (size_t)-1)
*meter++ = right_blocks[blocks_index];
for (i = 0; i < left_width; ++i)
{
*meter++ = FULL_BLOCK;
}
}
/** Fill a meter with ASCII characters.
*
* The filled region is proportional to \a x, starting at the left.
*
* \pre \a x is within the interval <code>[0, 1]</code>.
*
* \pre meter is a buffer capable of holding \a meter_width characters
* (not including the terminating null character).
*
* It is the caller's responsibility to null-terminate the \a meter buffer.
*/
void
left_char_meter(double x, char* meter, size_t meter_width,
char fill, char unfill)
{
size_t i;
if (meter_width == 0)
return;
clamp(&x);
// round to nearest int
const size_t num_filled = (size_t)(x * meter_width + 0.5);
[[maybe_unused]] const size_t num_unfilled = meter_width - num_filled;
for (i = 0; i < num_filled; ++i)
{
meter[i] = fill;
}
for (; i < meter_width; ++i)
{
meter[i] = unfill;
}
}
/** Fill a meter with ASCII characters.
*
* The filled region is proportional to \a x, starting at the right.
*
* \pre \a x is within the interval <code>[0, 1]</code>.
*
* \pre meter is a buffer capable of holding \a meter_width characters
* (not including the terminating null character).
*
* It is the caller's responsibility to null-terminate the \a meter buffer.
*/
void
right_char_meter(double x, char* meter, size_t meter_width,
char fill, char unfill)
{
size_t i;
if (meter_width == 0)
return;
clamp(&x);
// round to nearest int
const size_t num_filled = (size_t)(x * meter_width + 0.5);
const size_t num_unfilled = meter_width - num_filled;
for (i = 0; i < num_unfilled; ++i)
{
meter[i] = unfill;
}
for (; i < meter_width; ++i)
{
meter[i] = fill;
}
}