Colored text

From Qi-Hardware
Jump to: navigation, search

There is 16 text colors, to change them you use escape codes. I use bash so if something don't work in ash try bash. There is a lot info on the Internet so i just take some examples.

[edit] Example

Partial example of /etc/profile:

# export PS1='\u@\h:\w\$ '
export PROMPT_COMMAND=fprompt
function fprompt {

local   ECODE="$?"
local   BATTVOLT=`cat /sys/class/power_supply/battery/voltage_now|sed 's/\([0-9]\)/\1./'|cut -c 0-4`

local   NO_COLOR="\[\033[0m\]"
local   PURPLE="\[\033[0;35m\]"
local   BLUE="\[\033[1;34m\]"
local   GREEN="\[\033[1;32m\]"
local   YELLOW="\[\033[1;33m\]"
local   RED="\[\033[1;31m\]"

case $BATTVOLT in
3.[8-9][0-9]|4.[0-1][0-9])
        local BATTCOLOR=$GREEN
        ;;
3.[3-7][0-9])
        local BATTCOLOR=$YELLOW
        ;;
*)
        local BATTCOLOR=$RED
        ;;
esac

if [ $ECODE -eq 0 ]
then local ECOLOR=$GREEN
else local ECOLOR=$RED
fi

if [ $UID -eq 0 ]
then local USERCOLOR=$RED
else local USERCOLOR=$GREEN
fi

local SEPCOLOR=$PURPLE

PS1="$SEPCOLOR[$USERCOLOR\u$SEPCOLOR|$BATTCOLOR$BATTVOLT$SEPCOLOR|$ECOLOR$ECODE$SEPCOLOR|$BLUE\w$SEPCOLOR]$NO_COLOR "

}

It gives you a colorful prompt that look something like this:

[root|3.77|0|/] _
[root|4.12|127|/card] _

The first part is the username (green or red depending on $UID), then battery voltage (green, yellow or red depending voltage), then exit code (green if zero, else red) and last workdir (blue).

[edit] Change colors

As 30-37 sets a text color 40-47 sets background colors. echo -e "\x1B[1;32;43mtest\x1B[0m" - Set text color to bright green (1;32), background to brown (43). Write some text and then restore to normal colors (0). Always restore colors when you are done with them (\x1B[0m).

To get the bright color value for text you use 1;nn where nn is 30-37. To get a bright background color you can send 5;nn where nn is 40-47, but don't expect bright backgrounds to work anywhere but in the console.

A small script to list all colors:

#!/bin/sh
echo -e '\x1B[30m(0;30) \x1B[31m(0;31) \x1B[32m(0;32) \x1B[33m(0;33)
\x1B[34m(0;34) \x1B[35m(0;35) \x1B[36m(0;36) \x1B[37m(0;37)
\x1B[1;30m(1;30) \x1B[1;31m(1;31) \x1B[1;32m(1;32) \x1B[1;33m(1;33)
\x1B[1;34m(1;34) \x1B[1;35m(1;35) \x1B[1;36m(1;36) \x1B[1;37m(1;37)\x1B[0m'
echo -e '\x1B[40m(0;40) \x1B[41m(0;41) \x1B[42m(0;42) \x1B[43m(0;43)\x1B[0m
\x1B[44m(0;44) \x1B[45m(0;45) \x1B[46m(0;46) \x1B[47m(0;47)\x1B[0m
\x1B[5;40m(5;40) \x1B[5;41m(5;41) \x1B[5;42m(5;42) \x1B[5;43m(5;43)\x1B[0m
\x1B[5;44m(5;44) \x1B[5;45m(5;45) \x1B[5;46m(5;46) \x1B[5;47m(5;47)\x1B[0m'

The script above will generate something similar to this: Colors.png

[edit] Redefine colors

You can also redefine the color palette (but only in the console i think).

echo -en "\x1B]PNRRGGBB" - Change color N (0-F, 1-7 equals to 30-37 and 8-F equals to 1;30-1;37) to color 000000-FFFFFF.
echo -en "\x1B]P0002200"  - Makes the background color for the console dark green (by changing the color value for the default background color.
echo -en "\x1B]P700FF00" - Makes the default textcolor very green.
echo -en "\x1B]P7000000\x1B]P0FFFFFF" - Give you a white console with white text. But you should also edit the rest of the palette so all colors is easy to see.
echo -en "\x1B]R" - Resets all colors to normal.

Also take a look at man console_codes (4). //by Rikard

Personal tools
Namespaces
Variants
Actions
Navigation
interactive
Toolbox
Print/export