games: Import colorbars and hals_end from NetBSD.
[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 int
35 main(void)
36 {
37         static struct colorInfo {
38                 const char *name;
39                 int color;
40         } colorInfo[] = {
41                 { "Black", COLOR_BLACK },
42                 { "Red", COLOR_RED },
43                 { "Green", COLOR_GREEN },
44                 { "Yellow", COLOR_YELLOW },
45                 { "Blue", COLOR_BLUE },
46                 { "Magenta", COLOR_MAGENTA },
47                 { "Cyan", COLOR_CYAN },
48                 { "White", COLOR_WHITE },
49         };
50         size_t lengths[__arysize(colorInfo)];
51
52         static const size_t numcolors = __arysize(colorInfo);
53         size_t labelwidth;
54
55         int colorOK;
56         int spacing, offsetx, labeloffsety, labeloffsetx;
57
58         if (!initscr())
59                 errx(EXIT_FAILURE, "Cannot initialize curses");
60
61         colorOK = has_colors();
62         if (!colorOK) {
63                 endwin();
64                 errx(EXIT_FAILURE, "Terminal cannot display color");
65         }
66
67         if (COLS < 45 || LINES < 10) {
68                 endwin();
69                 errx(EXIT_FAILURE, "Terminal size must be at least 45x10.");
70         }
71
72         spacing = COLS / numcolors;
73         offsetx = (COLS - (spacing * numcolors)) / 2;
74
75
76         start_color();
77
78         labelwidth = 0;
79         for (size_t i = 0; i < numcolors; i++) {
80                 lengths[i] = strlen(colorInfo[i].name);
81                 if (lengths[i] > labelwidth)
82                         labelwidth = lengths[i];
83                 init_pair(i, COLOR_WHITE, colorInfo[i].color);
84         }
85
86         labeloffsetx = spacing / 2;
87         labeloffsety = (LINES - 1 - labelwidth) / 2;
88         clear();
89
90         move(0, 0);
91
92         for (size_t i = 0; i < numcolors; i++) {
93                 int xoffs = offsetx + spacing * i;
94
95                 attrset(COLOR_PAIR(i));
96
97                 for (int line = 0; line < LINES - 1; line++)
98                         for (int xpos = 0; xpos < spacing; xpos++)
99                                mvprintw(line, xoffs + xpos, " ");
100
101                 attrset(COLOR_PAIR(0));
102
103                 xoffs += labeloffsetx;
104                 for (size_t line = 0; line < lengths[i]; line++)
105                         mvprintw(line + labeloffsety, xoffs, "%c",
106                             colorInfo[i].name[line]);
107         }
108
109         attrset(COLOR_PAIR(0));
110
111         mvprintw(LINES - 1, 0, "ANSI Color chart - Press any key to exit: ");
112
113         refresh();
114
115         getch();
116
117         endwin();
118
119         return EXIT_SUCCESS;
120 }