1010import os
1111import sys
1212import argparse
13+ import argcomplete
1314import logging
1415import kaptan
1516from . import config
2728teamocil_config_dir = os .path .expanduser ('~/.teamocil/' )
2829
2930
31+ class ConfigCompleter (argcomplete .completers .FilesCompleter ):
32+
33+ def __call__ (self , prefix , ** kwargs ):
34+ completion = argcomplete .completers .FilesCompleter .__call__ (
35+ self , prefix , ** kwargs
36+ )
37+
38+ completion += [os .path .join (config_dir , c )
39+ for c in config .in_dir (config_dir )]
40+
41+ return completion
42+
43+
44+ class TmuxinatorCompleter (argcomplete .completers .FilesCompleter ):
45+
46+ def __call__ (self , prefix , ** kwargs ):
47+ completion = argcomplete .completers .FilesCompleter .__call__ (
48+ self , prefix , ** kwargs
49+ )
50+
51+ tmuxinator_configs = config .in_dir (
52+ tmuxinator_config_dir , extensions = 'yml' )
53+ completion += [os .path .join (tmuxinator_config_dir , f )
54+ for f in tmuxinator_configs ]
55+
56+ return completion
57+
58+
59+ class TeamocilCompleter (argcomplete .completers .FilesCompleter ):
60+
61+ def __call__ (self , prefix , ** kwargs ):
62+ completion = argcomplete .completers .FilesCompleter .__call__ (
63+ self , prefix , ** kwargs
64+ )
65+
66+ teamocil_configs = config .in_dir (teamocil_config_dir , extensions = 'yml' )
67+ completion += [os .path .join (teamocil_config_dir , f )
68+ for f in teamocil_configs ]
69+
70+ return completion
71+
72+
73+ def SessionCompleter (prefix , ** kwargs ):
74+ t = Server ()
75+ return [s .get ('session_name' ) for s in t ._sessions if s .get ('session_name' ).startswith (prefix )]
76+
77+
3078def query_yes_no (question , default = "yes" ):
3179 """Ask a yes/no question via raw_input() and return their answer.
3280
@@ -388,22 +436,23 @@ def cli_parser():
388436 dest = 'session_name' ,
389437 type = str ,
390438 default = None ,
391- )
439+ ). completer = SessionCompleter
392440
393441 attach_session = subparsers .add_parser ('attach-session' )
394442 attach_session .set_defaults (callback = subcommand_attach_session )
395443
396444 attach_session .add_argument (
397445 dest = 'session_name' ,
398446 type = str ,
399- )
447+ ). completer = SessionCompleter
400448
401449 load = subparsers .add_parser ('load' )
402450
403451 loadgroup = load .add_mutually_exclusive_group (required = True )
404452 loadgroup .add_argument (
405- '-l' , '--list' , dest = 'list' , action = 'store_true' ,
406- help = 'List config files available' )
453+ '--list' , dest = 'list' , action = 'store_true' ,
454+ help = 'List config files available' ,
455+ )
407456
408457 loadgroup .add_argument (
409458 dest = 'config' ,
@@ -418,8 +467,10 @@ def cli_parser():
418467
419468 will check launch a ~/.pullv.yaml / ~/.pullv.json from the cwd.
420469 will also check for any ./*.yaml and ./*.json.
421- ''' % (cwd_dir + '/' , config_dir )
422- )
470+ ''' % (cwd_dir + '/' , config_dir ),
471+ #).completer = ConfigCompleter
472+ #).completer = argcomplete.completers.FilesCompleter(allowednames=('.yaml', '.json'), directories=False)
473+ ).completer = ConfigCompleter (allowednames = ('.yaml' , '.json' ), directories = False )
423474 load .set_defaults (callback = subcommand_load )
424475
425476 convert = subparsers .add_parser ('convert' )
@@ -436,7 +487,8 @@ def cli_parser():
436487 will check launch a ~/.pullv.yaml / ~/.pullv.json from the cwd.
437488 will also check for any ./*.yaml and ./*.json.
438489 ''' % (cwd_dir + '/' , config_dir )
439- )
490+ ).completer = ConfigCompleter (allowednames = ('.yaml' , '.json' ), directories = False )
491+
440492 convert .set_defaults (callback = subcommand_convert )
441493
442494 importparser = subparsers .add_parser ('import' )
@@ -449,7 +501,7 @@ def cli_parser():
449501 import_teamocilgroup = import_teamocil .add_mutually_exclusive_group (
450502 required = True )
451503 import_teamocilgroup .add_argument (
452- '-l' , '- -list' , dest = 'list' , action = 'store_true' ,
504+ '--list' , dest = 'list' , action = 'store_true' ,
453505 help = 'List yaml configs in ~/.teamocil and current working directory.'
454506 )
455507
@@ -460,15 +512,15 @@ def cli_parser():
460512 help = '''\
461513 Checks current ~/.teamocil and current directory for yaml files.
462514 '''
463- )
515+ ). completer = TeamocilCompleter ( allowednames = ( '.yml' ), directories = False )
464516 import_teamocil .set_defaults (callback = subcommand_import_teamocil )
465517
466518 import_tmuxinator = importsubparser .add_parser ('tmuxinator' )
467519
468520 import_tmuxinatorgroup = import_tmuxinator .add_mutually_exclusive_group (
469521 required = True )
470522 import_tmuxinatorgroup .add_argument (
471- '-l' , '- -list' , dest = 'list' , action = 'store_true' ,
523+ '--list' , dest = 'list' , action = 'store_true' ,
472524 help = 'List yaml configs in ~/.tmuxinator and current working directory.'
473525 )
474526
@@ -479,7 +531,7 @@ def cli_parser():
479531 help = '''\
480532 Checks current ~/.tmuxinator and current directory for yaml files.
481533 '''
482- )
534+ ). completer = TmuxinatorCompleter ( allowednames = ( '.yml' ), directories = False )
483535
484536 import_tmuxinator .set_defaults (callback = subcommand_import_tmuxinator )
485537
@@ -506,6 +558,8 @@ def main():
506558
507559 parser = cli_parser ()
508560
561+ argcomplete .autocomplete (parser , always_complete_options = False )
562+
509563 args = parser .parse_args ()
510564
511565 setupLogger (level = args .log_level .upper ())
@@ -532,93 +586,3 @@ def main():
532586 subcommand_kill_session (args )
533587 else :
534588 parser .print_help ()
535-
536-
537- def complete (cline , cpoint ):
538-
539- parser = argparse .ArgumentParser ()
540- parser .add_argument ('-L' , dest = 'socket_name' , default = None ,
541- metavar = 'socket-name' )
542-
543- parser .add_argument ('-S' , dest = 'socket_path' , default = None ,
544- metavar = 'socket-path' )
545-
546- parser .add_argument (
547- dest = 'ctexta' ,
548- nargs = '*' ,
549- type = str ,
550- default = None ,
551- )
552-
553- args = parser .parse_args ()
554- ctext = cline .replace ('tmuxp ' , '' )
555-
556- commands = []
557- commands .extend (['attach-session' , 'kill-session' , 'load' , 'convert' ])
558-
559- commands = [c for c in commands if ctext in c ]
560-
561- if args .socket_path :
562- ctext = ctext .replace (args .socket_path or None , '' )
563-
564- if args .socket_name :
565- ctext = ctext .replace (args .socket_name or None , '' )
566-
567- t = Server (
568- socket_name = args .socket_name or None ,
569- socket_path = args .socket_path or None
570- )
571-
572- def session_complete (command , commands , ctext ):
573- if ctext .startswith (command + ' ' ):
574- commands [:] = []
575- ctext_subargs = ctext .replace (command + ' ' , '' )
576-
577- try :
578- sessions = [s .get ('session_name' ) for s in t ._sessions ]
579- commands .extend ([c for c in sessions if ctext_subargs in c ])
580- except Exception :
581- pass
582-
583- def config_complete (command , commands , ctext ):
584- if ctext .startswith (command + ' ' ):
585- commands [:] = []
586- ctext_subargs = ctext .replace (command + ' ' , '' )
587- configs = []
588-
589- # if ctext_subargs.startswith('.') or ctext_subargs.startswith('/'):
590- # configs += ['.hi']
591-
592- # if ctext_subargs.endswith('/') and ctext_subargs != './':
593- # configs += ['./' + c for c in config.in_dir(os.path.relpath(ctext_subargs))]
594- # else:
595- # configs += os.listdir(os.path.relpath(ctext_subargs))
596-
597- configs += ['./' + c for c in config .in_dir (cwd_dir )]
598- configs += ['./' + c for c in config .in_cwd ()]
599- configs += [os .path .join (config_dir , c )
600- for c in config .in_dir (config_dir )]
601- commands += [c for c in configs if c .startswith (ctext_subargs )]
602-
603- def teamocil_config_complete (command , commands , ctext ):
604- try :
605- configs_in_user = config .in_dir (
606- teamocil_config_dir , extensions = 'yml' )
607- except OSError :
608- configs_in_user = []
609- configs_in_cwd = config .in_dir (config_dir = cwd_dir , extensions = 'yml' )
610-
611- def tmuxinator_config_complete (command , commands , ctext ):
612- try :
613- configs_in_user = config .in_dir (
614- tmuxinator_config_dir , extensions = 'yml' )
615- except OSError :
616- configs_in_user = []
617- configs_in_cwd = config .in_dir (config_dir = cwd_dir , extensions = 'yml' )
618-
619- session_complete ('attach-session' , commands , ctext )
620- session_complete ('kill-session' , commands , ctext )
621- config_complete ('load' , commands , ctext )
622- config_complete ('convert' , commands , ctext )
623-
624- print (' \n ' .join (commands ))
0 commit comments