0

I am building a node.js CLI and I want to display an ASCII art logo. But depending on the background color of the terminal, I want to change the color of my logo. Is it possible to do this? If yes how? Thank you.

codelyzer
  • 467
  • 1
  • 4
  • 17
  • This answer contains POC shell script code for querying the terminal background color: https://stackoverflow.com/a/30540928/473672 – Johan Walles Oct 15 '21 at 08:50

1 Answers1

2

What you are looking for is here:

AND here:

#!/bin/sh
#
# Query a property from the terminal, e.g. background color.
#
# XTerm Operating System Commands
#     "ESC ] Ps;Pt ST"

oldstty=$(stty -g)

# What to query?
# 11: text background
Ps=${1:-11}

stty raw -echo min 0 time 0
# stty raw -echo min 0 time 1
printf "\033]$Ps;?\033\\"
# xterm needs the sleep (or "time 1", but that is 1/10th second).
sleep 0.00000001
read -r answer
# echo $answer | cat -A
result=${answer#*;}
stty $oldstty
# Remove escape at the end.
echo $result | sed 's/[^rgb:0-9a-f/]\+$//'
Community
  • 1
  • 1
MichaelMMeskhi
  • 659
  • 8
  • 26
  • 2
    Please consider including the solution itself in your answer (as per SO guidelines) or flag the question as duplicate if you think it is. – werediver May 12 '17 at 07:34