Skip to content

Commit 05827d3

Browse files
committed
feat(cli): Basic project-based shell completion
1 parent 0cca489 commit 05827d3

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

tmuxp/cli/load.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import importlib
88
import logging
99
import os
10+
import pathlib
1011
import sys
12+
from typing import List
1113

1214
import click
1315
import kaptan
@@ -17,7 +19,7 @@
1719

1820
from .. import config, exc, log, util
1921
from ..workspacebuilder import WorkspaceBuilder
20-
from .utils import ConfigPath, _validate_choices, tmuxp_echo
22+
from .utils import ConfigPath, _validate_choices, get_config_dir, tmuxp_echo
2123

2224

2325
def set_layout_hook(session, hook_name):
@@ -438,9 +440,40 @@ def load_workspace(
438440
return _setup_plugins(builder)
439441

440442

443+
def config_shell_complete(ctx, params, incomplete):
444+
config_dir = pathlib.Path(get_config_dir())
445+
choices: List[pathlib.Path] = []
446+
447+
# CWD Paths
448+
choices += sorted(
449+
[
450+
pathlib.Path(os.path.relpath(p, pathlib.Path.cwd()))
451+
for p in [pathlib.Path.cwd(), *pathlib.Path.cwd().parents]
452+
if config.in_dir(str(p)) or len(list(p.glob(".tmuxp.*")))
453+
]
454+
)
455+
# CWD look one directory up
456+
choices += [
457+
pathlib.Path(f"./{os.path.relpath(p, pathlib.Path.cwd())}")
458+
for p in pathlib.Path.cwd().glob("*/.tmuxp.*")
459+
]
460+
461+
# Project configs
462+
choices += sorted([(config_dir / c).stem for c in config.in_dir(str(config_dir))])
463+
# choices += [config_dir / c for c in config.in_dir(str(config_dir))]
464+
# choices += ["~" / f.relative_to(pathlib.Path.home()) for f in choices]
465+
466+
return sorted([str(c) for c in choices if str(c).startswith(incomplete)])
467+
468+
441469
@click.command(name="load", short_help="Load tmuxp workspaces.")
442470
@click.pass_context
443-
@click.argument("config", type=ConfigPath(exists=True), nargs=-1)
471+
@click.argument(
472+
"config",
473+
type=ConfigPath(exists=True),
474+
nargs=-1,
475+
shell_complete=config_shell_complete,
476+
)
444477
@click.option("-S", "socket_path", help="pass-through for tmux -S")
445478
@click.option("-L", "socket_name", help="pass-through for tmux -L")
446479
@click.option("-f", "tmux_config_file", help="pass-through for tmux -f")

0 commit comments

Comments
 (0)