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.Does anyone know how this can be achieved in Bash script?
Asked
Active
Viewed 2,050 times
0

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

Jacob James
- 13
-
@Quasímodo He means the first line: black text on white background looks like "ordinary colors, but inverted". – Hermann May 31 '20 at 00:22
-
@Hermann Thanks, I need to learn to read titles too... – Quasímodo May 31 '20 at 00:25
-
It looks like you have an application window theming issue, yr app being nano. This seems unrelated to bash per se. – Cbhihe May 31 '20 at 10:12
1 Answers
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
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
-
-
1Reverse 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