xitiomet is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againnglog / vt100.h
- commit
- a624f6dc89d9
- parent
- 835500c42a0e
- branch
- default
Fixed warnings with unsigned ints
1 |
ee2b834af766
|
// vt100.h |
2 |
ee2b834af766
|
/* |
3 |
ee2b834af766
|
---------------------------------------------- |
4 |
ee2b834af766
|
Terminal macros to use in C++ console programs |
5 |
ee2b834af766
|
---------------------------------------------- |
6 |
ee2b834af766
|
|
7 |
ee2b834af766
|
Use the following macros in your C++ cout statements to manipulate the |
8 |
ee2b834af766
|
video display. Most systems emulate either VT100 terminals or the ANSI |
9 |
ee2b834af766
|
terminal protocol. If yours does not, these won't work until you find |
10 |
ee2b834af766
|
an ANSI terminal driver. |
11 |
ee2b834af766
|
|
12 |
ee2b834af766
|
The following macros are practically guaranteed to work: |
13 |
ee2b834af766
|
clear_screen |
14 |
ee2b834af766
|
audible_bell |
15 |
ee2b834af766
|
goto_xy() |
16 |
ee2b834af766
|
move_to() |
17 |
ee2b834af766
|
default_attributes |
18 |
ee2b834af766
|
set_reverse() |
19 |
ee2b834af766
|
set_colors() |
20 |
ee2b834af766
|
finalize |
21 |
ee2b834af766
|
|
22 |
ee2b834af766
|
The least likely to work are |
23 |
ee2b834af766
|
show_cursor() |
24 |
ee2b834af766
|
set_italic() |
25 |
ee2b834af766
|
|
26 |
ee2b834af766
|
............................................................................. |
27 |
ee2b834af766
|
Example usage: |
28 |
ee2b834af766
|
|
29 |
ee2b834af766
|
cout << clear_screen |
30 |
ee2b834af766
|
<< goto_xy( 10, 2 ) << "Hello"; |
31 |
ee2b834af766
|
|
32 |
ee2b834af766
|
To terminate your program, your code should look like this: |
33 |
ee2b834af766
|
|
34 |
ee2b834af766
|
cout << finalize; |
35 |
ee2b834af766
|
return EXIT_SUCCESS; |
36 |
ee2b834af766
|
|
37 |
ee2b834af766
|
............................................................................. |
38 |
ee2b834af766
|
T H E M A C R O S |
39 |
ee2b834af766
|
|
40 |
ee2b834af766
|
cout << reset Restores the terminal to it's original |
41 |
ee2b834af766
|
state. |
42 |
ee2b834af766
|
You probably don't want to use this. |
43 |
ee2b834af766
|
|
44 |
ee2b834af766
|
cout << clear_screen Clear the entire screen. |
45 |
ee2b834af766
|
<< clear_to_bos Clear from the top of the screen. |
46 |
ee2b834af766
|
<< clear_to_eos Clear to the bottom of the screen. |
47 |
ee2b834af766
|
|
48 |
ee2b834af766
|
cout << clear_to_eol Clear to the end of the line. |
49 |
ee2b834af766
|
<< clear_to_bol Clear to the beginning of the line. |
50 |
ee2b834af766
|
<< clear_line Clear the entire line. |
51 |
ee2b834af766
|
|
52 |
ee2b834af766
|
cout << visual_bell Blinks the screen (unreliable). |
53 |
ee2b834af766
|
<< audible_bell Sounds the speaker beep. |
54 |
ee2b834af766
|
|
55 |
ee2b834af766
|
cout << show_cursor( TRUE ) Make the cursor visible (default) |
56 |
ee2b834af766
|
<< show_cursor( FALSE ) Make the cursor invisible |
57 |
ee2b834af766
|
|
58 |
ee2b834af766
|
cout << goto_xy( x, y ) Move the cursor to coordinate (x, y), |
59 |
ee2b834af766
|
or (column x, line y), where (1, 1) |
60 |
ee2b834af766
|
is the upper-left corner of the screen. |
61 |
ee2b834af766
|
<< move_to( line, column ) Same. |
62 |
ee2b834af766
|
|
63 |
ee2b834af766
|
cout << cursor_up( count ) Move the cursor "count" characters |
64 |
ee2b834af766
|
<< cursor_down( count ) (or lines) from its current position. |
65 |
ee2b834af766
|
<< cursor_left( count ) |
66 |
ee2b834af766
|
<< cursor_right( count ) |
67 |
ee2b834af766
|
|
68 |
ee2b834af766
|
cout << set_scrolling_region( top, bottom ) |
69 |
ee2b834af766
|
Set the lines in the range [top,bottom] |
70 |
ee2b834af766
|
to be those that scroll. |
71 |
ee2b834af766
|
<< scroll_up( count ) Scroll the region up or down "count" |
72 |
ee2b834af766
|
<< scroll_down( count ) lines. |
73 |
ee2b834af766
|
|
74 |
ee2b834af766
|
cout << insert_line( count ) Insert or delete "count" items. |
75 |
ee2b834af766
|
<< delete_line( count ) Always inserts blanks. |
76 |
ee2b834af766
|
<< insert_char( count ) The result is affected if you change |
77 |
ee2b834af766
|
<< delete_char( count ) the scroll region. |
78 |
ee2b834af766
|
|
79 |
ee2b834af766
|
cout << default_attributes Reset so that the cursor attributes are |
80 |
ee2b834af766
|
the default (no bold, underline, etc. |
81 |
ee2b834af766
|
and colors are VT_DEFAULT). |
82 |
ee2b834af766
|
<< set_bold( bool ) Bold text. |
83 |
ee2b834af766
|
<< set_italic( bool ) Italic text. |
84 |
ee2b834af766
|
<< set_underline( bool ) Underline text. |
85 |
ee2b834af766
|
<< set_reverse( bool ) Reverse or invert the text colors. |
86 |
ee2b834af766
|
<< set_attributes( bold, italic, underline, reverse ) |
87 |
ee2b834af766
|
Set all four text attributes at once |
88 |
ee2b834af766
|
without affecting the color attributes. |
89 |
ee2b834af766
|
<< set_colors( fg, bg ) The colors are |
90 |
ee2b834af766
|
VT_BLACK VT_YELLOW VT_CYAN |
91 |
ee2b834af766
|
VT_RED VT_BLUE VT_WHITE |
92 |
ee2b834af766
|
VT_GREEN VT_MAGENTA VT_DEFAULT |
93 |
ee2b834af766
|
|
94 |
ee2b834af766
|
cout << finalize Do this before your program ends. |
95 |
ee2b834af766
|
(Alias for default_attributes.) |
96 |
ee2b834af766
|
|
97 |
ee2b834af766
|
............................................................................. |
98 |
ee2b834af766
|
Lastly |
99 |
ee2b834af766
|
|
100 |
ee2b834af766
|
All this does is drive some basic functionality of a VT100/ANSI terminal. |
101 |
ee2b834af766
|
Notibly, it lacks: |
102 |
ee2b834af766
|
* The ability to tell you where the cursor is (so you must remember!) |
103 |
ee2b834af766
|
* The ability to tell you the size of the terminal screen. |
104 |
ee2b834af766
|
(You can use "stty size" at the command prompt to find out.) |
105 |
ee2b834af766
|
* The ability to get just one character from the keyboard. |
106 |
ee2b834af766
|
This takes some jumping through a few hoops. If you want to do some more |
107 |
ee2b834af766
|
advanced stuff than what is presented here, consider using the ncurses |
108 |
ee2b834af766
|
library (type "man ncurses" at the prompt). |
109 |
ee2b834af766
|
(Here at RU, you'll have to include </usr/local/include/ncurses.h> |
110 |
ee2b834af766
|
instead of just plain old <ncurses.h> to make curses work. Don't forget |
111 |
ee2b834af766
|
to compile with "g++ ... -lncurses"!) |
112 |
ee2b834af766
|
If you're interested in this kind of junk, type "man screen" at the prompt. |
113 |
ee2b834af766
|
|
114 |
ee2b834af766
|
Enjoy! |
115 |
ee2b834af766
|
|
116 |
ee2b834af766
|
--Michael Thomas Greer |
117 |
ee2b834af766
|
|
118 |
ee2b834af766
|
*/ |
119 |
ee2b834af766
|
|
120 |
ee2b834af766
|
#ifndef VT100_MACRO_H |
121 |
ee2b834af766
|
#define VT100_MACRO_H |
122 |
ee2b834af766
|
|
123 |
ee2b834af766
|
#ifndef TRUE |
124 |
ee2b834af766
|
#define TRUE (1) |
125 |
ee2b834af766
|
#endif |
126 |
ee2b834af766
|
#ifndef FALSE |
127 |
ee2b834af766
|
#define FALSE (0) |
128 |
ee2b834af766
|
#endif |
129 |
ee2b834af766
|
|
130 |
ee2b834af766
|
#define VT_BLACK 0 |
131 |
ee2b834af766
|
#define VT_RED 1 |
132 |
ee2b834af766
|
#define VT_GREEN 2 |
133 |
ee2b834af766
|
#define VT_YELLOW 3 |
134 |
ee2b834af766
|
#define VT_BLUE 4 |
135 |
ee2b834af766
|
#define VT_MAGENTA 5 |
136 |
ee2b834af766
|
#define VT_CYAN 6 |
137 |
ee2b834af766
|
#define VT_WHITE 7 |
138 |
ee2b834af766
|
#define VT_DEFAULT 9 |
139 |
ee2b834af766
|
|
140 |
ee2b834af766
|
#define reset "\33c" |
141 |
ee2b834af766
|
|
142 |
ee2b834af766
|
#define clear_screen "\33[2J" |
143 |
ee2b834af766
|
#define clear_to_bos "\33[1J" |
144 |
ee2b834af766
|
#define clear_to_eos "\33[J" |
145 |
ee2b834af766
|
|
146 |
ee2b834af766
|
#define clear_line "\33[2K" |
147 |
ee2b834af766
|
#define clear_to_bol "\33[1K" |
148 |
ee2b834af766
|
#define clear_to_eol "\33[K" |
149 |
ee2b834af766
|
|
150 |
ee2b834af766
|
#define visual_bell "\33g" |
151 |
ee2b834af766
|
#define audible_bell "\a" |
152 |
ee2b834af766
|
|
153 |
ee2b834af766
|
#define show_cursor( v ) ((v) ? "\33\67p" : "\33\66p") |
154 |
ee2b834af766
|
|
155 |
ee2b834af766
|
#define goto_xy( x, y ) "\33[" << y << ";" << x << "H" |
156 |
ee2b834af766
|
#define move_to( y, x ) "\33[" << y << ";" << x << "f" |
157 |
ee2b834af766
|
|
158 |
ee2b834af766
|
#define cursor_up( count ) "\33[" << count << "A" |
159 |
ee2b834af766
|
#define cursor_down( count ) "\33[" << count << "B" |
160 |
ee2b834af766
|
#define cursor_right( count ) "\33[" << count << "C" |
161 |
ee2b834af766
|
#define cursor_left( count ) "\33[" << count << "D" |
162 |
ee2b834af766
|
|
163 |
ee2b834af766
|
#define set_scrolling_region( top, bottom ) "\33[" << top << ";" << bottom << "r" |
164 |
ee2b834af766
|
|
165 |
ee2b834af766
|
#define scroll_up( count ) "\33[" << count << "S" |
166 |
ee2b834af766
|
#define scroll_down( count ) "\33[" << count << "T" |
167 |
ee2b834af766
|
|
168 |
ee2b834af766
|
#define insert_line( count ) "\33[" << count << "L" |
169 |
ee2b834af766
|
#define delete_line( count ) "\33[" << count << "M" |
170 |
ee2b834af766
|
|
171 |
ee2b834af766
|
#define insert_char( count ) "\33[" << count << "@" |
172 |
ee2b834af766
|
#define delete_char( count ) "\33[" << count << "P" |
173 |
ee2b834af766
|
|
174 |
ee2b834af766
|
static bool is_b = FALSE; |
175 |
ee2b834af766
|
static bool is_i = FALSE; |
176 |
ee2b834af766
|
static bool is_u = FALSE; |
177 |
ee2b834af766
|
static bool is_r = FALSE; |
178 |
ee2b834af766
|
static int fg_c = VT_DEFAULT; |
179 |
ee2b834af766
|
static int bg_c = VT_DEFAULT; |
180 |
ee2b834af766
|
|
181 |
ee2b834af766
|
#define default_attributes "\33[0m" |
182 |
ee2b834af766
|
|
183 |
ee2b834af766
|
// these four following are not meant to be used externally |
184 |
ee2b834af766
|
#define set_b( b ) ((is_b = b) ? "\33[1m" : "") |
185 |
ee2b834af766
|
#define set_i( i ) ((is_i = i) ? "\33[3m" : "") |
186 |
ee2b834af766
|
#define set_u( u ) ((is_u = u) ? "\33[4m" : "") |
187 |
ee2b834af766
|
#define set_r( r ) ((is_r = r) ? "\33[7m" : "") |
188 |
ee2b834af766
|
|
189 |
ee2b834af766
|
#define set_colors( fg, bg ) "\33[3" << (fg_c = fg) << ";4" << (bg_c = bg) << "m" |
190 |
ee2b834af766
|
|
191 |
ee2b834af766
|
#define set_attributes( b, i, u, r ) set_b( b ) << set_i( i ) << set_u( u ) << set_r( r ) << set_colors( fg_c, bg_c ) |
192 |
ee2b834af766
|
|
193 |
ee2b834af766
|
#define set_bold( b ) default_attributes << set_attributes( b, is_i, is_u, is_r ) |
194 |
ee2b834af766
|
#define set_italic( i ) default_attributes << set_attributes( is_b, i, is_u, is_r ) |
195 |
ee2b834af766
|
#define set_underline( u ) default_attributes << set_attributes( is_b, is_i, u, is_r ) |
196 |
ee2b834af766
|
#define set_reverse( r ) default_attributes << set_attributes( is_b, is_i, is_u, r ) |
197 |
ee2b834af766
|
|
198 |
ee2b834af766
|
#define finalize default_attributes |
199 |
ee2b834af766
|
|
200 |
ee2b834af766
|
#endif |
201 |
ee2b834af766
|
|
202 |
ee2b834af766
|
// end vt100.h |