-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy path__init__.py
More file actions
377 lines (342 loc) · 10.3 KB
/
Copy path__init__.py
File metadata and controls
377 lines (342 loc) · 10.3 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""CLI utilities for tmuxp."""
from __future__ import annotations
import argparse
import logging
import os
import sys
import typing as t
from libtmux.__about__ import __version__ as libtmux_version
from libtmux.common import has_minimum_version
from libtmux.exc import TmuxCommandNotFound
from tmuxp import exc
from tmuxp.__about__ import __version__
from tmuxp.log import setup_logger
from ._colors import build_description
from ._formatter import TmuxpHelpFormatter, create_themed_formatter
from .convert import CONVERT_DESCRIPTION, command_convert, create_convert_subparser
from .debug_info import (
DEBUG_INFO_DESCRIPTION,
CLIDebugInfoNamespace,
command_debug_info,
create_debug_info_subparser,
)
from .edit import EDIT_DESCRIPTION, command_edit, create_edit_subparser
from .freeze import (
FREEZE_DESCRIPTION,
CLIFreezeNamespace,
command_freeze,
create_freeze_subparser,
)
from .import_config import (
IMPORT_DESCRIPTION,
command_import_teamocil,
command_import_tmuxinator,
create_import_subparser,
)
from .load import (
LOAD_DESCRIPTION,
CLILoadNamespace,
command_load,
create_load_subparser,
)
from .ls import LS_DESCRIPTION, CLILsNamespace, command_ls, create_ls_subparser
from .search import (
SEARCH_DESCRIPTION,
CLISearchNamespace,
command_search,
create_search_subparser,
)
from .shell import (
SHELL_DESCRIPTION,
CLIShellNamespace,
command_shell,
create_shell_subparser,
)
from .utils import tmuxp_echo
logger = logging.getLogger(__name__)
CLI_DESCRIPTION = build_description(
"""
tmuxp - tmux session manager.
Manage and launch tmux sessions from YAML/JSON workspace files.
""",
(
(
"load",
[
"tmuxp load myproject",
"tmuxp load ./workspace.yaml",
"tmuxp load -d myproject",
"tmuxp load -y dev staging",
],
),
(
"freeze",
[
"tmuxp freeze mysession",
"tmuxp freeze mysession -o session.yaml",
],
),
(
"ls",
[
"tmuxp ls",
"tmuxp ls --tree",
"tmuxp ls --full",
"tmuxp ls --json",
],
),
(
"search",
[
"tmuxp search dev",
"tmuxp search name:myproject",
"tmuxp search -i DEV",
"tmuxp search --json dev",
],
),
(
"shell",
[
"tmuxp shell",
"tmuxp shell -L mysocket",
"tmuxp shell -c 'print(server.sessions)'",
],
),
(
"convert",
[
"tmuxp convert workspace.yaml",
"tmuxp convert workspace.json",
],
),
(
"import",
[
"tmuxp import teamocil ~/.teamocil/project.yml",
"tmuxp import tmuxinator ~/.tmuxinator/project.yml",
],
),
(
"edit",
[
"tmuxp edit myproject",
],
),
(
"debug-info",
[
"tmuxp debug-info",
"tmuxp debug-info --json",
],
),
),
)
if t.TYPE_CHECKING:
import pathlib
from typing import TypeAlias
CLIVerbosity: TypeAlias = t.Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
CLIColorMode: TypeAlias = t.Literal["auto", "always", "never"]
CLISubparserName: TypeAlias = t.Literal[
"ls",
"load",
"freeze",
"convert",
"edit",
"import",
"search",
"shell",
"debug-info",
]
CLIImportSubparserName: TypeAlias = t.Literal["teamocil", "tmuxinator"]
def create_parser() -> argparse.ArgumentParser:
"""Create CLI :class:`argparse.ArgumentParser` for tmuxp."""
# Use factory to create themed formatter with auto-detected color mode
# This respects NO_COLOR, FORCE_COLOR env vars and TTY detection
formatter_class = create_themed_formatter()
parser = argparse.ArgumentParser(
prog="tmuxp",
description=CLI_DESCRIPTION,
formatter_class=formatter_class,
)
parser.add_argument(
"--version",
"-V",
action="version",
version=f"%(prog)s {__version__}, libtmux {libtmux_version}",
)
parser.add_argument(
"--log-level",
action="store",
metavar="log-level",
default="warning",
choices=["debug", "info", "warning", "error", "critical"],
help='log level (debug, info, warning, error, critical) (default "warning")',
)
parser.add_argument(
"--color",
choices=["auto", "always", "never"],
default="auto",
help="when to use colors: auto (default), always, or never",
)
subparsers = parser.add_subparsers(dest="subparser_name")
load_parser = subparsers.add_parser(
"load",
help="load tmuxp workspaces",
description=LOAD_DESCRIPTION,
formatter_class=formatter_class,
)
create_load_subparser(load_parser)
shell_parser = subparsers.add_parser(
"shell",
help="launch python shell for tmux server, session, window and pane",
description=SHELL_DESCRIPTION,
formatter_class=formatter_class,
)
create_shell_subparser(shell_parser)
import_parser = subparsers.add_parser(
"import",
help="import workspaces from teamocil and tmuxinator.",
description=IMPORT_DESCRIPTION,
formatter_class=formatter_class,
)
create_import_subparser(import_parser)
convert_parser = subparsers.add_parser(
"convert",
help="convert workspace files between yaml and json.",
description=CONVERT_DESCRIPTION,
formatter_class=formatter_class,
)
create_convert_subparser(convert_parser)
debug_info_parser = subparsers.add_parser(
"debug-info",
help="print out all diagnostic info",
description=DEBUG_INFO_DESCRIPTION,
formatter_class=formatter_class,
)
create_debug_info_subparser(debug_info_parser)
ls_parser = subparsers.add_parser(
"ls",
help="list workspaces in tmuxp directory",
description=LS_DESCRIPTION,
formatter_class=formatter_class,
)
create_ls_subparser(ls_parser)
search_parser = subparsers.add_parser(
"search",
help="search workspace files by name, session, path, or content",
description=SEARCH_DESCRIPTION,
formatter_class=formatter_class,
)
create_search_subparser(search_parser)
edit_parser = subparsers.add_parser(
"edit",
help="run $EDITOR on workspace file",
description=EDIT_DESCRIPTION,
formatter_class=formatter_class,
)
create_edit_subparser(edit_parser)
freeze_parser = subparsers.add_parser(
"freeze",
help="freeze a live tmux session to a tmuxp workspace file",
description=FREEZE_DESCRIPTION,
formatter_class=formatter_class,
)
create_freeze_subparser(freeze_parser)
return parser
class CLINamespace(argparse.Namespace):
"""Typed :class:`argparse.Namespace` for tmuxp root-level CLI."""
log_level: CLIVerbosity
color: CLIColorMode
subparser_name: CLISubparserName
import_subparser_name: CLIImportSubparserName | None
version: bool
ns = CLINamespace()
def cli(_args: list[str] | None = None) -> None:
"""Manage tmux sessions.
Pass the "--help" argument to any command to see detailed help.
See detailed documentation and examples at:
http://tmuxp.git-pull.com/
"""
try:
has_minimum_version()
except TmuxCommandNotFound:
tmuxp_echo("tmux not found. tmuxp requires you install tmux first.")
sys.exit()
except exc.TmuxpException as e:
tmuxp_echo(str(e))
sys.exit()
parser = create_parser()
args = parser.parse_args(_args, namespace=ns)
setup_logger(level=args.log_level.upper())
if args.subparser_name is None:
parser.print_help()
return
if args.subparser_name == "load":
command_load(
args=CLILoadNamespace(**vars(args)),
parser=parser,
)
elif args.subparser_name == "shell":
command_shell(
args=CLIShellNamespace(**vars(args)),
parser=parser,
)
elif args.subparser_name == "import":
import_subparser_name = getattr(args, "import_subparser_name", None)
if import_subparser_name is None:
parser.print_help()
return
if import_subparser_name == "teamocil":
command_import_teamocil(
workspace_file=args.workspace_file,
parser=parser,
color=args.color,
)
elif import_subparser_name == "tmuxinator":
command_import_tmuxinator(
workspace_file=args.workspace_file,
parser=parser,
color=args.color,
)
elif args.subparser_name == "convert":
command_convert(
workspace_file=args.workspace_file,
answer_yes=args.answer_yes,
parser=parser,
color=args.color,
)
elif args.subparser_name == "debug-info":
command_debug_info(
args=CLIDebugInfoNamespace(**vars(args)),
parser=parser,
)
elif args.subparser_name == "edit":
command_edit(
workspace_file=args.workspace_file,
parser=parser,
color=args.color,
)
elif args.subparser_name == "freeze":
command_freeze(
args=CLIFreezeNamespace(**vars(args)),
parser=parser,
)
elif args.subparser_name == "ls":
command_ls(
args=CLILsNamespace(**vars(args)),
parser=parser,
)
elif args.subparser_name == "search":
command_search(
args=CLISearchNamespace(**vars(args)),
parser=parser,
)
def startup(config_dir: pathlib.Path) -> None:
"""
Initialize CLI.
Parameters
----------
str : get_workspace_dir(): Config directory to search
"""
if not os.path.exists(config_dir):
os.makedirs(config_dir)