|
| 1 | +import os |
| 2 | + |
| 3 | +from libtmux.server import Server |
| 4 | +from tmuxp import config |
| 5 | +from tmuxp.cli.utils import get_config_dir |
| 6 | + |
| 7 | +config_dir = get_config_dir() |
| 8 | + |
| 9 | +try: |
| 10 | + import argcomplete |
| 11 | +except ImportError: |
| 12 | + |
| 13 | + class ArgComplete: |
| 14 | + class Completers: |
| 15 | + class Completer: |
| 16 | + def __call__(self, *args: object, **kwargs: object) -> object: |
| 17 | + ... |
| 18 | + |
| 19 | + FilesCompleter = Completer |
| 20 | + |
| 21 | + completers = Completers() |
| 22 | + |
| 23 | + argcomplete = ArgComplete() # type:ignore |
| 24 | + |
| 25 | + |
| 26 | +class ConfigFileCompleter(argcomplete.completers.FilesCompleter): |
| 27 | + |
| 28 | + """argcomplete completer for tmuxp files.""" |
| 29 | + |
| 30 | + def __call__(self, prefix, **kwargs): |
| 31 | + |
| 32 | + completion = argcomplete.completers.FilesCompleter.__call__( |
| 33 | + self, prefix, **kwargs |
| 34 | + ) |
| 35 | + |
| 36 | + completion += [os.path.join(config_dir, c) for c in config.in_dir(config_dir)] |
| 37 | + |
| 38 | + return completion |
| 39 | + |
| 40 | + |
| 41 | +class TmuxinatorCompleter(argcomplete.completers.FilesCompleter): |
| 42 | + |
| 43 | + """argcomplete completer for Tmuxinator files.""" |
| 44 | + |
| 45 | + def __call__(self, prefix, **kwargs): |
| 46 | + from tmuxp.cli.import_config import get_tmuxinator_dir |
| 47 | + |
| 48 | + tmuxinator_config_dir = get_tmuxinator_dir() |
| 49 | + completion = argcomplete.completers.FilesCompleter.__call__( |
| 50 | + self, prefix, **kwargs |
| 51 | + ) |
| 52 | + tmuxinator_configs = config.in_dir(tmuxinator_config_dir, extensions="yml") |
| 53 | + completion += [ |
| 54 | + os.path.join(tmuxinator_config_dir, f) for f in tmuxinator_configs |
| 55 | + ] |
| 56 | + |
| 57 | + return completion |
| 58 | + |
| 59 | + |
| 60 | +class TeamocilCompleter(argcomplete.completers.FilesCompleter): |
| 61 | + |
| 62 | + """argcomplete completer for Teamocil files.""" |
| 63 | + |
| 64 | + def __call__(self, prefix, **kwargs): |
| 65 | + from tmuxp.cli.import_config import get_teamocil_dir |
| 66 | + |
| 67 | + teamocil_config_dir = get_teamocil_dir() |
| 68 | + |
| 69 | + completion = argcomplete.completers.FilesCompleter.__call__( |
| 70 | + self, prefix, **kwargs |
| 71 | + ) |
| 72 | + teamocil_configs = config.in_dir(teamocil_config_dir, extensions="yml") |
| 73 | + completion += [os.path.join(teamocil_config_dir, f) for f in teamocil_configs] |
| 74 | + |
| 75 | + return completion |
| 76 | + |
| 77 | + |
| 78 | +def SessionCompleter(prefix, parsed_args, **kwargs): |
| 79 | + """Return list of session names for argcomplete completer.""" |
| 80 | + |
| 81 | + t = Server(socket_name=parsed_args.socket_name, socket_path=parsed_args.socket_path) |
| 82 | + |
| 83 | + sessions_available = [ |
| 84 | + s.get("session_name") |
| 85 | + for s in t._sessions |
| 86 | + if s.get("session_name").startswith(" ".join(prefix)) |
| 87 | + ] |
| 88 | + |
| 89 | + if parsed_args.session_name and sessions_available: |
| 90 | + return [] |
| 91 | + |
| 92 | + return [ |
| 93 | + s.get("session_name") |
| 94 | + for s in t._sessions |
| 95 | + if s.get("session_name").startswith(prefix) |
| 96 | + ] |
0 commit comments