Skip to content

Commit d510761

Browse files
authored
Set default keyword argument timeout of 1 for get_dec_mode, get_location, get_fgcolor, get_bgcolor. (#315)
1 parent fda1759 commit d510761

File tree

8 files changed

+48
-10
lines changed

8 files changed

+48
-10
lines changed

bin/color_query.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
"""Query terminal foreground and background colors."""
3+
from blessed import Terminal
4+
5+
term = Terminal()
6+
7+
# Get foreground color
8+
r, g, b = term.get_fgcolor()
9+
if (r, g, b) != (-1, -1, -1):
10+
print(f"Foreground color: RGB({r}, {g}, {b})")
11+
print(f" Hex: #{r:04x}{g:04x}{b:04x}")
12+
else:
13+
print("Could not determine foreground color")
14+
15+
# Get background color
16+
r, g, b = term.get_bgcolor()
17+
if (r, g, b) != (-1, -1, -1):
18+
print(f"Background color: RGB({r}, {g}, {b})")
19+
print(f" Hex: #{r:04x}{g:04x}{b:04x}")
20+
else:
21+
print("Could not determine background color")

bin/dec_modes_query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Query mouse support
77
mode = term.DecPrivateMode(term.DecPrivateMode.MOUSE_REPORT_CLICK)
8-
response = term.get_dec_mode(mode, timeout=1)
8+
response = term.get_dec_mode(mode)
99

1010
print(f"Checking {mode.name} (mode {mode.value}) {mode.long_description}: ", end="")
1111

bin/detect-multibyte.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def get_pos(term):
4848
# Invalid variable name "Position"
4949
Position = collections.namedtuple('Position', ('row', 'column'))
5050

51-
pos = Position(*term.get_location(timeout=5.0))
51+
pos = Position(*term.get_location())
5252

5353
if -1 in pos:
5454
print('stdin: not a human', file=sys.stderr)

bin/display-modes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def display_dec_modes(term):
8989
# Query each mode
9090
for idx, (mode_name, mode_code) in enumerate(sorted(all_modes.items(), key=lambda x: x[1])):
9191
print(f' Testing {mode_name}...' + term.clear_eol, end='\r', flush=True)
92-
response = term.get_dec_mode(mode_code, timeout=0.1, force=force_mode)
92+
response = term.get_dec_mode(mode_code, force=force_mode)
9393
if response.supported:
9494
supported_modes[mode_name] = response
9595

bin/resize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def main():
6666
# to the farthest lower-right corner. By requesting the current
6767
# position, we may negotiate about the window size directly with the
6868
# terminal emulator connected at the distant end.
69-
pos = Position(*term.get_location(timeout=5.0))
69+
pos = Position(*term.get_location())
7070

7171
if -1 not in pos:
7272
# true size was determined

blessed/terminal.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ def location(self, x: Optional[int] = None, y: Optional[int]
721721
self.stream.write(self.restore)
722722
self.stream.flush()
723723

724-
def get_location(self, timeout: Optional[float] = None) -> Tuple[int, int]:
724+
def get_location(self, timeout: float = 1) -> Tuple[int, int]:
725725
r"""
726726
Return tuple (row, column) of cursor position.
727727
@@ -798,7 +798,7 @@ def get_location(self, timeout: Optional[float] = None) -> Tuple[int, int]:
798798
# rather than crowbarring such logic into an exception handler.
799799
return -1, -1
800800

801-
def get_fgcolor(self, timeout: Optional[float] = None) -> Tuple[int, int, int]:
801+
def get_fgcolor(self, timeout: float = 1) -> Tuple[int, int, int]:
802802
"""
803803
Return tuple (r, g, b) of foreground color.
804804
@@ -813,11 +813,13 @@ def get_fgcolor(self, timeout: Optional[float] = None) -> Tuple[int, int, int]:
813813
814814
The foreground color is determined by emitting an `OSC 10 color query
815815
<https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands>`_.
816+
817+
See also :meth:`~.Terminal.get_bgcolor` for querying the background color.
816818
"""
817819
match = self._query_response('\x1b]10;?\x07', RE_GET_FGCOLOR_RESPONSE, timeout)
818820
return tuple(int(val, 16) for val in match.groups()) if match else (-1, -1, -1)
819821

820-
def get_bgcolor(self, timeout: Optional[float] = None) -> Tuple[int, int, int]:
822+
def get_bgcolor(self, timeout: float = 1) -> Tuple[int, int, int]:
821823
"""
822824
Return tuple (r, g, b) of background color.
823825
@@ -832,6 +834,8 @@ def get_bgcolor(self, timeout: Optional[float] = None) -> Tuple[int, int, int]:
832834
833835
The background color is determined by emitting an `OSC 11 color query
834836
<https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands>`_.
837+
838+
See also :meth:`~.Terminal.get_fgcolor` for querying the foreground color.
835839
"""
836840
match = self._query_response('\x1b]11;?\x07', RE_GET_BGCOLOR_RESPONSE, timeout)
837841
return tuple(int(val, 16) for val in match.groups()) if match else (-1, -1, -1)
@@ -976,7 +980,7 @@ def does_sixel(self, timeout: Optional[float] = 1.0, force: bool = False) -> boo
976980
return da.supports_sixel if da is not None else False
977981

978982
def get_dec_mode(self, mode: Union[int, _DecPrivateMode],
979-
timeout: Optional[float] = None, force: bool = False) -> DecModeResponse:
983+
timeout: float = 1.0, force: bool = False) -> DecModeResponse:
980984
"""
981985
Query the state of a DEC Private Mode (DECRQM).
982986
@@ -1005,7 +1009,7 @@ def get_dec_mode(self, mode: Union[int, _DecPrivateMode],
10051009
re-inquiry unless ``force=True``. Although there are special cases
10061010
where a user may re-configure their terminal settings after the state
10071011
was requested by an application, the application is generally restarted
1008-
to recognize the new settings rather than to repeatidly re-inquire about
1012+
to recognize the new settings rather than to repeatedly re-inquire about
10091013
their latest value!
10101014
10111015
:arg mode: DEC Private Mode to query

docs/colors.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ color!
5151

5252
.. include:: all_the_colors.txt
5353

54+
Querying Terminal Colors
55+
-------------------------
56+
57+
You can query the terminal's current foreground and background colors using
58+
:meth:`~.Terminal.get_fgcolor` and :meth:`~.Terminal.get_bgcolor`. These methods return RGB tuples
59+
that represent the actual colors configured in the terminal emulator:
60+
61+
.. literalinclude:: ../bin/color_query.py
62+
:language: python
63+
64+
These methods are useful for adapting your application's color scheme to match the user's terminal
65+
theme, or for detecting whether the terminal has a light or dark background.
66+
5467
256 Colors
5568
----------
5669

docs/location.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ This uses a kind of "answer back" sequence that your terminal emulator responds
9090
terminal may not respond, or may take some time to respond, the :paramref:`~.get_location.timeout`
9191
keyword argument can be specified to return coordinates (-1, -1) after a blocking timeout:
9292

93-
>>> term.get_location(timeout=5)
93+
>>> term.get_location()
9494
(32, 0)
9595

9696
The return value of :meth:`~.Terminal.get_location` mirrors the arguments of

0 commit comments

Comments
 (0)