Skip to content

Commit

Permalink
[Unicode placeholders] Detect tmux by default and ensure passthrough
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-grechanik committed Dec 31, 2022
1 parent 963fe91 commit a51100c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
16 changes: 11 additions & 5 deletions kittens/icat/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
render_as_single_image, render_image
)
from ..tui.operations import clear_images_on_screen, raw_mode
from ..tui.utils import (running_in_tmux, make_sure_tmux_allows_passthrough)

OPTIONS = '''\
--align
Expand Down Expand Up @@ -147,9 +148,11 @@
--tmux
type=bool-set
Surround graphics commands with tmux escape sequences and also enable
--unicode-placeholder.
type=choices
choices=detect,yes,no
default=detect
Whether to surround graphics commands with tmux escape sequences and also enable
--unicode-placeholder. The default is to auto-detect tmux.
'''


Expand Down Expand Up @@ -636,8 +639,11 @@ def main(args: List[str] = sys.argv) -> None:
sys.stdin = open(os.ctermid())

parsed_opts = ParsedOpts()
parsed_opts.unicode_placeholder = cli_opts.unicode_placeholder or cli_opts.tmux
inside_tmux = cli_opts.tmux
parsed_opts.unicode_placeholder = cli_opts.unicode_placeholder
if cli_opts.tmux == 'yes' or (cli_opts.tmux == 'detect' and running_in_tmux()):
make_sure_tmux_allows_passthrough()
parsed_opts.unicode_placeholder = True
inside_tmux = True
if cli_opts.place:
try:
parsed_opts.place = parse_place(cli_opts.place)
Expand Down
8 changes: 4 additions & 4 deletions kittens/ssh/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
RESTORE_PRIVATE_MODE_VALUES, SAVE_PRIVATE_MODE_VALUES, Mode,
restore_colors, save_colors, set_mode
)
from ..tui.utils import kitty_opts, running_in_tmux
from ..tui.utils import (
kitty_opts, running_in_tmux, make_sure_tmux_allows_passthrough
)
from .config import init_config
from .copy import CopyInstruction
from .options.types import Options as SSHOptions
Expand Down Expand Up @@ -572,9 +574,7 @@ def dcs_to_kitty(payload: Union[bytes, str], type: str = 'ssh') -> bytes:
ans = b'\033P@kitty-' + type.encode('ascii') + b'|' + payload
tmux = running_in_tmux()
if tmux:
cp = subprocess.run([tmux, 'set', '-p', 'allow-passthrough', 'on'])
if cp.returncode != 0:
raise SystemExit(cp.returncode)
make_sure_tmux_allows_passthrough()
ans = b'\033Ptmux;\033' + ans + b'\033\033\\\033\\'
else:
ans += b'\033\\'
Expand Down
30 changes: 30 additions & 0 deletions kittens/tui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,33 @@ def running_in_tmux() -> str:
if exe.lower() == 'tmux':
return exe
return ''

def make_sure_tmux_allows_passthrough() -> None:
import subprocess
tmux = running_in_tmux()
if tmux:
# Check if the allow-passthrough option is set. `-p` means that it is a
# pane option, `-A` means that this option may be inherited from the parent.
# We use a timeout because `tmux show` may freeze when the version of the
# tmux executable is older than the version of the running tmux server.
try:
cp = subprocess.run([tmux, 'show', '-Ap', 'allow-passthrough'],
capture_output=True, timeout=2)
except subprocess.TimeoutExpired:
print("Tmux command timed out. This often happens when the version "
"of tmux on your PATH is older than the version of the running tmux server",
file=sys.stderr)
raise SystemExit(1)
if cp.returncode != 0:
print("Could not check if tmux allows passthrough, you may need to update tmux",
file=sys.stderr)
raise SystemExit(cp.returncode)
# Both `on` (allow passthrough if pane is visible) and `all` (allow
# passthrough even if it's invisible) are acceptable values.
if not (cp.stdout.endswith(b" on\n") or cp.stdout.endswith(b" all\n")):
# If the option is not set, set it to `on`.
cp = subprocess.run([tmux, 'set', '-p', 'allow-passthrough', 'on'])
if cp.returncode != 0:
print("Could not set the allow-passthrough option, you may need to update tmux",
file=sys.stderr)
raise SystemExit(cp.returncode)

0 comments on commit a51100c

Please sign in to comment.