-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathls.py
More file actions
646 lines (558 loc) · 19.2 KB
/
ls.py
File metadata and controls
646 lines (558 loc) · 19.2 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
"""CLI for ``tmuxp ls`` subcommand.
List and display workspace configuration files.
Examples
--------
>>> from tmuxp.cli.ls import WorkspaceInfo
Create workspace info from file path:
>>> import pathlib
>>> ws = WorkspaceInfo(
... name="dev",
... path="~/.tmuxp/dev.yaml",
... format="yaml",
... size=256,
... mtime="2024-01-15T10:30:00",
... session_name="development",
... source="global",
... )
>>> ws["name"]
'dev'
>>> ws["source"]
'global'
"""
from __future__ import annotations
import argparse
import datetime
import json
import logging
import pathlib
import typing as t
import yaml
from tmuxp._internal.config_reader import ConfigReader
from tmuxp._internal.private_path import PrivatePath
from tmuxp.workspace.constants import VALID_WORKSPACE_DIR_FILE_EXTENSIONS
from tmuxp.workspace.finders import (
find_local_workspace_files,
get_workspace_dir,
get_workspace_dir_candidates,
)
from ._colors import Colors, build_description, get_color_mode
from ._output import OutputFormatter, OutputMode, get_output_mode
logger = logging.getLogger(__name__)
LS_DESCRIPTION = build_description(
"""
List workspace files in the tmuxp configuration directory.
""",
(
(
None,
[
"tmuxp ls",
"tmuxp ls --tree",
"tmuxp ls --full",
],
),
(
"Machine-readable output examples",
[
"tmuxp ls --json",
"tmuxp ls --json --full",
"tmuxp ls --ndjson",
"tmuxp ls --json | jq '.workspaces[].name'",
],
),
),
)
if t.TYPE_CHECKING:
from typing import TypeAlias
CLIColorModeLiteral: TypeAlias = t.Literal["auto", "always", "never"]
class WorkspaceInfo(t.TypedDict):
"""Workspace file information for JSON output.
Attributes
----------
name : str
Workspace name (file stem without extension).
path : str
Path to workspace file (with ~ contraction).
format : str
File format (yaml or json).
size : int
File size in bytes.
mtime : str
Modification time in ISO format.
session_name : str | None
Session name from config if parseable.
source : str
Source location: "local" (cwd/parents) or "global" (~/.tmuxp/).
"""
name: str
path: str
format: str
size: int
mtime: str
session_name: str | None
source: str
class CLILsNamespace(argparse.Namespace):
"""Typed :class:`argparse.Namespace` for tmuxp ls command.
Examples
--------
>>> ns = CLILsNamespace()
>>> ns.color = "auto"
>>> ns.color
'auto'
"""
color: CLIColorModeLiteral
tree: bool
output_json: bool
output_ndjson: bool
full: bool
def create_ls_subparser(
parser: argparse.ArgumentParser,
) -> argparse.ArgumentParser:
"""Augment :class:`argparse.ArgumentParser` with ``ls`` subcommand.
Parameters
----------
parser : argparse.ArgumentParser
The parser to augment.
Returns
-------
argparse.ArgumentParser
The augmented parser.
Examples
--------
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> result = create_ls_subparser(parser)
>>> result is parser
True
"""
parser.add_argument(
"--tree",
action="store_true",
help="display workspaces grouped by directory",
)
parser.add_argument(
"--json",
action="store_true",
dest="output_json",
help="output as JSON",
)
parser.add_argument(
"--ndjson",
action="store_true",
dest="output_ndjson",
help="output as NDJSON (one JSON per line)",
)
parser.add_argument(
"--full",
action="store_true",
help="include full config content in output",
)
return parser
def _get_workspace_info(
filepath: pathlib.Path,
*,
source: str = "global",
include_config: bool = False,
) -> dict[str, t.Any]:
"""Extract metadata from a workspace file.
Parameters
----------
filepath : pathlib.Path
Path to the workspace file.
source : str
Source location: "local" or "global". Default "global".
include_config : bool
If True, include full parsed config content. Default False.
Returns
-------
dict[str, Any]
Workspace metadata dictionary. Includes 'config' key when include_config=True.
Examples
--------
>>> content = "session_name: test-session" + chr(10) + "windows: []"
>>> yaml_file = tmp_path / "test.yaml"
>>> _ = yaml_file.write_text(content)
>>> info = _get_workspace_info(yaml_file)
>>> info['session_name']
'test-session'
>>> info['format']
'yaml'
>>> info['source']
'global'
>>> info_local = _get_workspace_info(yaml_file, source="local")
>>> info_local['source']
'local'
>>> info_full = _get_workspace_info(yaml_file, include_config=True)
>>> 'config' in info_full
True
>>> info_full['config']['session_name']
'test-session'
"""
stat = filepath.stat()
ext = filepath.suffix.lower()
file_format = "json" if ext == ".json" else "yaml"
# Try to extract session_name and optionally full config
session_name: str | None = None
config_content: dict[str, t.Any] | None = None
try:
config = ConfigReader.from_file(filepath)
if isinstance(config.content, dict):
session_name = config.content.get("session_name")
if include_config:
config_content = config.content
except (yaml.YAMLError, json.JSONDecodeError, OSError):
# If we can't parse it, just skip session_name
pass
result: dict[str, t.Any] = {
"name": filepath.stem,
"path": str(PrivatePath(filepath)),
"format": file_format,
"size": stat.st_size,
"mtime": datetime.datetime.fromtimestamp(
stat.st_mtime,
tz=datetime.timezone.utc,
).isoformat(),
"session_name": session_name,
"source": source,
}
if include_config:
result["config"] = config_content
return result
def _render_config_tree(config: dict[str, t.Any], colors: Colors) -> list[str]:
"""Render config windows/panes as tree lines for human output.
Parameters
----------
config : dict[str, Any]
Parsed config content.
colors : Colors
Color manager.
Returns
-------
list[str]
Lines of formatted tree output.
Examples
--------
>>> from tmuxp.cli._colors import ColorMode, Colors
>>> colors = Colors(ColorMode.NEVER)
>>> config = {
... "session_name": "dev",
... "windows": [
... {"window_name": "editor", "layout": "main-horizontal"},
... {"window_name": "shell"},
... ],
... }
>>> lines = _render_config_tree(config, colors)
>>> "editor" in lines[0]
True
>>> "shell" in lines[1]
True
"""
lines: list[str] = []
windows = config.get("windows", [])
for i, window in enumerate(windows):
if not isinstance(window, dict):
continue
is_last_window = i == len(windows) - 1
prefix = "└── " if is_last_window else "├── "
child_prefix = " " if is_last_window else "│ "
# Window line
window_name = window.get("window_name", f"window {i}")
layout = window.get("layout", "")
layout_info = f" [{layout}]" if layout else ""
lines.append(f"{prefix}{colors.info(window_name)}{colors.muted(layout_info)}")
# Panes
panes = window.get("panes", [])
for j, pane in enumerate(panes):
is_last_pane = j == len(panes) - 1
pane_prefix = "└── " if is_last_pane else "├── "
# Get pane command summary
if isinstance(pane, dict):
cmds = pane.get("shell_command", [])
if isinstance(cmds, str):
cmd_str = cmds
elif isinstance(cmds, list) and cmds:
cmd_str = str(cmds[0])
else:
cmd_str = ""
elif isinstance(pane, str):
cmd_str = pane
else:
cmd_str = ""
# Truncate long commands
if len(cmd_str) > 40:
cmd_str = cmd_str[:37] + "..."
pane_info = f": {cmd_str}" if cmd_str else ""
lines.append(
f"{child_prefix}{pane_prefix}{colors.muted(f'pane {j}')}{pane_info}"
)
return lines
def _render_global_workspace_dirs(
formatter: OutputFormatter,
colors: Colors,
global_dir_candidates: list[dict[str, t.Any]],
) -> None:
"""Render global workspace directories section.
Parameters
----------
formatter : OutputFormatter
Output formatter.
colors : Colors
Color manager.
global_dir_candidates : list[dict[str, Any]]
List of global workspace directory candidates with metadata.
Examples
--------
>>> from tmuxp.cli._output import OutputFormatter, OutputMode
>>> from tmuxp.cli._colors import Colors, ColorMode
>>> formatter = OutputFormatter(OutputMode.HUMAN)
>>> colors = Colors(ColorMode.NEVER)
>>> candidates = [
... {"path": "~/.tmuxp", "source": "Legacy", "exists": True,
... "workspace_count": 5, "active": True},
... {"path": "~/.config/tmuxp", "source": "XDG", "exists": False,
... "workspace_count": 0, "active": False},
... ]
>>> _render_global_workspace_dirs(formatter, colors, candidates)
<BLANKLINE>
Global workspace directories:
Legacy: ~/.tmuxp (5 workspaces, active)
XDG: ~/.config/tmuxp (not found)
"""
formatter.emit_text("")
formatter.emit_text(colors.heading("Global workspace directories:"))
for candidate in global_dir_candidates:
path = candidate["path"]
source = candidate.get("source", "")
source_prefix = f"{source}: " if source else ""
if candidate["exists"]:
count = candidate["workspace_count"]
status = f"{count} workspace{'s' if count != 1 else ''}"
if candidate["active"]:
status += ", active"
formatter.emit_text(
f" {colors.muted(source_prefix)}{colors.info(path)} "
f"({colors.success(status)})"
)
else:
formatter.emit_text(
f" {colors.muted(source_prefix)}{colors.info(path)} ({status})"
)
else:
formatter.emit_text(
f" {colors.muted(source_prefix)}{colors.info(path)} "
f"({colors.muted('not found')})"
)
def _output_flat(
workspaces: list[dict[str, t.Any]],
formatter: OutputFormatter,
colors: Colors,
*,
full: bool = False,
global_dir_candidates: list[dict[str, t.Any]] | None = None,
) -> None:
"""Output workspaces in flat list format.
Groups workspaces by source (local vs global) for human output.
Parameters
----------
workspaces : list[dict[str, Any]]
Workspaces to display.
formatter : OutputFormatter
Output formatter.
colors : Colors
Color manager.
full : bool
If True, show full config details in tree format. Default False.
global_dir_candidates : list[dict[str, Any]] | None
List of global workspace directory candidates with metadata.
Examples
--------
>>> from tmuxp.cli._output import OutputFormatter, OutputMode
>>> from tmuxp.cli._colors import Colors, ColorMode
>>> formatter = OutputFormatter(OutputMode.HUMAN)
>>> colors = Colors(ColorMode.NEVER)
>>> workspaces = [{"name": "dev", "path": "~/.tmuxp/dev.yaml", "source": "global"}]
>>> _output_flat(workspaces, formatter, colors)
Global workspaces:
dev
"""
# Separate by source for human output grouping
local_workspaces = [ws for ws in workspaces if ws["source"] == "local"]
global_workspaces = [ws for ws in workspaces if ws["source"] == "global"]
def output_workspace(ws: dict[str, t.Any], show_path: bool) -> None:
"""Output a single workspace."""
formatter.emit(ws)
path_info = f" {colors.info(ws['path'])}" if show_path else ""
formatter.emit_text(f" {colors.highlight(ws['name'])}{path_info}")
# With --full, show config tree
if full and ws.get("config"):
for line in _render_config_tree(ws["config"], colors):
formatter.emit_text(f" {line}")
# Output local workspaces first (closest to user's context)
if local_workspaces:
formatter.emit_text(colors.heading("Local workspaces:"))
for ws in local_workspaces:
output_workspace(ws, show_path=True)
# Output global workspaces with active directory in header
if global_workspaces:
if local_workspaces:
formatter.emit_text("") # Blank line separator
# Find active directory for header
active_dir = ""
if global_dir_candidates:
for candidate in global_dir_candidates:
if candidate["active"]:
active_dir = candidate["path"]
break
if active_dir:
formatter.emit_text(colors.heading(f"Global workspaces ({active_dir}):"))
else:
formatter.emit_text(colors.heading("Global workspaces:"))
for ws in global_workspaces:
output_workspace(ws, show_path=False)
# Output global workspace directories section
if global_dir_candidates:
_render_global_workspace_dirs(formatter, colors, global_dir_candidates)
def _output_tree(
workspaces: list[dict[str, t.Any]],
formatter: OutputFormatter,
colors: Colors,
*,
full: bool = False,
global_dir_candidates: list[dict[str, t.Any]] | None = None,
) -> None:
"""Output workspaces grouped by directory (tree view).
Parameters
----------
workspaces : list[dict[str, Any]]
Workspaces to display.
formatter : OutputFormatter
Output formatter.
colors : Colors
Color manager.
full : bool
If True, show full config details in tree format. Default False.
global_dir_candidates : list[dict[str, Any]] | None
List of global workspace directory candidates with metadata.
Examples
--------
>>> from tmuxp.cli._output import OutputFormatter, OutputMode
>>> from tmuxp.cli._colors import Colors, ColorMode
>>> formatter = OutputFormatter(OutputMode.HUMAN)
>>> colors = Colors(ColorMode.NEVER)
>>> workspaces = [{"name": "dev", "path": "~/.tmuxp/dev.yaml", "source": "global"}]
>>> _output_tree(workspaces, formatter, colors)
<BLANKLINE>
~/.tmuxp
dev
"""
# Group by parent directory
by_directory: dict[str, list[dict[str, t.Any]]] = {}
for ws in workspaces:
# Extract parent directory from path
parent = str(pathlib.Path(ws["path"]).parent)
by_directory.setdefault(parent, []).append(ws)
# Output grouped
for directory in sorted(by_directory.keys()):
dir_workspaces = by_directory[directory]
# Human output: directory header
formatter.emit_text(f"\n{colors.highlight(directory)}")
for ws in dir_workspaces:
# JSON/NDJSON output
formatter.emit(ws)
# Human output: indented workspace name
ws_name = ws["name"]
ws_session = ws.get("session_name")
session_info = ""
if ws_session and ws_session != ws_name:
session_info = f" {colors.muted(f'→ {ws_session}')}"
formatter.emit_text(f" {colors.highlight(ws_name)}{session_info}")
# With --full, show config tree
if full and ws.get("config"):
for line in _render_config_tree(ws["config"], colors):
formatter.emit_text(f" {line}")
# Output global workspace directories section
if global_dir_candidates:
_render_global_workspace_dirs(formatter, colors, global_dir_candidates)
def command_ls(
args: CLILsNamespace | None = None,
parser: argparse.ArgumentParser | None = None,
) -> None:
"""Entrypoint for ``tmuxp ls`` subcommand.
Lists both local workspaces (from cwd and parent directories) and
global workspaces (from ~/.tmuxp/).
Parameters
----------
args : CLILsNamespace | None
Parsed command-line arguments.
parser : argparse.ArgumentParser | None
The argument parser (unused but required by CLI interface).
Examples
--------
>>> # command_ls() lists workspaces from cwd/parents and ~/.tmuxp/
"""
# Get color mode from args or default to AUTO
color_mode = get_color_mode(args.color if args else None)
colors = Colors(color_mode)
# Determine output mode and options
output_json = args.output_json if args else False
output_ndjson = args.output_ndjson if args else False
tree = args.tree if args else False
full = args.full if args else False
output_mode = get_output_mode(output_json, output_ndjson)
formatter = OutputFormatter(output_mode)
# Get global workspace directory candidates
global_dir_candidates = get_workspace_dir_candidates()
# 1. Collect local workspace files (cwd and parents)
local_files = find_local_workspace_files()
workspaces: list[dict[str, t.Any]] = [
_get_workspace_info(f, source="local", include_config=full) for f in local_files
]
# 2. Collect global workspace files (~/.tmuxp/)
tmuxp_dir = pathlib.Path(get_workspace_dir())
if tmuxp_dir.exists() and tmuxp_dir.is_dir():
workspaces.extend(
_get_workspace_info(f, source="global", include_config=full)
for f in sorted(tmuxp_dir.iterdir())
if not f.is_dir()
and f.suffix.lower() in VALID_WORKSPACE_DIR_FILE_EXTENSIONS
)
if not workspaces:
formatter.emit_text(colors.warning("No workspaces found."))
# Still show global workspace directories even with no workspaces
if output_mode == OutputMode.HUMAN:
_render_global_workspace_dirs(formatter, colors, global_dir_candidates)
elif output_mode == OutputMode.JSON:
# Output structured JSON with empty workspaces
output_data = {
"workspaces": [],
"global_workspace_dirs": global_dir_candidates,
}
formatter.emit_object(output_data)
# NDJSON: just output nothing for empty workspaces
return
# JSON mode: output structured object instead of using formatter
if output_mode == OutputMode.JSON:
output_data = {
"workspaces": workspaces,
"global_workspace_dirs": global_dir_candidates,
}
formatter.emit_object(output_data)
return
# Human and NDJSON output
if tree:
_output_tree(
workspaces,
formatter,
colors,
full=full,
global_dir_candidates=global_dir_candidates,
)
else:
_output_flat(
workspaces,
formatter,
colors,
full=full,
global_dir_candidates=global_dir_candidates,
)
formatter.finalize()