2018-10-02 Terminal

I really like the macros used for ANSI sequences from 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

Tags:

Comments


Please make sure you contribute only your own work, or work licensed under the GNU Free Documentation License. Note: in order to facilitate peer review and fight vandalism, we will store your IP number for a number of days. See Privacy Policy for more information. See Info for text formatting rules. You can edit the comment page if you need to fix typos. You can subscribe to new comments by email without leaving a comment.

To save this page you must answer this question:

Please say HELLO.