3 #include <ipxe/ansicol.h>
4 #include <ipxe/console.h>
6 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
8 static void ansiscr_reset(struct _curses_screen *scr) __nonnull;
9 static void ansiscr_movetoyx(struct _curses_screen *scr,
10 unsigned int y, unsigned int x) __nonnull;
11 static void ansiscr_putc(struct _curses_screen *scr, chtype c) __nonnull;
13 static unsigned int saved_usage;
15 static void ansiscr_attrs ( struct _curses_screen *scr, attr_t attrs ) {
16 int bold = ( attrs & A_BOLD );
17 attr_t cpair = PAIR_NUMBER ( attrs );
19 if ( scr->attrs != attrs ) {
21 /* Reset attributes and set/clear bold as appropriate */
22 printf ( "\033[0;%dm", ( bold ? 1 : 22 ) );
23 /* Set foreground and background colours */
24 ansicol_set_pair ( cpair );
28 static void ansiscr_reset ( struct _curses_screen *scr ) {
29 /* Reset terminal attributes and clear screen */
34 ansicol_set_pair ( CPAIR_DEFAULT );
38 static void ansiscr_init ( struct _curses_screen *scr ) {
39 saved_usage = console_set_usage ( CONSOLE_USAGE_TUI );
40 ansiscr_reset ( scr );
43 static void ansiscr_exit ( struct _curses_screen *scr ) {
44 ansiscr_reset ( scr );
45 console_set_usage ( saved_usage );
48 static void ansiscr_erase ( struct _curses_screen *scr, attr_t attrs ) {
49 ansiscr_attrs ( scr, attrs );
53 static void ansiscr_movetoyx ( struct _curses_screen *scr,
54 unsigned int y, unsigned int x ) {
55 if ( ( x != scr->curs_x ) || ( y != scr->curs_y ) ) {
56 /* ANSI escape sequence to update cursor position */
57 printf ( "\033[%d;%dH", ( y + 1 ), ( x + 1 ) );
63 static void ansiscr_putc ( struct _curses_screen *scr, chtype c ) {
64 unsigned int character = ( c & A_CHARTEXT );
65 attr_t attrs = ( c & ( A_ATTRIBUTES | A_COLOR ) );
67 /* Update attributes if changed */
68 ansiscr_attrs ( scr, attrs );
70 /* Print the actual character */
71 putchar ( character );
73 /* Update expected cursor position */
74 if ( ++(scr->curs_x) == COLS ) {
80 static int ansiscr_getc ( struct _curses_screen *scr __unused ) {
84 static bool ansiscr_peek ( struct _curses_screen *scr __unused ) {
88 static void ansiscr_cursor ( struct _curses_screen *scr __unused,
90 printf ( "\033[?25%c", ( visibility ? 'h' : 'l' ) );
93 SCREEN _ansi_screen = {
96 .erase = ansiscr_erase,
97 .movetoyx = ansiscr_movetoyx,
100 .peek = ansiscr_peek,
101 .cursor = ansiscr_cursor,