Skip to content

Commit

Permalink
[lib,cli,tui] Added workaround for auto font ratio
Browse files Browse the repository at this point in the history
- Add: Implemented a workaround for auto font ratio on terminal emulators that swap the response to `CSI 14 t`.
  - This was noticed on older VTE-based terminal emulators (maybe on Gnome).
- Add: Added `.utils.SWAP_WIN_SIZE`.
- Add: Added `--swap_win_size` and `--no-swap_win_size` CL options.
- Add: Added `swap win size` config option.
- Change: Modified `.utils.get_window_size()`.
  • Loading branch information
AnonymouX47 committed Jun 26, 2022
1 parent 27aa2ee commit 4f9178f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [cli] `--query-timeout` command-line option ([3b658f3]).
- [cli] `--itc/--iterm2-compress`, `--itjq/--iterm2-jpeg-quality` and `--itnrff/iterm2-no-read-from-file` style-specific command-line options ([#55]).
- [cli] `-multi` command-line option.
- [cli] `--swap_win_size` and `--no-swap_win_size` command-line options.
- [tui] Concurrent/Parallel frame rendering for TUI animations ([#42]).
- [tui] Included key codes in the help menu.
- [cli,tui] `no multi`, `query timeout` and `style` config options.
- [cli,tui] `swap win size` config option.
- [cli,tui] Attempt to set window title at startup.
- [lib,cli,tui] Support for the Kitty terminal graphics protocol ([#39]).
- [lib,cli,tui] Automatic render style selection based on the detected terminal support ([#37]).
Expand Down
4 changes: 2 additions & 2 deletions docs/source/library/reference/utils.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.. automodule:: term_image.utils
:members: lock_tty, read_tty
:members: SWAP_WIN_SIZE, lock_tty, read_tty
:show-inheritance:


Expand Down Expand Up @@ -75,7 +75,7 @@
|
The ``term_image.utils`` modules provides the following public utilities.
The ``term_image.utils`` module provides the following public definitions.

.. attention::
Any other definition in this module should be considered part of the private
Expand Down
2 changes: 2 additions & 0 deletions term_image/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
set_font_ratio,
set_query_timeout,
tui,
utils,
)
from .config import config_options, store_config
from .exceptions import StyleError, TermImageError, URLNotFoundError
Expand Down Expand Up @@ -642,6 +643,7 @@ def check_arg(
setattr(args, var_name, getattr(config, var_name))

set_query_timeout(args.query_timeout)
utils.SWAP_WIN_SIZE = args.swap_win_size

if args.auto_font_ratio:
args.font_ratio = None
Expand Down
6 changes: 6 additions & 0 deletions term_image/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def update_config(config: Dict[str, Any], old_version: str) -> bool:
("['no multi']", NotImplemented, False),
("['query timeout']", NotImplemented, 0.1),
("['style']", NotImplemented, "auto"),
("['swap win size']", NotImplemented, False),
],
}

Expand Down Expand Up @@ -400,6 +401,7 @@ def update_context_nav_keys(
no_multi = _no_multi = False
query_timeout = _query_timeout = QUERY_TIMEOUT
style = _style = "auto"
swap_win_size = _swap_win_size = False

_nav = {
"Left": ["left", "\u25c0"],
Expand Down Expand Up @@ -555,4 +557,8 @@ def update_context_nav_keys(
lambda x: x in {"auto", "block", "iterm2", "kitty"},
"must be one of 'auto', 'block', 'iterm2', 'kitty'",
),
"swap win size": (
lambda x: isinstance(x, bool),
"must be a boolean",
),
}
18 changes: 18 additions & 0 deletions term_image/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@
help="Determine the font ratio from the terminal, if possible",
)

win_size_options = general.add_mutually_exclusive_group()
win_size_options.add_argument(
"--swap-win-size",
action="store_true",
default=config.swap_win_size,
help=(
"A workaround for 'auto font ratio' on some terminal emulators (e.g older "
"VTE-based ones) that wrongly report window dimensions swapped"
),
)
win_size_options.add_argument(
"--no-swap-win-size",
action="store_false",
default=config.swap_win_size,
dest="swap_win_size",
help="Unlike '--swap-win-size', use the reported window size as-is",
)

mode_options = general.add_mutually_exclusive_group()
mode_options.add_argument(
"--cli",
Expand Down
10 changes: 9 additions & 1 deletion term_image/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
else:
OS_IS_UNIX = True

#: A workaround for some terminal emulators (e.g older VTE-based ones) that wrongly
#: report window dimensions swapped.
#:
#: If ``True``, the dimensions reported by the terminal emulator are swapped.
#:
#: This setting affects AUTO font ratio calculation.
SWAP_WIN_SIZE: bool = False

# Decorator Classes


Expand Down Expand Up @@ -368,7 +376,7 @@ def get_window_size() -> Optional[Tuple[int, int]]:
if os.environ.get("SHELL", "").startswith("/data/data/com.termux/"):
size = (size[0], size[1] * 2)

return size and (0 in size and None or size)
return size[:: -SWAP_WIN_SIZE or 1] if size and 0 not in size else None


@unix_tty_only
Expand Down

0 comments on commit 4f9178f

Please sign in to comment.