2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 #include <ipxe/ansiesc.h>
30 #include <ipxe/ansicol.h>
31 #include <config/colour.h>
39 /** ANSI colour pair definitions */
40 static struct ansicol_pair ansicol_pairs[] = {
41 [CPAIR_DEFAULT] = { COLOR_DEFAULT, COLOR_DEFAULT },
42 [CPAIR_NORMAL] = { COLOR_NORMAL_FG, COLOR_NORMAL_BG },
43 [CPAIR_SELECT] = { COLOR_SELECT_FG, COLOR_SELECT_BG },
44 [CPAIR_SEPARATOR] = { COLOR_SEPARATOR_FG, COLOR_SEPARATOR_BG },
45 [CPAIR_EDIT] = { COLOR_EDIT_FG, COLOR_EDIT_BG },
46 [CPAIR_ALERT] = { COLOR_ALERT_FG, COLOR_ALERT_BG },
47 [CPAIR_URL] = { COLOR_URL_FG, COLOR_URL_BG },
48 [CPAIR_PXE] = { COLOR_PXE_FG, COLOR_PXE_BG },
52 * Set ANSI colour (when no colour definition support is present)
54 * @v colour Colour index
55 * @v which Foreground/background selector
57 __weak void ansicol_set ( unsigned int colour, unsigned int which ) {
59 /* Colour indices are hardcoded and should never be out of range */
60 assert ( colour < 10 );
62 /* Set basic colour */
63 printf ( CSI "%c%dm", which, colour );
67 * Set ANSI foreground colour
69 * @v colour Colour index
71 static void ansicol_foreground ( unsigned int colour ) {
72 ansicol_set ( colour, '3' );
76 * Set ANSI background colour
78 * @v colour Colour index
80 static void ansicol_background ( unsigned int colour ) {
81 ansicol_set ( colour, '4' );
85 * Set ANSI foreground and background colour
87 * @v cpair Colour pair index
89 void ansicol_set_pair ( unsigned int cpair ) {
90 struct ansicol_pair *pair;
92 /* Colour pair indices are hardcoded and should never be out of range */
93 assert ( cpair < ( sizeof ( ansicol_pairs ) /
94 sizeof ( ansicol_pairs[0] ) ) );
96 /* Set both foreground and background colours */
97 pair = &ansicol_pairs[cpair];
98 ansicol_foreground ( pair->foreground );
99 ansicol_background ( pair->background );
103 * Define ANSI colour pair
105 * @v cpair Colour pair index
106 * @v foreground Foreground colour index
107 * @v background Background colour index
108 * @ret rc Return status code
110 int ansicol_define_pair ( unsigned int cpair, unsigned int foreground,
111 unsigned int background ) {
112 struct ansicol_pair *pair;
114 /* Fail if colour index is out of range */
115 if ( cpair >= ( sizeof ( ansicol_pairs ) / sizeof ( ansicol_pairs[0] )))
118 /* Update colour pair definition */
119 pair = &ansicol_pairs[cpair];
120 pair->foreground = foreground;
121 pair->background = background;
122 DBGC ( &ansicol_pairs[0], "ANSICOL redefined colour pair %d as "
123 "foreground %d background %d\n", cpair, foreground, background );