Using Colors With Printf

Using colors with printf

You're mixing the parts together instead of separating them cleanly.

printf '\e[1;34m%-6s\e[m' "This is text"

Basically, put the fixed stuff in the format and the variable stuff in the parameters.

How to get color and width formatting with printf

It would appear that the printf function is counting the escape sequence characters that set the colours as part of the output width. Adding the number of characters required for each colour format to the width specifier should fix the issue.

Minimal Working Example:

$ GREEN=$(tput setaf 2) && printf "[%-15s][%-10s][%-5s][${GREEN}%-10s]\n" "Hello" "World" "Eat" "Cake"
[Hello ][World ][Eat ][Cake ]

bash script - output in color using printf

Try below example

#!/bin/bash
printf "\x1b[31m\"this is in red\"\x1b[0m\n";
printf "this is in default color\n";

The output looks like:

Sample Image

colorful text using printf in C

I know that this is incredibly easy to do in C++, but I found this for you to look at in C:

#include <stdio.h>
#include <windows.h> // WinApi header

int main()
{
HANDLE hConsole;
int k;

hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

// you can loop k higher to see more color choices
for(k = 1; k < 255; k++)
{
SetConsoleTextAttribute(hConsole, k);
printf("%3d %s\n", k, "I want to be nice today!");
}

getchar(); // wait
return 0;
}

All of the comments will help you to find your way through the code - hope it helps!

stdlib and colored output in C

All modern terminal emulators use ANSI escape codes to show colours and other things.

Don't bother with libraries, the code is really simple.

More info is here.

Example in C:

#include <stdio.h>

#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"

int main (int argc, char const *argv[]) {

printf(ANSI_COLOR_RED "This text is RED!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_GREEN "This text is GREEN!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_YELLOW "This text is YELLOW!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_BLUE "This text is BLUE!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_MAGENTA "This text is MAGENTA!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_CYAN "This text is CYAN!" ANSI_COLOR_RESET "\n");

return 0;
}

How can I colour the output of printf %s in shell?

You can process the output of the printf with a command which applies the color. For example,

printf %s '
\ /
\ /
\/
/ \
/ \
' | awk 'BEGIN{c=0} {printf "\033[3%dm%s\033[0m\n", 1 + c, $0; c = (c + 1) % 6;}'

gives

^[[31m^[[0m
^[[32m^[[0m
^[[33m\ /^[[0m
^[[34m \ /^[[0m
^[[35m \/^[[0m
^[[36m / \^[[0m
^[[31m/ \^[[0m

In steps:

  • The printf command sends its output via a pipe (the "|") to the awk script which is given in the single-quotes.
  • In that script, I initialized a counter 'c', using the first chunk in curly braces, after the special keyword "BEGIN" (which is executed once).
  • The second chunk in curly-braces is executed for each line read by awk.
  • awk uses its own printf to print the escape sequence setting color, printing the text and resetting the colors.
  • The "$0" symbol holds the whole line (without the newline).
  • I used an expression to update 'c' after each line ("%" is the modulus
    operator, making 'c' range from 0 through 5).

Color text in terminal applications in UNIX

This is a little C program that illustrates how you could use color codes:

#include <stdio.h>

#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"

int main()
{
printf("%sred\n", KRED);
printf("%sgreen\n", KGRN);
printf("%syellow\n", KYEL);
printf("%sblue\n", KBLU);
printf("%smagenta\n", KMAG);
printf("%scyan\n", KCYN);
printf("%swhite\n", KWHT);
printf("%snormal\n", KNRM);

return 0;
}


Related Topics



Leave a reply



Submit