-
Notifications
You must be signed in to change notification settings - Fork 1
/
color.c
724 lines (619 loc) · 17.3 KB
/
color.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/* $Id: color.c,v 1.41 2024/10/02 00:08:06 tom Exp $ */
#include <vttest.h>
#include <draw.h>
#include <esc.h>
#define MAX_COLORS 8
#define COLOR_BLACK 0
#define COLOR_RED 1
#define COLOR_GREEN 2
#define COLOR_YELLOW 3
#define COLOR_BLUE 4
#define COLOR_MAGENTA 5
#define COLOR_CYAN 6
#define COLOR_WHITE 7
static const char *colors[MAX_COLORS] =
{
"black", /* 30, 40 */
"red", /* 31, 41 */
"green", /* 32, 42 */
"yellow", /* 33, 43 */
"blue", /* 34, 44 */
"magenta", /* 35, 45 */
"cyan", /* 36, 46 */
"white" /* 37, 47 */
};
int do_colors = FALSE;
/*
* Pick an unusual color combination for testing, just in case the user's
* got the background set to something different.
*/
static void
c_sgr(const char *s)
{
char temp[80];
int reset = FALSE;
(void) strcpy(temp, s);
if (*temp == ';' || *temp == 0) {
reset = TRUE;
} else {
char *t;
for (t = temp; *t != 0; t++) {
if (((t[0] == '0')
&& (t == temp || t[-1] == ';')
&& (t[1] == 0 || t[1] == ';'))
|| ((t[0] == ';')
&& (t[1] == ';'))) {
reset = TRUE;
break;
}
}
}
if (reset && do_colors) {
sprintf(temp + strlen(temp), ";%d;%d", COLOR_YELLOW + 30, COLOR_BLUE + 40);
}
sgr(temp);
}
/*
* Some terminals will reset colors with SGR-0; I've added the 39, 49 codes for
* those that are ISO compliant. (The black/white codes are for emulators
* written by people who don't bother reading standards).
*/
static void
reset_all_colors(void)
{
sgr("0;40;37;39;49");
sgr("0");
}
static void
set_background(int bg)
{
if (do_colors) {
char temp[80];
(void) sprintf(temp, "4%d", bg);
sgr(temp);
}
}
static void
set_color_pair(int fg, int bg)
{
if (do_colors) {
char temp[80];
(void) sprintf(temp, "3%d;4%d", fg, bg);
sgr(temp);
}
}
static void
set_foreground(int fg)
{
if (do_colors) {
char temp[80];
(void) sprintf(temp, "3%d", fg);
sgr(temp);
}
}
static void
set_test_colors(void)
{
c_sgr("0");
}
static void
reset_test_colors(void)
{
/* Now, set the background again just in case there's a glitch */
set_foreground(COLOR_WHITE);
set_background(COLOR_BLACK);
}
/* Graphic rendition requires special handling with color, since SGR-0
* is supposed to reset the colors as well.
*/
static void
show_graphic_rendition(void)
{
ed(2);
/* *INDENT-OFF* */
cup( 1,20); printxx("Color/Graphic rendition test pattern:");
cup( 4, 1); c_sgr("0"); tprintf("vanilla");
cup( 4,40); c_sgr("0;1"); tprintf("bold");
cup( 6, 6); c_sgr(";4"); tprintf("underline");
cup( 6,45); c_sgr(";1");c_sgr("4");tprintf("bold underline");
cup( 8, 1); c_sgr("0;5"); tprintf("blink");
cup( 8,40); c_sgr("0;5;1"); tprintf("bold blink");
cup(10, 6); c_sgr("0;4;5"); tprintf("underline blink");
cup(10,45); c_sgr("0;1;4;5"); tprintf("bold underline blink");
cup(12, 1); c_sgr("1;4;5;0;7"); tprintf("negative");
cup(12,40); c_sgr("0;1;7"); tprintf("bold negative");
cup(14, 6); c_sgr("0;4;7"); tprintf("underline negative");
cup(14,45); c_sgr("0;1;4;7"); tprintf("bold underline negative");
cup(16, 1); c_sgr("1;4;;5;7"); tprintf("blink negative");
cup(16,40); c_sgr("0;1;5;7"); tprintf("bold blink negative");
cup(18, 6); c_sgr("0;4;5;7"); tprintf("underline blink negative");
cup(18,45); c_sgr("0;1;4;5;7"); tprintf("bold underline blink negative");
cup(20, 6); c_sgr(""); set_foreground(9); tprintf("original foreground");
cup(20,45); c_sgr(""); set_background(9); tprintf("original background");
/* *INDENT-ON* */
c_sgr(""); /* same as c_sgr("0") */
decscnm(FALSE); /* Inverse video off */
cup(max_lines - 1, 1);
el(0);
tprintf("Dark background. ");
holdit();
decscnm(TRUE); /* Inverse video */
cup(max_lines - 1, 1);
el(0);
tprintf("Light background. ");
holdit();
decscnm(FALSE);
}
static void
show_line_deletions(void)
{
int row;
ed(2);
cup(1, 1);
printxx("This test deletes every third line from a list, marking cursor with '*'.\n");
printxx("The foreground and background should be yellow(orange) and blue, respectively.\n");
for (row = 5; row <= max_lines; row++) {
cup(row, 1);
tprintf(" row %3d: this is some text", row);
}
for (row = 7; row <= max_lines; row += 2 /* 3 - deletion */ ) {
cup(row, 8);
dl(1);
putchar('*'); /* cursor should be in column 1 */
}
cup(3, 1);
holdit();
}
static void
show_line_insertions(void)
{
int row;
ed(2);
cup(1, 1);
printxx("This test inserts after every second line in a list, marking cursor with '*'.\n");
printxx("The foreground and background should be yellow(orange) and blue, respectively.\n");
for (row = 5; row <= max_lines; row++) {
cup(row, 1);
tprintf(" row %3d: this is some text", row);
}
for (row = 7; row <= max_lines; row += 3 /* 2 + insertion */ ) {
cup(row, 8);
il(1);
putchar('*'); /* cursor should be in column 1 */
}
cup(3, 1);
holdit();
}
static int
show_test_pattern(MENU_ARGS)
/* generate a color test pattern */
{
int i, j, k;
reset_all_colors();
ed(2);
cup(1, 1);
printxx("There are %d color combinations", MAX_COLORS * MAX_COLORS);
for (k = 0; k <= 11; k += 11) {
cup(k + 2, 1);
tprintf("%dx%d matrix of foreground/background colors, bright *",
MAX_COLORS, MAX_COLORS);
if (k) {
sgr("1");
tprintf("on");
sgr("0");
} else {
tprintf("off");
}
tprintf("*");
for (i = 0; i < MAX_COLORS; i++) {
cup(k + 3, (i + 1) * 8 + 1);
tprintf("%s", colors[i]);
}
for (i = 0; i < MAX_COLORS; i++) {
cup(k + i + 4, 1);
tprintf("%s", colors[i]);
}
for (i = 0; i < MAX_COLORS; i++) {
for (j = 0; j < MAX_COLORS; j++) {
if (k)
sgr("1");
set_color_pair(j, i);
cup(k + 4 + i, (j + 1) * 8 + 1);
tprintf("Hello");
reset_all_colors();
}
}
}
reset_all_colors();
cup(max_lines - 1, 1);
return MENU_HOLD;
}
/*
* Clear around the box for simple_bce_test().
*/
static void
simple_bce_erases(const BOX *box)
{
int i;
cup(box->top - 1, min_cols / 2);
ed(1); /* clear from home to cursor */
cuf(1);
el(0); /* clear from cursor to end of line */
cup(box->bottom + 1, min_cols / 2);
ed(0); /* clear from cursor to end */
cub(1);
el(1); /* clear to beginning of line */
for (i = box->top; i <= box->bottom; i++) {
cup(i, box->left - 1);
el(1);
cup(i, box->right + 1);
el(0);
}
}
/*
* "Real" color terminals support bce (background color erase).
*
* Set the foreground and background colors to something that's unusual.
* Then clear the screen (the background should stick) and draw some nested
* boxes (because that's simple). Use the ED, EL controls to clear away the
* outer box, so we can exercise the various parameter combinations of each
* of these.
*/
static int
simple_bce_test(MENU_ARGS)
{
BOX box1;
BOX box2;
static const char *text1[] =
{
"The screen background should be blue, with a box made of asterisks",
" and this caption, in orange (non-bold yellow). ",
" There should be no cells with the default foreground or background.",
0
};
static const char *text2[] =
{
"The screen background should be black, with a box made of asterisks",
" and this caption, in white (actually gray - it is not bold). ",
" Only the asterisk box should be in color.",
0
};
if (make_box_params(&box1, 3, 10) < 0
|| make_box_params(&box2, 7, 18) < 0)
return MENU_NOHOLD;
set_test_colors();
decaln();
draw_box_filled(&box1, 'X');
draw_box_outline(&box2, '*');
simple_bce_erases(&box2);
draw_box_caption(&box2, 1, text1);
cup(max_lines - 1, 1);
holdit();
reset_test_colors();
simple_bce_erases(&box2);
draw_box_caption(&box2, 1, text2);
cup(max_lines - 1, 1);
holdit();
reset_all_colors();
return MENU_NOHOLD;
}
/*
* Clear around the box for fancy_bce_test().
*/
static void
fancy_bce_erases(const BOX *box)
{
int i;
cup(box->top - 1, min_cols / 2);
ed(1); /* clear from home to cursor */
cuf(1);
el(0); /* clear from cursor to end of line */
cup(box->bottom + 1, min_cols / 2);
ed(0); /* clear from cursor to end */
cub(1);
el(1); /* clear to beginning of line */
for (i = box->top; i <= box->bottom; i++) {
int first;
int limit;
cup(i, box->left - 1);
el(1);
cup(i, box->right + 1);
limit = min_cols - box->right;
first = i + 1 - box->top;
if (first > limit)
first = limit;
dch(first);
limit -= first;
if (limit > 0)
ech(limit);
}
}
/*
* Scroll the box up/down to check if the colors are being shifted in.
*/
static void
fancy_bce_shifts(const BOX *box)
{
int i;
int limit = box->top - 1;
decsclm(TRUE); /* slow it down a little */
cup(1, 1);
for (i = 0; i < limit; ++i)
dl(1);
for (i = 0; i < limit; ++i)
ri();
decsclm(FALSE);
}
/*
* Scroll the box up/down to check if the colors are being shifted in. Erase
* the colors with ECH and DCH.
*/
static int
fancy_bce_test(MENU_ARGS)
{
BOX box1;
BOX box2;
static const char *text1[] =
{
"The screen background should be blue, with a box made of asterisks",
" and this caption, in orange (non-bold yellow). ",
" There should be no cells with the default foreground or background.",
0
};
static const char *text2[] =
{
"The screen background should be black, with a box made of asterisks",
" and this caption, in white (actually gray - it is not bold). ",
" Only the asterisk box should be in color.",
0
};
if (make_box_params(&box1, 3, 10) < 0
|| make_box_params(&box2, 7, 18) < 0)
return MENU_NOHOLD;
set_test_colors();
decaln();
draw_box_filled(&box1, 'X');
draw_box_outline(&box2, '*');
fancy_bce_erases(&box2);
draw_box_caption(&box2, 1, text1);
fancy_bce_shifts(&box2);
cup(max_lines - 1, 1);
holdit();
reset_test_colors();
fancy_bce_erases(&box2);
draw_box_caption(&box2, 1, text2);
fancy_bce_shifts(&box2);
cup(max_lines - 1, 1);
holdit();
reset_all_colors();
return MENU_NOHOLD;
}
static int
test_color_movements(MENU_ARGS)
{
set_test_colors();
tst_movements(PASS_ARGS);
reset_all_colors();
return MENU_NOHOLD;
}
static int
test_color_screen(MENU_ARGS)
{
set_test_colors();
/* The rest of the test can be done nicely with the standard vt100 test
* for insert/delete, since it doesn't modify SGR.
*/
tst_screen(PASS_ARGS);
reset_all_colors();
return MENU_NOHOLD;
}
/*
* Test the insert/delete line/character operations for color (bce) terminals
* We'll test insert/delete line operations specially, because it is very hard
* to see what is happening with the accordion test when it does not work.
*/
static int
test_color_insdel(MENU_ARGS)
{
set_test_colors();
show_line_insertions();
show_line_deletions();
/* The rest of the test can be done nicely with the standard vt100 test
* for insert/delete, since it doesn't modify SGR.
*/
tst_insdel(PASS_ARGS);
reset_all_colors();
return MENU_NOHOLD;
}
/*
* Test the other ECMA-48 features with color setup.
*/
static int
test_ecma48_misc(MENU_ARGS)
{
set_test_colors();
tst_ecma48_misc(PASS_ARGS);
reset_all_colors();
return MENU_NOHOLD;
}
static int
test_bce_color(MENU_ARGS)
{
set_test_colors();
do_scrolling();
show_graphic_rendition();
reset_all_colors();
return MENU_NOHOLD;
}
/*
* VT220 and higher implement the 22, 24, 25 and 27 codes.
* VT510 implements concealed text.
*
* ISO 6429 specifies additional SGR codes so that one needn't use SGR 0
* to reset everything before switching, e.g., set/clear pairs are
* bold 1/22
* faint 2/22
* italics 3/23
* underline 4/24
* blink 5/25
* inverse 7/27
* concealed 8/28
*/
static int
test_iso_6429_sgr(MENU_ARGS)
{
set_test_colors();
ed(2);
/* *INDENT-OFF* */
cup( 1,20); printxx("Extended/Graphic rendition test pattern:");
cup( 4, 1); c_sgr("0"); tprintf("vanilla");
cup( 4,40); c_sgr("0;1"); tprintf("bold");
cup( 6, 6); c_sgr("22;4"); tprintf("underline");
cup( 6,45); c_sgr("24;1;4"); tprintf("bold underline");
cup( 8, 1); c_sgr("22;24;5"); tprintf("blink");
cup( 8,40); c_sgr("25;5;1"); tprintf("bold blink");
cup(10, 6); c_sgr("22;4;5"); tprintf("underline blink");
cup(10,45); c_sgr("24;25;1;4;5"); tprintf("bold underline blink");
cup(12, 1); c_sgr("22;24;25;7"); tprintf("negative");
cup(12,40); c_sgr("1"); tprintf("bold negative");
cup(14, 6); c_sgr("22;4;7"); tprintf("underline negative");
cup(14,45); c_sgr("1;4;7"); tprintf("bold underline negative");
cup(16, 1); c_sgr("22;24;5;7"); tprintf("blink negative");
cup(16,40); c_sgr("1"); tprintf("bold blink negative");
cup(18, 6); c_sgr("22;4"); tprintf("underline blink negative");
cup(18,45); c_sgr("1"); tprintf("bold underline blink negative");
cup(20, 6); c_sgr(""); set_foreground(9); tprintf("original foreground");
cup(20,45); c_sgr(""); set_background(9); tprintf("original background");
cup(22, 1); c_sgr(";8"); tprintf("concealed");
cup(22,40); c_sgr("8;7"); tprintf("concealed negative");
/* *INDENT-ON* */
c_sgr(""); /* same as c_sgr("0") */
tprintf(" <- concealed text");
decscnm(FALSE); /* Inverse video off */
cup(max_lines - 1, 1);
el(0);
tprintf("Dark background. ");
holdit();
decscnm(TRUE); /* Inverse video */
cup(max_lines - 1, 1);
el(0);
tprintf("Light background. ");
holdit();
decscnm(FALSE);
cup(max_lines - 1, 1);
el(0);
tprintf("Dark background. ");
holdit();
reset_all_colors();
return MENU_NOHOLD;
}
/*
*/
static int
test_SGR_0(MENU_ARGS)
{
vt_move(1, 1);
println(the_title);
println("");
println("ECMA-48 states that SGR 0 \"cancels the effect of any preceding occurrence");
println("of SGR in the data stream regardless of the setting of the graphic rendition");
println("combination mode (GRCM)\".");
println("");
println("");
reset_all_colors();
printxx("You should see only black:");
sgr("30;40");
tprintf("SGR 30 and SGR 40 don't work");
reset_all_colors();
println(":up to here");
reset_all_colors();
printxx("You should see only white:");
sgr("37;47");
tprintf("SGR 37 and SGR 47 don't work");
reset_all_colors();
println(":up to here");
reset_all_colors();
printxx("You should see text here: ");
sgr("30;40");
sgr("0");
tprintf("SGR 0 reset works (explicit 0)");
println("");
reset_all_colors();
printxx("................and here: ");
sgr("37;47");
sgr("");
tprintf("SGR 0 reset works (default param)");
println("");
reset_all_colors();
holdit();
return MENU_NOHOLD;
}
/*
* Allow user to test the same screens w/o colors.
*/
int
toggle_color_mode(MENU_ARGS)
{
do_colors = !do_colors;
return MENU_NOHOLD;
}
/*
* VT100s of course never did colors, ANSI or otherwise. This test is for
* xterm.
*/
static int
test_vt100_colors(MENU_ARGS)
{
/* *INDENT-OFF* */
static MENU colormenu[] = {
{ "Exit", 0 },
{ "Test of cursor movements", test_color_movements },
{ "Test of screen features", test_color_screen },
{ "Test Insert/Delete Char/Line", test_color_insdel, },
{ "", 0 }
};
/* *INDENT-ON* */
int save_colors = do_colors;
do_colors = TRUE;
do {
vt_clear(2);
__(title(0), println("Test VT102-style features with BCE"));
__(title(2), println("Choose test type:"));
} while (menu(colormenu));
do_colors = save_colors;
return MENU_NOHOLD;
}
/*
* For terminals that support ANSI/ISO colors, work through a graduated
* set of tests that first display colors (if the terminal does indeed
* support them), then exercise the associated reset, clear operations.
*/
int
tst_colors(MENU_ARGS)
{
/* *INDENT-OFF* */
static MENU colormenu[] = {
{ "Exit", 0 },
{ txt_override_color, toggle_color_mode, },
{ "Display color test-pattern", show_test_pattern, },
{ "Test SGR-0 color reset", test_SGR_0, },
{ "Test BCE-style clear line/display (ED, EL)", simple_bce_test, },
{ "Test BCE-style clear line/display (ECH, Indexing)", fancy_bce_test, },
{ "Test of VT102-style features with BCE", test_vt100_colors, },
{ "Test other ISO-6429 features with BCE", test_ecma48_misc },
{ "Test screen features with BCE", test_bce_color, },
{ "Test screen features with ISO 6429 SGR 22-27 codes", test_iso_6429_sgr, },
{ "", 0 }
};
/* *INDENT-ON* */
int save_colors = do_colors;
do_colors = TRUE;
do {
vt_clear(2);
sprintf(txt_override_color, "%s color-switching", STR_ENABLE(do_colors));
__(title(0), println("ISO 6429 colors"));
__(title(2), println("Choose test type:"));
} while (menu(colormenu));
do_colors = save_colors;
return MENU_NOHOLD;
}