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