Skip to content

Commit 663fcaa

Browse files
committed
Split out shell, launch into shell by default unless BREAKPOINT entered
1 parent 63bc260 commit 663fcaa

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

tmuxp/cli.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from . import config, exc, log, util
2323
from .__about__ import __version__
24-
from ._compat import string_types
24+
from ._compat import PY3, PYMINOR, string_types
2525
from .workspacebuilder import WorkspaceBuilder, freeze
2626

2727
logger = logging.getLogger(__name__)
@@ -671,7 +671,15 @@ def startup(config_dir):
671671
'command',
672672
help='Instead of opening shell, execute python code in libtmux and exit',
673673
)
674-
def command_shell(session_name, window_name, socket_name, socket_path, command):
674+
@click.option(
675+
'--use-pdb/--no-pdb',
676+
'use_pdb',
677+
help='Use pdb / breakpoint() instead of code.interact()',
678+
default=False,
679+
)
680+
def command_shell(
681+
session_name, window_name, socket_name, socket_path, command, use_pdb
682+
):
675683
"""Launch python shell for tmux server, session, window and pane.
676684
677685
Priority given to loaded session/wndow/pane objects:
@@ -699,9 +707,15 @@ def command_shell(session_name, window_name, socket_name, socket_path, command):
699707
if command is not None:
700708
exec(command)
701709
else:
702-
from ._compat import breakpoint as tmuxp_breakpoint
710+
if use_pdb or (os.getenv('PYTHONBREAKPOINT') and PY3 and PYMINOR >= 7):
711+
from ._compat import breakpoint as tmuxp_breakpoint
712+
713+
tmuxp_breakpoint()
714+
return
715+
else:
716+
from .shell import launch
703717

704-
tmuxp_breakpoint()
718+
launch(server=server, session=session, window=window, pane=pane)
705719

706720

707721
@cli.command(name='shell_plus')

tmuxp/shell.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# -*- coding: utf-8 -*-
2+
"""Utility and helper methods for tmuxp.
3+
4+
tmuxp.shell
5+
~~~~~~~~~~~
6+
7+
"""
8+
from __future__ import absolute_import, unicode_literals
9+
10+
import logging
11+
import os
12+
13+
logger = logging.getLogger(__name__)
14+
15+
16+
def launch(shell=None, best=True, use_pythonrc=False, **kwargs):
17+
import code
18+
19+
import libtmux
20+
21+
imported_objects = {
22+
'libtmux': libtmux,
23+
'Server': libtmux.Server,
24+
'Session': libtmux.Session,
25+
'Window': libtmux.Window,
26+
'Pane': libtmux.Pane,
27+
'server': kwargs.get('server'),
28+
'session': kwargs.get('session'),
29+
'window': kwargs.get('window'),
30+
'pane': kwargs.get('pane'),
31+
}
32+
33+
try:
34+
# Try activating rlcompleter, because it's handy.
35+
import readline
36+
except ImportError:
37+
pass
38+
else:
39+
# We don't have to wrap the following import in a 'try', because
40+
# we already know 'readline' was imported successfully.
41+
import rlcompleter
42+
43+
readline.set_completer(rlcompleter.Completer(imported_objects).complete)
44+
# Enable tab completion on systems using libedit (e.g. macOS).
45+
# These lines are copied from Lib/site.py on Python 3.4.
46+
readline_doc = getattr(readline, '__doc__', '')
47+
if readline_doc is not None and 'libedit' in readline_doc:
48+
readline.parse_and_bind("bind ^I rl_complete")
49+
else:
50+
readline.parse_and_bind("tab:complete")
51+
52+
# We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
53+
# conventions and get $PYTHONSTARTUP first then .pythonrc.py.
54+
if use_pythonrc:
55+
for pythonrc in set(
56+
[os.environ.get("PYTHONSTARTUP"), os.path.expanduser('~/.pythonrc.py')]
57+
):
58+
if not pythonrc:
59+
continue
60+
if not os.path.isfile(pythonrc):
61+
continue
62+
with open(pythonrc) as handle:
63+
pythonrc_code = handle.read()
64+
# Match the behavior of the cpython shell where an error in
65+
# PYTHONSTARTUP prints an exception and continues.
66+
try:
67+
exec(compile(pythonrc_code, pythonrc, 'exec'), imported_objects)
68+
except Exception:
69+
import traceback
70+
71+
traceback.print_exc()
72+
73+
code.interact(local=imported_objects)

0 commit comments

Comments
 (0)