Home Termcolor Library
Post
Cancel

Termcolor Library

TermColor Is a C library used for ANSII Color formatting for Linux/Mac terminal output.

Termcolor Library

Installation

There are two ways to install termcolor.h library:

1. Install into gcc includes folder

need root permissions

1
curl https://raw.github.com/cyounes/termcolor/master/quickinstall.sh | sh

include it in your code:

1
#include <termcolor.h>

2. download termcolor.h in your project folder

get termcolor.h file:

1
curl -O https://raw.github.com/cyounes/termcolor/master/termcolor.h

include it in your code then start use the libray :)

1
#include "termcolor.h"

Termcolor users documentation

cprint() function facilitates the use of the termcolor library, the programer needs to know just some tags to get a good results. So to get a good results you must be OK to use cprint() instead of printf() :P

Example 1

1
cprint("${bd}Hello world!${/bd}\n");

In this example, the ${bd} tag tells the program to START showing text in bold and the ${/bd} tag tells the program to STOP showing text in bold.

Example 2

1
cprint("${ul}Hello world!${/ul}\n");

In the second example, the ${ul} tag tells the program to START underlining text and the ${/ul} tag tells the program to STOP underlining the text.

Easy to use?

The principle of using this function is roughly the principle of html tag, every time you open a tag you must close it after inserting a code. Except it is not quite the same, as you will see in the following examples…

Tags:

Text decoration

Tag Name:What it does:

${bd}

Start Bold

${/bd}

Stop Bold

${ul}

Start Underline

{/ul}

Stop underline

${bl}

Start blink

${/bl}

Stop blink

Colors:

Effects:
Tag name:What it does:

${rv}

reverse colors

${/rv}

stop reversing colors

${bi}

use high intensity for background color

${/bi}

stop using high intensity for background color

${fi}

use high intensity for foreground color

${/fi}

stop using high intensity for foreground color

Foreground and Background colors:
Background color tags:
Tag name:What it does:

${bb}

Black background

${rb}

Red background

${gb}

Green background

${yb}

Yellow background

${ub}

Blue background

${mb}

Magenta background

${cb}

Cyan background

${wb}

White background

${/bg}

Stop using background

Foreground color tags:
Tag name:What it does:

${bf}

Black foreground

${rf}

Red foreground

${gf}

Green foreground

${yf}

Yellow foreground

${uf}

Blue foreground

${mf}

Magenta foreground

${cf}

Cyan foreground

${wf}

White foreground

${/fg}

Stop using foreground

autoResetStyle():

This function has one BOOLEAN argument, when it take the TRUE variable, you need to restart all effects and decoration you did in the previous cprint(). Otherwise, if you forget to close the tags in the previous cprint() , the next one continue applying all the effects and decorations that you have forgot to close.

  • Example:

  • auto reset :

1
2
3
autoResetStyle(TRUE);
cprint("${bd}Hello ");
cprint("world\n");

this will display Hello in bold and world in normal weight.

  • don’t auto reset :
1
2
3
autoResetStyle(FALSE);
cprint("${bd}Hello ");
cprint("world\n");

this will display both Hello and world in bold

cprint() and variables:

Currently, cprint() must exactly take one arguments (char *) takes more than one argument, however it takes only the int %d , char %c and strings char * %s , it may prints anything wrong if you give a long or float argument. in the next versions may be developed to take all the data types possible which is the case of printf() .

So to print a variable of another data type using effects, you must disable auto reset by doing : autoResetStyle(FALSE); then put the printf(args) between tow cprint()s.

Example:

