TermColor Is a C library used for ANSII Color formatting for Linux/Mac terminal output.
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: |
---|---|
| Start Bold |
| Stop Bold |
| Start Underline |
| Stop underline |
| Start blink |
| Stop blink |
Colors:
Effects:
Tag name: | What it does: |
---|---|
| reverse colors |
| stop reversing colors |
| use high intensity for background color |
| stop using high intensity for background color |
| use high intensity for foreground color |
| stop using high intensity for foreground color |
Foreground and Background colors:
Background color tags:
Tag name: | What it does: |
---|---|
| Black background |
| Red background |
| Green background |
| Yellow background |
| Blue background |
| Magenta background |
| Cyan background |
| White background |
| Stop using background |
Foreground color tags:
Tag name: | What it does: |
---|---|
| Black foreground |
| Red foreground |
| Green foreground |
| Yellow foreground |
| Blue foreground |
| Magenta foreground |
| Cyan foreground |
| White foreground |
| 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 DEFAULT
as 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 texthighFgIntensity(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:
-
Default background + Red foreground:
1
2
3
bgColor(DEFAULT);
fgColor(RED);
printf("text with RED Foreground");
Result:
-
Yellow text on red background:
1
2
3
bgColor(RED);
fgColor(YELLOW);
printf("YELLOW text in RED Background");
Result:
-
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:
-
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:
-
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:
-
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:
-
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:
-
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.