-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr2_termui.h
221 lines (177 loc) · 7.72 KB
/
r2_termui.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* r2_termui - v0.0 - public domain very basic, terminal control sequences for
cursor control
no warranty implied; use at your own risk
Built in the style of: https://github.com/nothings/stb
This is written with, mostly, game development in mind.
Usage:
#include "r2_termui.h"
void main(void)
{
printf(ESC_ERASE_SCREEN);
printf(ESC_CURSOR_POS, 10, 10);
unsigned int pixel_color = 34; // blue
printf(ESC_SET_ATTRIBUTE_MODE_1, pixel_color);
printf("%s", "Hello World\n");
printf(ESC_SET_ATTRIBUTE_MODE_1, 0);
printf("\n\n\n");
printf(ESC_SHOW_CURSOR);
}
LICENSE
See end of file for license information.
*/
#ifndef R2_TERMCUI_H
#define R2_TERMCUI_H
#ifdef __cplusplus
extern "C"
{
#endif
// #define NB_COLORS 8
#define ESC_START '\033'
#define ESC_IDENTIFY "\033[c"
#define ESC_REPORT_DEVICE "\033[%d0c"
// https://www2.ccs.neu.edu/research/gpc/VonaUtils/vona/terminal/vtansi.htm
///////////////////////////////
// Device Status
///////////////////////////////
/* The following codes are used for reporting terminal/display settings, and vary depending on the implementation: */
/* Requests a Report Device Code response from the device. */
#define ESC_QUERY_DEVICE_CODE "\033[c"
/* Generated by the device in response to Query Device Code request. */
#define ESC_REPORT_DEVICE_CODE "\033[%d0c"
/* Requests a Report Device Status response from the device. */
#define ESC_QUERY_DEVICE_STATUS "\033[5n"
/* Generated by the device in response to a Query Device Status request; indicates that device is functioning correctly. */
#define ESC_REPORT_DEVICE_OK "\033[0n"
/* Generated by the device in response to a Query Device Status request; indicates that device is functioning improperly. */
#define ESC_REPORT_DEVICE_FAILURE "\033[3n"
/* Requests a Report Cursor Position response from the device. */
#define ESC_QUERY_CURSOR_POSITION "\033[6n"
/* Generated by the device in response to a Query Cursor Position request; reports current cursor position. */
#define ESC_REPORT_CURSOR_POSITION "\033[%d;%dR"
///////////////////////////////
// Cursor Control
///////////////////////////////
/* Sets the cursor position where subsequent
text will begin. If no row/column parameters are provided (ie. \033[H),
the cursor will move to the home position, at the upper left of the screen. */
#define ESC_CURSOR_POS "\033[%d;%dH"
#define ESC_CURSOR_HOME "\033[H"
/* Moves the cursor up by COUNT rows; the default count is 1. */
#define ESC_CURSOR_UP "\033[%dA"
/* Moves the cursor down by COUNT rows; the default count is 1. */
#define ESC_CURSOR_DOWN "\033[%dB"
/* Moves the cursor forward by COUNT columns; the default count is 1. */
#define ESC_CURSOR_FORWARD "\033[%dC"
/* Moves the cursor backward by COUNT columns; the default count is 1. */
#define ESC_CURSOR_BACKWARD "\033[%dD"
/* Identical to Cursor Home. */
#define ESC_FORCE_CURSOR_POS "\033[%d;%df"
/* Save current cursor position. */
#define ESC_SAVE_CURSOR "\033[s"
/* Restores cursor position after a Save Cursor. */
#define ESC_UNSAVE_CURSOR "\033[u"
#define ESC_HIDE_CURSOR "\033[?25l"
#define ESC_SHOW_CURSOR "\033[?25h"
/* Save current cursor position. */
// #define ESC_SAVE_CURSOR_ATTR "\0337"
/* Restores cursor position after a Save Cursor. */
// #define ESC_RESTORE_CURSOR_ATTR "\0338"
///////////////////////////////
// Erasing Text
///////////////////////////////
/* Erases from the current cursor position to the end of the current line. */
#define ESC_ERASE_END_OF_LINE "\033[K"
/* Erases from the current cursor position to the start of the current line. */
#define ESC_ERASE_START_OF_LINE "\033[1K"
/* Erases the entire current line. */
#define ESC_ERASE_LINE "\033[2K"
/* Erases the screen from the current line down to the bottom of the screen. */
#define ESC_ERASE_DOWN "\033[J"
/* Erases the screen from the current line up to the top of the screen. */
#define ESC_ERASE_UP "\033[1J"
/* Erases the screen with the background colour and moves the cursor to home. */
#define ESC_ERASE_SCREEN "\033[2J"
///////////////////////////////
// Define Key
///////////////////////////////
/* Associates a string of text to a keyboard key. {key} indicates the key by its ASCII value in decimal. */
#define ESC_SET_KEY_DEFINITION "\033[%d;\"%s\"p"
///////////////////////////////
// Set Display Attributes
///////////////////////////////
/* Sets multiple display attribute settings. The following lists standard attributes: */
#define ESC_SET_ATTRIBUTE_MODE_3 "\033[%d;%d;%dm"
#define ESC_SET_ATTRIBUTE_MODE_2 "\033[%d;%dm"
#define ESC_SET_ATTRIBUTE_MODE_1 "\033[%dm"
/*
0 Reset all attributes
1 Bright
2 Dim
4 Underscore
5 Blink
7 Reverse
8 Hidden
----Foreground Colours
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White
----Background Colours
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White
*/
#ifdef __cplusplus
}
#endif
#endif /* R2_TERMCUI_H */
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2020 Rob Rohan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/