Skip to content

Add support for VT100 terminal foreground/background color query #2050

@FabriceSalvaire

Description

@FabriceSalvaire

Sometimes, it can be useful to know if the user is using a black or white terminal to set colours.

I got this Bash script, but I am unable to make it works on Python:

#!/bin/bash
# /dev//tty is the terminal for the current process
success=false
# redirect shell stdin to tty
# useless ???
# exec < /dev/tty
# -g --save
#   return a string
oldstty=$(stty -g)
# raw same as
#     -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -icanon -opost  -isig
#     -iuclc -ixany -imaxbel -xcase min 1 time 0
# -echo   NO echo input characters
# min N   with -icanon, set N characters minimum for a completed read
# useless ???
# stty raw -echo min 0
stty -echo
# else
#  ^[]11;rgb:2323/2626/2727^[\rgb:2323/2626/2727
col=11   # background
# -n   do not append a newline
# -e   enable interpretation of the following backslash escapes
#          OSC   Ps  ;Pt ST
echo -en "\033]${col};?\033\\" > /dev/tty  # echo opts differ w/ OSes
# 10;rgb:fcfc/fcfc/fcfc
# 11;rgb:2323/2626/2727
result=
# IFS Input Field Separator
# Read a line from the standard input and split it into fields.
#   -r         do not allow backslashes to escape any characters
#   -d delim   continue until the first character of DELIM is read, rather than newline
# useless ???
# if IFS=';' read -r -d '\' color ; then
if read -r -d '\' color ; then
    # s/^.*\;// ; s/[^rgb:0-9a-f/]//g
    result=$(echo $color | sed 's/^.*\;//')
    success=true
fi
# restore tty setup
stty $oldstty
# print value
echo $result
# rgb:2323/2626/2727
$success

The magic seems in stty -echo to disable echo and read -r -d '\' color to get the value.

I didn't find any code in prompt_toolkit to query VT-100 (excepted cursor position ???).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions