2018-10-02 Terminal

I really like the macros used for ANSI sequences from everything you ever wanted to know about terminals.

everything you ever wanted to know about terminals

#include <unistd.h>
#define szstr(str) str,sizeof(str)

#define     plain "0" /* or "" */
#define        no "2"
#define    bright "1"
#define       dim "2"
#define    italic "3"
#define underline "4"
#define   reverse "7"
#define      with ";"
#define  ansi_esc "\x1b"
#define fmt(style) ansi_esc "[" style "m"

int main() {
	write(1, szstr(    "plain text - "
		fmt(right)     "bright text"     fmt(no bright) " - "
		fmt(dim)       "dim text"        fmt(no dim)    " - "
		fmt(italic)    "italic text"     fmt(no italic) " - "
		fmt(reverse)   "reverse video"   fmt(plain)     " - "
		fmt(underline) "underlined text" fmt(no underline) ")
	);
}

And then you can write code like this: `fmt(underline with bright with no italic)`. And the blog post keeps adding to this! Eight colors, 256 colors, true colors, and so on.

Note: β€œall sample code in this document is the sole property of the author and is released exclusively under the GNU AGPLv3.” – Lexi Summer Hale

​#Programming