-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathshell.py
More file actions
257 lines (226 loc) · 6.84 KB
/
shell.py
File metadata and controls
257 lines (226 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
"""CLI for ``tmuxp shell`` subcommand."""
from __future__ import annotations
import argparse
import logging
import os
import pathlib
import typing as t
from libtmux.server import Server
from tmuxp import util
from tmuxp._compat import PY3, PYMINOR
from ._colors import Colors, build_description, get_color_mode
from .utils import tmuxp_echo
logger = logging.getLogger(__name__)
SHELL_DESCRIPTION = build_description(
"""
Launch interactive Python shell with tmux server, session, window and pane.
""",
(
(
None,
[
"tmuxp shell",
"tmuxp shell -L mysocket",
"tmuxp shell -c 'print(server.sessions)'",
"tmuxp shell --best",
],
),
),
)
if t.TYPE_CHECKING:
from typing import TypeAlias
CLIColorModeLiteral: TypeAlias = t.Literal["auto", "always", "never"]
CLIColorsLiteral: TypeAlias = t.Literal[56, 88]
CLIShellLiteral: TypeAlias = t.Literal[
"best",
"pdb",
"code",
"ptipython",
"ptpython",
"ipython",
"bpython",
]
class CLIShellNamespace(argparse.Namespace):
"""Typed :class:`argparse.Namespace` for tmuxp shell command."""
color: CLIColorModeLiteral
session_name: str
socket_name: str | None
socket_path: str | None
colors: CLIColorsLiteral | None
log_file: str | None
window_name: str | None
command: str | None
shell: CLIShellLiteral | None
use_pythonrc: bool
use_vi_mode: bool
def create_shell_subparser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
"""Augment :class:`argparse.ArgumentParser` with ``shell`` subcommand."""
parser.add_argument("session_name", metavar="session-name", nargs="?")
parser.add_argument("window_name", metavar="window-name", nargs="?")
parser.add_argument(
"-S",
dest="socket_path",
metavar="socket-path",
help="pass-through for tmux -S",
)
parser.add_argument(
"-L",
dest="socket_name",
metavar="socket-name",
help="pass-through for tmux -L",
)
parser.add_argument(
"-c",
dest="command",
help="instead of opening shell, execute python code in libtmux and exit",
)
shells = parser.add_mutually_exclusive_group()
shells.add_argument(
"--best",
dest="shell",
const="best",
action="store_const",
help="use best shell available in site packages",
default="best",
)
shells.add_argument(
"--pdb",
dest="shell",
const="pdb",
action="store_const",
help="use plain pdb",
)
shells.add_argument(
"--code",
dest="shell",
const="code",
action="store_const",
help="use stdlib's code.interact()",
)
shells.add_argument(
"--ptipython",
dest="shell",
const="ptipython",
action="store_const",
help="use ptpython + ipython",
)
shells.add_argument(
"--ptpython",
dest="shell",
const="ptpython",
action="store_const",
help="use ptpython",
)
shells.add_argument(
"--ipython",
dest="shell",
const="ipython",
action="store_const",
help="use ipython",
)
shells.add_argument(
"--bpython",
dest="shell",
const="bpython",
action="store_const",
help="use bpython",
)
parser.add_argument(
"--use-pythonrc",
dest="use_pythonrc",
action="store_true",
help="load PYTHONSTARTUP env var and ~/.pythonrc.py script in --code",
default=False,
)
parser.add_argument(
"--no-startup",
dest="use_pythonrc",
action="store_false",
help="load PYTHONSTARTUP env var and ~/.pythonrc.py script in --code",
default=False,
)
parser.add_argument(
"--use-vi-mode",
dest="use_vi_mode",
action="store_true",
help="use vi-mode in ptpython/ptipython",
default=False,
)
parser.add_argument(
"--no-vi-mode",
dest="use_vi_mode",
action="store_false",
help="use vi-mode in ptpython/ptipython",
default=False,
)
return parser
def command_shell(
args: CLIShellNamespace,
parser: argparse.ArgumentParser | None = None,
) -> None:
"""Entrypoint for ``tmuxp shell`` for tmux server, session, window and pane.
Priority given to loaded session/window/pane objects:
- session_name and window_name arguments
- current shell: envvar ``TMUX_PANE`` for determining window and session
- :attr:`libtmux.Server.attached_sessions`, :attr:`libtmux.Session.active_window`,
:attr:`libtmux.Window.active_pane`
"""
color_mode = get_color_mode(args.color)
cli_colors = Colors(color_mode)
# If inside a server, detect socket_path
env_tmux = os.getenv("TMUX")
if env_tmux is not None and isinstance(env_tmux, str):
env_socket_path = pathlib.Path(env_tmux.split(",")[0])
if (
env_socket_path.exists()
and args.socket_path is None
and args.socket_name is None
):
args.socket_path = str(env_socket_path)
server = Server(socket_name=args.socket_name, socket_path=args.socket_path)
server.raise_if_dead()
current_pane = util.get_current_pane(server=server)
session = util.get_session(
server=server,
session_name=args.session_name,
current_pane=current_pane,
)
window = util.get_window(
session=session,
window_name=args.window_name,
current_pane=current_pane,
)
pane = util.get_pane(window=window, current_pane=current_pane)
if args.command is not None:
exec(args.command)
elif args.shell == "pdb" or (
os.getenv("PYTHONBREAKPOINT") and PY3 and PYMINOR >= 7
):
from tmuxp._compat import breakpoint as tmuxp_breakpoint
tmuxp_echo(
cli_colors.muted("Launching ")
+ cli_colors.highlight("pdb", bold=False)
+ cli_colors.muted(" shell..."),
)
tmuxp_breakpoint()
return
else:
from tmuxp.shell import launch
shell_name = args.shell or "best"
tmuxp_echo(
cli_colors.muted("Launching ")
+ cli_colors.highlight(shell_name, bold=False)
+ cli_colors.muted(" shell for session ")
+ cli_colors.info(session.name or "")
+ cli_colors.muted("..."),
)
launch(
shell=args.shell,
use_pythonrc=args.use_pythonrc, # shell: code
use_vi_mode=args.use_vi_mode, # shell: ptpython, ptipython
# tmux environment / libtmux variables
server=server,
session=session,
window=window,
pane=pane,
)