0

I understand that the title may seem a little confusing. However, I have an example, GNU Nano. The Nano editor has this one line at the top whose color is totally inverted.Screencap of GNU Nano top barDoes anyone know how this can be achieved in Bash script?

Quasímodo
  • 18,865
  • 4
  • 36
  • 73

1 Answers1

1

That's quite easy, you can try this:

# Reset
reset='\033[0m'

# White Background
BG='\033[47m'

# Black Foreground
FG='\033[0;30m'

# Usage
echo -e "$FG$BG This will print black text on white background $reset"

If you want the whole line:

reset='\033[0m'
BG='\033[47m'
FG='\033[0;30m'

text="A black text on white"
cols=$(tput cols)

# Left Aligned
x=$((cols-${#text}))

printf "$FG$BG%s%*s$reset\n" "$text" $x

# Centered text
x_center=$(((${#text}+cols)/2))
x_rest=$((cols-x_center))

printf "$FG$BG%*s%*s$reset\n" $x_center "$text" $x_rest

Example output: black text on white

More detail: https://stackoverflow.com/a/28938235/3689465

annahri
  • 2,075
  • @JacobJames Well, you didn't mention it in your question that you need it to be on top of the screen. Otherwise, It might be more trickier. Or maybe a lazy way to do it, do clear then print the output, bam! you'll get it on the top of the screen (at least terminal screen). – annahri May 31 '20 at 00:59
  • @Jaob James: please edit your OQ to fully expose your question. For your information, comments aren't for adding details to the OQ. Moreover, once a question is correctly asked and answered, comments are meant do be deleted. – dan May 31 '20 at 13:23
  • Thanks, @annahri! – Jacob James May 31 '20 at 18:21
  • 1
    Reverse video is fun. There's a reason that programs tend to do what this script does, namely manually swap the colours and print lots of spaces, rather than turn the ECMA-48 reverse video attribute on. (-: – JdeBP Jun 01 '20 at 02:56