How to right align and left align text strings in Bash - bash

I'm creating a bash script and would like to display a message with a right aligned status (OK, Warning, Error, etc) on the same line.
Without the colors, the alignment is perfect, but adding in the colors makes the right aligned column wrap to the next line, incorrectly.
#!/bin/bash
log_msg() {
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)
MSG="$1"
let COL=$(tput cols)-${#MSG}
echo -n $MSG
printf "%${COL}s" "$GREEN[OK]$NORMAL"
}
log_msg "Hello World"
exit;

I'm not sure why it'd wrap to the next line -- having nonprinting sequences (the color changes) should make the line shorter, not longer. Widening the line to compensate works for me (and BTW I recommend using printf instead of echo -n for the actual message):
log_msg() {
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)
MSG="$1"
let COL=$(tput cols)-${#MSG}+${#GREEN}+${#NORMAL}
printf "%s%${COL}s" "$MSG" "$GREEN[OK]$NORMAL"
}

You have to account for the extra space provided by the colors.
log_msg() {
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)
MSG="$1"
STATUS="[OK]"
STATUSCOLOR="$GREEN${STATUS}$NORMAL"
let COL=$(tput cols)-${#MSG}+${#STATUSCOLOR}-${#STATUS}
echo -n $MSG
printf "%${COL}s\n" "$STATUSCOLOR"
}

Related

How can I clean a string in bashrc?

