C code changes terminal text color; how to restore defaults? Linux

I have a C file running on Linux. It prints some lines in red (failures) and some in green (passes). As you might expect, it uses escape codes in the printf statements as follows:

#define BLACK  "\033[22;30m"
#define GREEN  "\033[22;31m"

printf(GREEN "this will show up green" BLACK "\n");

If the BLACK at the end wasn't there, the terminal text will continue to be green for everything. In case you didn't catch it, that's fine for a terminal window with a non-black background, but otherwise you'll end up with black-on-black. Not good! Running the program has this problem, as does capturing the output in a text file and then viewing the file with "more" or "less".

Is there a code to restore defaults instead of specifying a color at the end of the printf statement? This needs to be in C, but I would be interested in reading about other approaches.

Update: Thank you all. Your responses helped me find even more useful info elsewhere. Я обновил свои макросы следующим образом (примечание 31 для красного цвета, и я исправил эту опечатку ниже):

#define RESET_COLOR "\e[m"
#define MAKE_GREEN "\e[32m"

printf(MAKE_GREEN "this will show up green" RESET_COLOR "\n");

Я нашел следующие ссылки полезными для понимания работы этих кодов:

http://www.phwinfo.com/forum /comp-unix-shell/450861-bash-shell-escapes-not-working-via-putty-ssh.html объясняет, что делают эти escape-последовательности, и использует ncurses, если требуется переносимость.

http: //www.linuxselfhelp.com/howtos/Bash-Prompt/Bash-Prompt-HOWTO-6.html

http://bluesock.org/~willg/dev/ansi.html показывает еще больше escape-последовательностей ; полезно получить общую картину

18
задан jasper77 18 August 2010 в 15:09
поделиться

3 ответа

Попробуйте использовать:

#define RESETCOLOR "\033[0m"

Это должно сбросить его до значений по умолчанию.

Подробнее об этих кодах терминалов можно найти здесь: http://en.wikipedia.org/wiki/ANSI_escape_code

23
ответ дан 30 November 2019 в 08:10
поделиться
"\033[0m"

См. Здесь: http://en.wikipedia.org/wiki/ANSI_color

4
ответ дан 30 November 2019 в 08:10
поделиться

тип reset в терминале .

В Linux и OSX есть бинарный файл под названием reset.

2
ответ дан 30 November 2019 в 08:10
поделиться