10
10
import os
11
11
import sys
12
12
import argparse
13
+ import argcomplete
13
14
import logging
14
15
import kaptan
15
16
from . import config
27
28
teamocil_config_dir = os .path .expanduser ('~/.teamocil/' )
28
29
29
30
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
+
30
78
def query_yes_no (question , default = "yes" ):
31
79
"""Ask a yes/no question via raw_input() and return their answer.
32
80
@@ -388,22 +436,23 @@ def cli_parser():
388
436
dest = 'session_name' ,
389
437
type = str ,
390
438
default = None ,
391
- )
439
+ ). completer = SessionCompleter
392
440
393
441
attach_session = subparsers .add_parser ('attach-session' )
394
442
attach_session .set_defaults (callback = subcommand_attach_session )
395
443
396
444
attach_session .add_argument (
397
445
dest = 'session_name' ,
398
446
type = str ,
399
- )
447
+ ). completer = SessionCompleter
400
448
401
449
load = subparsers .add_parser ('load' )
402
450
403
451
loadgroup = load .add_mutually_exclusive_group (required = True )
404
452
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
+ )
407
456
408
457
loadgroup .add_argument (
409
458
dest = 'config' ,
@@ -418,8 +467,10 @@ def cli_parser():
418
467
419
468
will check launch a ~/.pullv.yaml / ~/.pullv.json from the cwd.
420
469
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 )
423
474
load .set_defaults (callback = subcommand_load )
424
475
425
476
convert = subparsers .add_parser ('convert' )
@@ -436,7 +487,8 @@ def cli_parser():
436
487
will check launch a ~/.pullv.yaml / ~/.pullv.json from the cwd.
437
488
will also check for any ./*.yaml and ./*.json.
438
489
''' % (cwd_dir + '/' , config_dir )
439
- )
490
+ ).completer = ConfigCompleter (allowednames = ('.yaml' , '.json' ), directories = False )
491
+
440
492
convert .set_defaults (callback = subcommand_convert )
441
493
442
494
importparser = subparsers .add_parser ('import' )
@@ -449,7 +501,7 @@ def cli_parser():
449
501
import_teamocilgroup = import_teamocil .add_mutually_exclusive_group (
450
502
required = True )
451
503
import_teamocilgroup .add_argument (
452
- '-l' , '- -list' , dest = 'list' , action = 'store_true' ,
504
+ '--list' , dest = 'list' , action = 'store_true' ,
453
505
help = 'List yaml configs in ~/.teamocil and current working directory.'
454
506
)
455
507
@@ -460,15 +512,15 @@ def cli_parser():
460
512
help = '''\
461
513
Checks current ~/.teamocil and current directory for yaml files.
462
514
'''
463
- )
515
+ ). completer = TeamocilCompleter ( allowednames = ( '.yml' ), directories = False )
464
516
import_teamocil .set_defaults (callback = subcommand_import_teamocil )
465
517
466
518
import_tmuxinator = importsubparser .add_parser ('tmuxinator' )
467
519
468
520
import_tmuxinatorgroup = import_tmuxinator .add_mutually_exclusive_group (
469
521
required = True )
470
522
import_tmuxinatorgroup .add_argument (
471
- '-l' , '- -list' , dest = 'list' , action = 'store_true' ,
523
+ '--list' , dest = 'list' , action = 'store_true' ,
472
524
help = 'List yaml configs in ~/.tmuxinator and current working directory.'
473
525
)
474
526
@@ -479,7 +531,7 @@ def cli_parser():
479
531
help = '''\
480
532
Checks current ~/.tmuxinator and current directory for yaml files.
481
533
'''
482
- )
534
+ ). completer = TmuxinatorCompleter ( allowednames = ( '.yml' ), directories = False )
483
535
484
536
import_tmuxinator .set_defaults (callback = subcommand_import_tmuxinator )
485
537
@@ -506,6 +558,8 @@ def main():
506
558
507
559
parser = cli_parser ()
508
560
561
+ argcomplete .autocomplete (parser , always_complete_options = False )
562
+
509
563
args = parser .parse_args ()
510
564
511
565
setupLogger (level = args .log_level .upper ())
@@ -532,93 +586,3 @@ def main():
532
586
subcommand_kill_session (args )
533
587
else :
534
588
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