【问题标题】:Is it possible to determine the background color of the terminal in a linux machine?是否可以确定 linux 机器中终端的背景颜色?
【发布时间】:2017-05-12 07:00:40
【问题描述】:

我正在构建一个 node.js CLI,我想显示一个 ASCII 艺术徽标。但是根据终端的背景颜色,我想更改徽标的颜色。是否有可能做到这一点?如果是怎么办?谢谢你。

【问题讨论】:

标签: linux ubuntu system


【解决方案1】:

你要找的东西在这里:

这里:

#!/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/]\+$//'

【讨论】:

  • 请考虑将解决方案本身包含在您的答案中(根据 SO 指南),或者如果您认为是重复问题,请将该问题标记为重复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2021-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
相关资源
最近更新 更多