I'd like to know how to "clean up" a line of code in bash. I've created a string to colour the command line in my ~/.bashrc which looks like this:
PS1='\[\033[1;36m\]\u\[\033[1;31m\]#\[\033[1;32m\]\h:\[\033[1;35m\]\w\[\033[1;31m\]\$\[\033[0m\] '
I'd like to make it easier to discern. More something along these lines:
PS1='\[\033[1;36m\]
\u\[\033[1;31m\]
#\[\033[1;32m\]
\h:\[\033[1;35m\]
\w\[\033[1;31m\]
\$\[\033[0m\] '
I've tried putting these {} brackets but that didn't seem to work, either.
EDIT: thanks to tripleee. The code that works is as follows:
tprompt () {
local bold=$(tput bold)
local red=$(tput setaf 1)
local green=$(tput setaf 2)
local magenta=$(tput setaf 5)
local cyan=$(tput setaf 6)
local plain=$(tput sgr0)
printf -v PS1 "%s" "$bold" "$cyan" '\u' \
"$red" "#" \
"$green" '\h:' \
"$magenta" '\w' \
"$red" '\$' \
"$plain"
}
tprompt
A better overall approach might be to use tput to generate these codes, rather than hard-code them for a specific terminal. This also allows you to assign mnemonic names to them.
tprompt () {
local bold=$(tput bold)
local red=$(tput setaf 1)
local green=$(tput setaf 2)
local magenta=$(tput setaf 5)
local cyan=$(tput setaf 6)
local plain=$(tput sgr0)
printf -v PS1 "%s" "$bold" "$cyan" '\u' \
"$red" "#" \
"$green" '\h:' \
"$magenta" '\w' \
"$red" '\$' \
"$plain" ' '
}
tprompt
(Not sure I managed to correctly map back the escape codes to their meanings, but you should at least be able to see how this is more readable and maintainable than what you had. The sole reason to define a function for this is to create a local scope so the helper variables don't pollute the global namespace. We only call the function once; you could even undefine the function when you have called it, too.)
As an aside, notice also how printf lets you easily paste together multiple strings. Even if you don't adopt this approach wholesale, you might want to refactor to
printf -v PS1 '%s' \
'\[\033[1;36m\]' \
'\u\[\033[1;31m\]' \
'#\[\033[1;32m\]' \
'\h:\[\033[1;35m\]' \
'\w\[\033[1;31m\]' \
'\$\[\033[0m\] '
As a final aside, non-printing sequences in the prompt should be wrapped in \[ ... \] to tell Bash to disregard them when calculating the line length. Maybe wrap tput with a function, too:
tput () {
printf '\\['
command tput "$#"
printf '\\]'
}

Bash and Zsh prompt that ring a bell and do display error code of last command

I want to have the same prompt in both Bash and Zsh. And I want it to:
bell a ring, if the last command failed, and
display its error code.
In Bash, I do have:
BLK="\[$(tput setaf 0; tput bold)\]"
RED="\[$(tput setaf 1; tput bold)\]"
grn="\[$(tput setaf 2)\]"
GRN="\[$(tput setaf 2; tput bold)\]"
yel="\[$(tput setaf 3)\]"
reset_color="\[$(tput sgr0)\]"
PS1='\n\
`if [[ $? -gt 0 ]]; then printf "\[\033[01;31m\]$?"; tput bel; else printf "\[\033[01;32m\]0"; fi`\
\[\033]0;$PWD\007\] \
\[\033[0;32m\]\u#\h\
\[\033[01;30m\]:\
\[\033[;;33m\]\w\
\[\033[36m\]`__git_ps1`\
\[\033[0m\]\n$ '
In Zsh, that's my config:
BLK=$(tput setaf 0; tput bold)
RED=$(tput setaf 1; tput bold)
grn=$(tput setaf 2)
GRN=$(tput setaf 2; tput bold)
yel=$(tput setaf 3)
reset_color=$(tput sgr0)
PROMPT="
%(?.$GRN.$RED)%?$reset_color $grn%n#%m$BLK:$reset_color$yel%~ $reset_color
%(!.#.$) "
And this is how it looks like in the terminal:
Both prompts do ring the bell when there is an error with the last command.
But, in Bash, it prints 0 instead of the right return code of the command that
failed.
How to fix that?
PS- Any better way to improve the above code is welcomed!
The command to test $? itself resets $? to the result of the test. You need to save the value you want to display first.
PS1='\n\
$(st=$?; if [[ $st -gt 0 ]]; then printf "\[\033[01;31m\]$st"; tput bel; else printf "\[\033[01;32m\]0"; fi)\
\[\033]0;$PWD\007\] \
\[\033[0;32m\]\u#\h\
\[\033[01;30m\]:\
\[\033[;;33m\]\w\
\[\033[36m\]`__git_ps1`\
\[\033[0m\]\n$ '
I would recommend building up the value of PS1 using PROMPT_COMMAND, instead of embedding executable code. This gives you more flexibility
for commenting and separating any computations you need from the actual
formatting. make_prompt doesn't need quite so many lines, but it's
just a demonstration.
set_title () {
printf '\033]0;%s%s' "$1" "$(tput bel)"
}
make_prompt () {
local st=$?
local c bell
bell=$(tput bel)
# Green for success, red and a bell for failure
if [[ $st -gt 0 ]]; then
c=31
else
c=32 bell=
fi
win_title=$(set_title "$PWD")
git_status=$(__git_ps1)
PS1="\n"
PS1+="\[\e[01;${c}m$bell\]" # exit status color and bell
PS1+=$st
PS1+="\[$win_title\]" # Set the title of the window
PS1+="\[\e[0;32m\]" # Color for user and host
PS1+="\u#\h"
PS1+="\[\e[01;30m\]" # Color for : separator
PS1+=":"
PS1+="\[\e[;;33m\]" # Color for directory
PS1+="\w"
PS1+="\[\e[36m\]" # Color for git branch
PS1+=$git_status
PS1+="\[\e[0m\]" # Reset to terminal defaults
PS1+="\n$ "
}
PROMPT_COMMAND=make_prompt
zsh already has terminal-agnostic escape sequences for adding color.
PROMPT="%B%(?.%F{green}.%F{red}$(tput bel))%?%f%b %F{green}%n#%m%F{black}%B:%b%F{yellow}%~ %f%(!.#.$) "
Any external command (such as printf or tput) resets the value of $?. You need to capture it in a variable before that happens.
rc=$?
if [ $rc -gt 0 ]; then
printf "\[\033[01;31m\]$rc"
tput bel
else
printf "\[\033[01;32m\]0"
fi
(Unwrapped for legibility; this will replace the code inside the backticks.)
Notice that this will still overwrite the value of $?; perhaps add exit $? at the end to properly preserve the value.

How to print a string in color, when a Variable has the actual color name?

I am trying to print a string with Specific color depending on the error code. Color varies on each error code, I am storing the actual color name in variable and using it in printf.
BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
BLUE=$(tput setaf 4)
CYAN=$(tput setaf 5)
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
# this color varies depending on error code
color=GREEN
printf "\${$color} This is a String ${NORMAL} \n"
But I get output as
${GREEN} This is a String
Expected output(In actual Green color)
This is a String
I can get this using
printf "${GREEN} This is a String ${NORMAL} \n"
But I want this output using color variable
Bash doesn't treat ${<variable>} recursively inside strings, as you tried to do.
You can do color=$<colorVariable>, ie color=$GREEN before the printf, and then in the printf string doing "$color This is a String ${NORMAL} \n"
So, final result:
BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
BLUE=$(tput setaf 4)
CYAN=$(tput setaf 5)
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
# Event-dependant color
color=$GREEN
# And finally,
printf "$color This is a String ${NORMAL} \n"
There's another way you can do it.
# Define all colors, only GREEN and NORMAL here for brevity reasons
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)
# Notice the missing $, as the original question
color=GREEN
# And then, use Bash's variable reference method (here notice both ! instead of $ and the missing \ at the beginning
printf "${!color} This is a String ${NORMAL} \n"
How about:
color=$GREEN
And then:
printf "$color This is a String ${NORMAL} \n"
Gives me:
This is a String
in green.

Problem with printf width field formatting

I am trying to right-align coloured text with a given field width.
Based on right text align - bash
and Using colors with printf
I don't understand the output of:
blue=$(tput setaf 4)
normal=$(tput sgr0)
printf "%4s\n" "${blue}aaa${normal}"
blue=$(tput setaf 4)
normal=$(tput sgr0)
printf "%10s\n" "${blue}aaa${normal}"
blue=$(tput setaf 4)
normal=$(tput sgr0)
printf "%40s\n" "${blue}aaa${normal}"
blue=$(tput setaf 4)
normal=$(tput sgr0)
printf "%8s\n" "${blue}aaa${normal}"
which is:
aaa
aaa
aaa
aaa
why are entries 1,2 and 4 left-aligned?
Thanks!
GNU bash, version 4.4.23(1)-release (x86_64-redhat-linux-gnu)

Bash colour one word using echo

I am wanting to colourize one word in the middle of an echo sentence, but can't seem to achieve this.
This works:
#!/bin/bash
wipe="\033[1m\033[0m"
yellow='\E[1;33'
echo -e "$yellow"
echo Hello World
echo -e "$wipe"
But this doesn't:
#!/bin/bash
wipe="\033[1m\033[0m"
yellow='\E[1;33'
black="40m"
echo -e "Output a $yellow coloured $wipe word."
# or
echo -e "Output a ${yellow} coloured ${wipe} word."
What am I stupidly doing wrong? :)
Much better, use tput to set a foreground colour:
textreset=$(tput sgr0) # reset the foreground colour
red=$(tput setaf 1)
yellow=$(tput setaf 2)
echo "Output a ${yellow} coloured ${textreset} ${red} word ${textreset}."
You forgot an m in your ANSI escape code for yellow. This works:
yellow='\E[1;33m'

Resources