Upgrade libressl. 1/2
[dragonfly.git] / games / colorbars / colorbars.c
1 /*      $NetBSD: colorbars.c,v 1.1 2012/06/06 00:13:36 christos Exp $   */
2
3 /*-
4  * Copyright (c) 2012 Nathanial Sloss <nathanialsloss@yahoo.com.au>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 #include <curses.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <err.h>
32 #include <stdlib.h>
33
34 #include <sys/param.h>
35
36 int
37 main(void)
38 {
39         static struct colorInfo {
40                 const char *name;
41                 int color;
42         } colorInfo[] = {
43                 { "Black", COLOR_BLACK },
44                 { "Red", COLOR_RED },
45                 { "Green", COLOR_GREEN },
46                 { "Yellow", COLOR_YELLOW },
47                 { "Blue", COLOR_BLUE },
48                 { "Magenta", COLOR_MAGENTA },
49                 { "Cyan", COLOR_CYAN },
50                 { "White", COLOR_WHITE },
51         };
52         size_t lengths[NELEM(colorInfo)];
53
54         static const size_t numcolors = NELEM(colorInfo);
55         size_t labelwidth;
56
57         int colorOK;
58         int spacing, offsetx, labeloffsety, labeloffsetx;
59
60         if (!initscr())
61                 errx(EXIT_FAILURE, "Cannot initialize curses");
62
63         colorOK = has_colors();
64         if (!colorOK) {
65                 endwin();
66                 errx(EXIT_FAILURE, "Terminal cannot display color");
67         }
68
69         if (COLS < 45 || LINES < 10) {
70                 endwin();
71                 errx(EXIT_FAILURE, "Terminal size must be at least 45x10.");
72         }
73
74         spacing = COLS / numcolors;
75         offsetx = (COLS - (spacing * numcolors)) / 2;
76
77
78         start_color();
79
80         labelwidth = 0;
81         for (size_t i = 0; i < numcolors; i++) {
82                 lengths[i] = strlen(colorInfo[i].name);
83                 if (lengths[i] > labelwidth)
84                         labelwidth = lengths[i];
85                 init_pair(i, COLOR_WHITE, colorInfo[i].color);
86         }
87
88         labeloffsetx = spacing / 2;
89         labeloffsety = (LINES - 1 - labelwidth) / 2;
90         clear();
91
92         move(0, 0);
93
94         for (size_t i = 0; i < numcolors; i++) {
95                 int xoffs = offsetx + spacing * i;
96
97                 attrset(COLOR_PAIR(i));
98
99                 for (int line = 0; line < LINES - 1; line++)
100                         for (int xpos = 0; xpos < spacing; xpos++)
101                                mvprintw(line, xoffs + xpos, " ");
102
103                 attrset(COLOR_PAIR(0));
104
105                 xoffs += labeloffsetx;
106                 for (size_t line = 0; line < lengths[i]; line++)
107                         mvprintw(line + labeloffsety, xoffs, "%c",
108                             colorInfo[i].name[line]);
109         }
110
111         attrset(COLOR_PAIR(0));
112
113         mvprintw(LINES - 1, 0, "ANSI Color chart - Press any key to exit: ");
114
115         refresh();
116
117         getch();
118
119         endwin();
120
121         return EXIT_SUCCESS;
122 }