Last active
October 16, 2022 18:53
-
-
Save natanaeljr/8b26dba7b876e2c006bfa28a4147b407 to your computer and use it in GitHub Desktop.
Some ANSI Escape Codes for controlling a terminal, (CSI - Control Sequence Introducer), originaly made to work with ESP32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _ESCAPE_CODE_H_ | |
#define _ESCAPE_CODE_H_ | |
#include <stdio.h> | |
#include "freertos/FreeRTOS.h" | |
#include "freertos/task.h" | |
#define ESC "\033" | |
#define ESC_CSI ESC "[" | |
// text style | |
#define ESC_STYLE_RESET ESC_CSI "0m" /*!< Reset text, font and color attributes */ | |
#define ESC_TEXT_BOLD ESC_CSI "1m" | |
#define ESC_TEXT_DIM ESC_CSI "2m" | |
#define ESC_TEXT_ITALIC ESC_CSI "3m" | |
#define ESC_TEXT_UNDERLINE ESC_CSI "4m" | |
#define ESC_TEXT_SLOW_BLINK ESC_CSI "5m" | |
#define ESC_TEXT_FAST_BLINK ESC_CSI "6m" | |
#define ESC_TEXT_INVISIBLE ESC_CSI "8m" | |
#define ESC_TEXT_STRIKE_THROUGH ESC_CSI "9m" | |
#define ESC_TEXT_FRAKTUR ESC_CSI "20m" | |
#define ESC_TEXT_BOLD_OFF ESC_CSI "21m" | |
#define ESC_TEXT_DIM_OFF ESC_CSI "22m" | |
#define ESC_TEXT_ITALIC_OFF ESC_CSI "23m" | |
#define ESC_TEXT_UNDERLINE_OFF ESC_CSI "24m" | |
#define ESC_TEXT_BLINK_OFF ESC_CSI "25m" | |
#define ESC_TEXT_REVEAL ESC_CSI "28m" | |
#define ESC_TEXT_STRIKE_THROUGH_OFF ESC_CSI "29m" | |
#define ESC_TEXT_FRAKTUR_OFF ESC_CSI "23m" | |
#define ESC_FONT_DEFAULT ESC_CSI "10m" | |
#define ESC_FONT(n) ESC_CSI "1" #n "m" /*!< font: 1-9 */ | |
// colors | |
#define ESC_COLOR_REVERSE ESC_CSI "7m" | |
#define ESC_COLOR_REVERSE_OFF ESC_CSI "27m" | |
#define ESC_FGCOLOR_BLACK ESC_CSI "30m" | |
#define ESC_FGCOLOR_RED ESC_CSI "31m" | |
#define ESC_FGCOLOR_GREEN ESC_CSI "32m" | |
#define ESC_FGCOLOR_YELLOW ESC_CSI "33m" | |
#define ESC_FGCOLOR_BLUE ESC_CSI "34m" | |
#define ESC_FGCOLOR_MAGENTA ESC_CSI "35m" | |
#define ESC_FGCOLOR_CYAN ESC_CSI "36m" | |
#define ESC_FGCOLOR_WHITE ESC_CSI "37m" | |
#define ESC_FGCOLOR_DEFAULT ESC_CSI "39m" | |
#define ESC_FGCOLOR_BRIGHT_BLACK ESC_CSI "90m" | |
#define ESC_FGCOLOR_BRIGHT_RED ESC_CSI "91m" | |
#define ESC_FGCOLOR_BRIGHT_GREEN ESC_CSI "92m" | |
#define ESC_FGCOLOR_BRIGHT_YELLOW ESC_CSI "93m" | |
#define ESC_FGCOLOR_BRIGHT_BLUE ESC_CSI "94m" | |
#define ESC_FGCOLOR_BRIGHT_MAGENTA ESC_CSI "95m" | |
#define ESC_FGCOLOR_BRIGHT_CYAN ESC_CSI "96m" | |
#define ESC_FGCOLOR_BRIGHT_WHITE ESC_CSI "97m" | |
#define ESC_FGCOLOR_8BIT(n) ESC_CSI "38;5;" #n "m" | |
#define ESC_FGCOLOR_RGB(r, g, b) ESC_CSI "38;2;" #r ";" #g ";" #b "m" | |
#define ESC_BGCOLOR_BLACK ESC_CSI "40m" | |
#define ESC_BGCOLOR_RED ESC_CSI "41m" | |
#define ESC_BGCOLOR_GREEN ESC_CSI "42m" | |
#define ESC_BGCOLOR_YELLOW ESC_CSI "43m" | |
#define ESC_BGCOLOR_BLUE ESC_CSI "44m" | |
#define ESC_BGCOLOR_MAGENTA ESC_CSI "45m" | |
#define ESC_BGCOLOR_CYAN ESC_CSI "46m" | |
#define ESC_BGCOLOR_WHITE ESC_CSI "47m" | |
#define ESC_BGCOLOR_DEFAULT ESC_CSI "49m" | |
#define ESC_BGCOLOR_BRIGHT_BLACK ESC_CSI "100m" | |
#define ESC_BGCOLOR_BRIGHT_RED ESC_CSI "101m" | |
#define ESC_BGCOLOR_BRIGHT_GREEN ESC_CSI "102m" | |
#define ESC_BGCOLOR_BRIGHT_YELLOW ESC_CSI "103m" | |
#define ESC_BGCOLOR_BRIGHT_BLUE ESC_CSI "104m" | |
#define ESC_BGCOLOR_BRIGHT_MAGENTA ESC_CSI "105m" | |
#define ESC_BGCOLOR_BRIGHT_CYAN ESC_CSI "106m" | |
#define ESC_BGCOLOR_BRIGHT_WHITE ESC_CSI "107m" | |
#define ESC_BGCOLOR_8BIT(n) ESC_CSI "48;5;" #n "m" /*!< 0~255 */ | |
#define ESC_BGCOLOR_RGB(r, g, b) ESC_CSI "48;2;" #r ";" #g ";" #b "m" | |
// cursor | |
#define ESC_CURSOR_VISIBLE ESC_CSI "?25h" | |
#define ESC_CURSOR_INVISIBLE ESC_CSI "?25l" | |
#define ESC_CURSOR_HOME ESC_CSI "H" | |
#define ESC_CURSOR_POS(v, h) ESC_CSI #v ";" #h "H" | |
#define ESC_CURSOR_SAVE_POS ESC_CSI "s" | |
#define ESC_CURSOR_RESTORE_POS ESC_CSI "u" | |
#define ESC_CURSOR_GET_POS ESC_CSI "6n" /*!< Response: ESCLine;ColumnR (e.g. '\e30;10R') */ | |
#define ESC_CURSOR_MOVE_UP(n) ESC_CSI #n "A" | |
#define ESC_CURSOR_MOVE_DOWN(n) ESC_CSI #n "B" | |
#define ESC_CURSOR_MOVE_FORWARD(n) ESC_CSI #n "C" | |
#define ESC_CURSOR_MOVE_BACK(n) ESC_CSI #n "D" | |
#define ESC_CURSOR_COLUMN(n) ESC_CSI #n "G" | |
#define ESC_CURSOR_LINE_DOWN(n) ESC_CSI #n "E" | |
#define ESC_CURSOR_LINE_UP(n) ESC_CSI #n "F" | |
#define ESC_CURSOR_NEXT_LINE ESC "E" | |
// window | |
#define ESC_WINDOW_SCROLL_UP ESC "D" | |
#define ESC_WINDOW_SCROLL_DOWN ESC "M" | |
#define ESC_WINDOW_RESIZE(h, w) ESC_CSI "8;" #h ";" #w "t" | |
// screen | |
#define ESC_SCREEN_CLEAR_DOWN ESC_CSI "0J" | |
#define ESC_SCREEN_CLEAR_UP ESC_CSI "1J" | |
#define ESC_SCREEN_CLEAR ESC_CSI "2J" | |
// line | |
#define ESC_LINE_CLEAR_RIGHT ESC_CSI "0K" | |
#define ESC_LINE_CLEAR_LEFT ESC_CSI "1K" | |
#define ESC_LINE_CLEAR ESC_CSI "2K" | |
#define ESC_LINE_AUTO_WRAP_ON ESC_CSI "?7h" | |
#define ESC_LINE_AUTO_WRAP_OFF ESC_CSI "?7l" | |
// tab | |
#define ESC_TAB_SET_COLUMN ESC_CSI "H" | |
#define ESC_TAB_CLEAR_COLUMN ESC_CSI "[0g" | |
#define ESC_TAB_CLEAR_ALL ESC_CSI "[3g" | |
// others | |
#define ESC_VIDEO_REVERSE ESC_CSI "?5h" | |
#define ESC_VIDEO_NORMAL ESC_CSI "?5l" | |
#define ESC_TERMINAL_RESET ESC "c" | |
#define ESC_TERMINAL_GET_TYPE ESC_CSI "c" /*!< Response: Esc[?1;Value0c */ | |
#define ESC_DEVICE_REPORT ESC_CSI "5n" /*!< Response: Esc0n -> terminal is OK, Esc3n -> terminal is not OK */ | |
// Check for terminal ANSI escape code support | |
static bool probeEscCode() { | |
printf("\033[5n\n"); | |
fflush(stdout); | |
const char* res = "\e[0n"; | |
int timeout = 200; | |
int readbytes = 0; | |
while(timeout > 0 && readbytes < 4) { | |
vTaskDelay(1 / portTICK_PERIOD_MS); | |
if(getchar() == res[readbytes]) | |
readbytes++; | |
timeout--; | |
} | |
if(readbytes < 4) | |
return false; | |
return true; | |
} | |
#endif /* end of include guard: _ESCAPE_CODE_H_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment