Browse Source

[mucurses] Use "<ESC>[2J" ANSI escape sequence to clear screen

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 10 years ago
parent
commit
7271b50890
3 changed files with 40 additions and 16 deletions
  1. 22
    11
      src/hci/mucurses/ansi_screen.c
  2. 10
    0
      src/hci/mucurses/clear.c
  3. 8
    5
      src/include/curses.h

+ 22
- 11
src/hci/mucurses/ansi_screen.c View File

14
 
14
 
15
 static unsigned int saved_usage;
15
 static unsigned int saved_usage;
16
 
16
 
17
+static void ansiscr_attrs ( struct _curses_screen *scr, attr_t attrs ) {
18
+	int bold = ( attrs & A_BOLD );
19
+	attr_t cpair = PAIR_NUMBER ( attrs );
20
+	short fcol;
21
+	short bcol;
22
+
23
+	if ( scr->attrs != attrs ) {
24
+		scr->attrs = attrs;
25
+		pair_content ( cpair, &fcol, &bcol );
26
+		/* ANSI escape sequence to update character attributes */
27
+		printf ( "\033[0;%d;3%d;4%dm", ( bold ? 1 : 22 ), fcol, bcol );
28
+	}
29
+}
30
+
17
 static void ansiscr_reset ( struct _curses_screen *scr ) {
31
 static void ansiscr_reset ( struct _curses_screen *scr ) {
18
 	/* Reset terminal attributes and clear screen */
32
 	/* Reset terminal attributes and clear screen */
19
 	scr->attrs = 0;
33
 	scr->attrs = 0;
20
 	scr->curs_x = 0;
34
 	scr->curs_x = 0;
21
 	scr->curs_y = 0;
35
 	scr->curs_y = 0;
22
-	printf ( "\033[0m" );
36
+	printf ( "\033[0m\033[2J" );
23
 }
37
 }
24
 
38
 
25
 static void ansiscr_init ( struct _curses_screen *scr ) {
39
 static void ansiscr_init ( struct _curses_screen *scr ) {
32
 	console_set_usage ( saved_usage );
46
 	console_set_usage ( saved_usage );
33
 }
47
 }
34
 
48
 
49
+static void ansiscr_erase ( struct _curses_screen *scr, attr_t attrs ) {
50
+	ansiscr_attrs ( scr, attrs );
51
+	printf ( "\033[2J" );
52
+}
53
+
35
 static void ansiscr_movetoyx ( struct _curses_screen *scr,
54
 static void ansiscr_movetoyx ( struct _curses_screen *scr,
36
 			       unsigned int y, unsigned int x ) {
55
 			       unsigned int y, unsigned int x ) {
37
 	if ( ( x != scr->curs_x ) || ( y != scr->curs_y ) ) {
56
 	if ( ( x != scr->curs_x ) || ( y != scr->curs_y ) ) {
45
 static void ansiscr_putc ( struct _curses_screen *scr, chtype c ) {
64
 static void ansiscr_putc ( struct _curses_screen *scr, chtype c ) {
46
 	unsigned int character = ( c & A_CHARTEXT );
65
 	unsigned int character = ( c & A_CHARTEXT );
47
 	attr_t attrs = ( c & ( A_ATTRIBUTES | A_COLOR ) );
66
 	attr_t attrs = ( c & ( A_ATTRIBUTES | A_COLOR ) );
48
-	int bold = ( attrs & A_BOLD );
49
-	attr_t cpair = PAIR_NUMBER ( attrs );
50
-	short fcol;
51
-	short bcol;
52
 
67
 
53
 	/* Update attributes if changed */
68
 	/* Update attributes if changed */
54
-	if ( attrs != scr->attrs ) {
55
-		scr->attrs = attrs;
56
-		pair_content ( cpair, &fcol, &bcol );
57
-		/* ANSI escape sequence to update character attributes */
58
-		printf ( "\033[0;%d;3%d;4%dm", ( bold ? 1 : 22 ), fcol, bcol );
59
-	}
69
+	ansiscr_attrs ( scr, attrs );
60
 
70
 
61
 	/* Print the actual character */
71
 	/* Print the actual character */
62
 	putchar ( character );
72
 	putchar ( character );
79
 SCREEN _ansi_screen = {
89
 SCREEN _ansi_screen = {
80
 	.init		= ansiscr_init,
90
 	.init		= ansiscr_init,
81
 	.exit		= ansiscr_exit,
91
 	.exit		= ansiscr_exit,
92
+	.erase		= ansiscr_erase,
82
 	.movetoyx	= ansiscr_movetoyx,
93
 	.movetoyx	= ansiscr_movetoyx,
83
 	.putc		= ansiscr_putc,
94
 	.putc		= ansiscr_putc,
84
 	.getc		= ansiscr_getc,
95
 	.getc		= ansiscr_getc,

+ 10
- 0
src/hci/mucurses/clear.c View File

88
 	wclrtobot( win );
88
 	wclrtobot( win );
89
 	return OK;
89
 	return OK;
90
 }
90
 }
91
+
92
+/**
93
+ * Completely clear the screen
94
+ *
95
+ * @ret rc	return status code
96
+ */
97
+int erase ( void ) {
98
+	stdscr->scr->erase( stdscr->scr, stdscr->attrs );
99
+	return OK;
100
+}

+ 8
- 5
src/include/curses.h View File

37
 
37
 
38
 	void ( *init ) ( struct _curses_screen *scr );
38
 	void ( *init ) ( struct _curses_screen *scr );
39
 	void ( *exit ) ( struct _curses_screen *scr );
39
 	void ( *exit ) ( struct _curses_screen *scr );
40
+	/**
41
+	 * Erase screen
42
+	 *
43
+	 * @v scr	screen on which to operate
44
+	 * @v attrs	attributes
45
+	 */
46
+	void ( * erase ) ( struct _curses_screen *scr, attr_t attrs );
40
 	/**
47
 	/**
41
 	 * Move cursor to position specified by x,y coords
48
 	 * Move cursor to position specified by x,y coords
42
 	 *
49
 	 *
242
 extern int echochar ( const chtype );
249
 extern int echochar ( const chtype );
243
 extern int endwin ( void );
250
 extern int endwin ( void );
244
 extern char erasechar ( void );
251
 extern char erasechar ( void );
245
-//extern int erase ( void );
252
+extern int erase ( void );
246
 extern void filter ( void );
253
 extern void filter ( void );
247
 extern int flash ( void );
254
 extern int flash ( void );
248
 extern int flushinp ( void );
255
 extern int flushinp ( void );
552
 	return wdeleteln( stdscr );
559
 	return wdeleteln( stdscr );
553
 }
560
 }
554
 
561
 
555
-static inline int erase ( void ) {
556
-	return werase ( stdscr );
557
-}
558
-
559
 static inline int getch ( void ) {
562
 static inline int getch ( void ) {
560
 	return wgetch ( stdscr );
563
 	return wgetch ( stdscr );
561
 }
564
 }

Loading…
Cancel
Save