prompt-toolkit / prompt-toolkit/python-prompt-toolkit

Add support for VT100 terminal foreground/background color query

Open
#2,050 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
10.6k
Forks
815
PR merge metrics
No merged PRs in 30d

Description

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 ???).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating prompt_toolkit's existing VT-100 cursor-position handling and terminal input/output entry points. Compare them with the described Bash query for foreground and background colors, then define what successful support should return and how it should behave when the terminal does not respond.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.