1
2
3
4
5
6
7
long a=10, b=10;
autoResetStyle(FALSE);
cprint("a = ${bd} }
printf("%ld",a);
cprint("${/bd} b= ${bd}");
printf("%ld, b);
cprint("${/bd}\n");

Termcolor developers documentation:

Available Colors:

BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE

BOOLEAN ?

By including termcolor library in your code, you don’t need to include the stdbool library. However you need to write the boolean keywords in uppercase characters: TRUE and FALSE

Functions:

Colors:

  • bgColor(COLOR) : set the background color using the available colors.

  • fgColor(COLOR) : set the foreground color using the available colors.

for bgColor(COLOR) and fgColor(COLOR) functions use the variable DEFAULTas parameter to reset the default color:

1
2
bgColor(DEFAULT);
fgColor(DEFAULT);

Text decorations:

  • textBold(BOOLEAN) : enable or disable text bolding for the next output:
    1
    2
    
    textBold(TRUE);
    textBold(FALSE);
    
  • textBlink(BOOLEAN) : enable or disable text blinking for the next output:
    1
    2
    
    textBlink(TRUE);
    textBlink(FALSE);
    
  • colorReverse(BOOLEAN): enable or disable colors reversing for the next output:
1
2
colorReverse(TRUE);
colorReverse(FALSE);
  • textUnderline(BOOLEAN): underline text

  • highFgIntensity(BOOLEAN) : Use High Intensity for foreground colors.

  • highBgIntensity(BOOLEAN) : Use High Intensity for background colors.

  • setStyle() : Usually the programer don’t need to invoke this function, it will be invoked automatically by the other functions each time you change the style.

  • resetStyle() : Reset all colors and decorations by default!, i suggest to invoke this function at the end of your main to restore all as default.

Examples:

Red Background:
1
2
bgColor(RED);
printf("text with RED Background");
Result:

termcolor red background -

Default background + Red foreground:
1
2
3
bgColor(DEFAULT);
fgColor(RED);
printf("text with RED Foreground");
Result:

termcolor red foreground -

Yellow text on red background:
1
2
3
bgColor(RED);
fgColor(YELLOW);
printf("YELLOW text in RED Background");
Result:

Yellow text on red background -

Yellow Bold text on red background:
1
2
3
4
bgColor(RED);
fgColor(YELLOW);
textBold(TRUE);
printf("YELLOW and BOLD text in RED Background");
Result:

termcolor Yellow Bold text on red background -

Yellow, Bold and underlined text on red background:
1
2
3
4
5
bgColor(RED);
fgColor(YELLOW);
textBold(TRUE);
textUnderline(TRUE);
printf("YELLOW and BOLD text in RED Background");
Result:

termcolor Yellow, Bold and underlined text on red background -

High Intensity: Yellow Bold text on red background:
1
2
3
4
5
6
bgColor(RED);
fgColor(YELLOW);
textBold(TRUE);
highFgIntensity(TRUE);
highBgIntensity(TRUE);
printf("YELLOW and BOLD text in RED Background");
Result:

termcolor High Intensity: Yellow Bold text on red background -

Reversed colors:
1
2
3
4
5
bgColor(DEFAULT);
fgColor(DEFAULT);
textBold(TRUE);
textUnderline(TRUE);
printf("REVERSED test with default foreground and default background :)");
Result:

termcolor Reversed colors -

Main Example:
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
#include <stdio.h>
#include "termcolor.h"

int main() {

	printf("Hi, Thanks for trying termcolor library :) \n" );
	printf("Using this library allow the developer to decorate \
the output of its console application.\n\n");
	printf("Some Examples: \n\n");

	bgColor(RED); // Background RED and not "RED" !
	printf("text with RED Background\n\n");
	bgColor(DEFAULT); // Default Background Color
	fgColor(RED); // RED Foreground color
	printf("text with RED Foreground\n\n");
	bgColor(RED);
	fgColor(YELLOW);
	printf("YELLOW text in RED Background\n\n");
	textBold(TRUE); // Bold Text
	printf("YELLOW and BOLD text in RED Background\n\n");
	textUnderline(TRUE); // Underlined text
	bgColor(DEFAULT);
	printf("YELLOW and BOLD and Underlined text\n\n");
	fgColor(DEFAULT);
	colorReverse(TRUE);
	printf("REVERSED test with default foreground and default background :)\n\n");
	colorReverse(FALSE);
	textBlink(TRUE);
	printf("BLINKING text with default foreground and default background\n\n");

	resetStyle(); // Reset all as default
	printf("Now Examples using High Intensity\n\n");
	highFgIntensity(TRUE);
	highBgIntensity(TRUE);
	bgColor(RED);
	printf("text with RED Background\n\n");
	bgColor(DEFAULT);
	fgColor(RED);
	printf("text with RED Foreground\n\n");
	bgColor(RED);
	fgColor(YELLOW);
	printf("YELLOW text in RED Background\n\n");
	textBold(TRUE);
	printf("YELLOW and BOLD text in RED Background\n\n");

	resetStyle();
	printf("Simple Text :D \n\n");

    return 0;
}

Result:

termcolor Reversed colors -

TODO:

  • insert horizontal line with specified color.
  • Text align : [Left ; Center ; Right ].
  • Text Border.
  • add availability to cprint() to take more than one argument.

Fork me on GitHub:

I’ll be very happy to take pull requests from others, Go ahead and fork me.

This post is licensed under CC BY-NC-SA 4.0 by the author.

Walletix Python API

Observable Concurrent Queue