@@ -29,77 +29,75 @@ import (
29
29
// )
30
30
// }
31
31
func ActionArgcomplete (command ... string ) carapace.Action {
32
- return carapace .ActionCallback (func (c carapace.Context ) carapace.Action {
33
- if len (command ) == 0 {
34
- return carapace .ActionMessage ("missing argument [ActionArgcomplete]" )
35
- }
36
-
37
- if _ , err := exec .LookPath (command [0 ]); err != nil {
38
- return carapace .ActionMessage (err .Error ())
39
- }
32
+ return actionCommand (command ... )(func (command ... string ) carapace.Action {
33
+ return carapace .ActionCallback (func (c carapace.Context ) carapace.Action {
34
+ if _ , err := exec .LookPath (command [0 ]); err != nil {
35
+ return carapace .ActionMessage (err .Error ())
36
+ }
40
37
41
- args := append (command [1 :], c .Args ... )
42
- current := c .Value
38
+ args := append (command [1 :], c .Args ... )
39
+ current := c .Value
43
40
44
- prefix := ""
45
- if strings .HasPrefix (current , "--" ) {
46
- if strings .Contains (current , "=" ) { // optarg flag which is handled as normal arg by the completer
47
- splitted := strings .SplitN (current , "=" , 2 )
48
- prefix = splitted [0 ] + "="
49
- args = append (args , splitted [0 ]) // add flag as arg
50
- current = "" // seem partial optarg value isn't completed
41
+ prefix := ""
42
+ if strings .HasPrefix (current , "--" ) {
43
+ if strings .Contains (current , "=" ) { // optarg flag which is handled as normal arg by the completer
44
+ splitted := strings .SplitN (current , "=" , 2 )
45
+ prefix = splitted [0 ] + "="
46
+ args = append (args , splitted [0 ]) // add flag as arg
47
+ current = "" // seem partial optarg value isn't completed
51
48
49
+ } else {
50
+ current = "--" // seems partial flag names aren't completed so get all
51
+ }
52
52
} else {
53
- current = "-- " // seems partial flag names aren't completed so get all
53
+ current = "" // seems partial positional arguments aren't completed as well
54
54
}
55
- } else {
56
- current = "" // seems partial positional arguments aren't completed as well
57
- }
58
55
59
- compLine := command [0 ] + " " + strings .Join (append (args , current ), " " ) // TODO escape/quote special characters
60
- c .Setenv ("_ARGCOMPLETE" , "1" )
61
- c .Setenv ("_ARGCOMPLETE_DFS" , "\t " )
62
- c .Setenv ("_ARGCOMPLETE_IFS" , "\n " )
63
- c .Setenv ("_ARGCOMPLETE_SHELL" , "fish" )
64
- c .Setenv ("_ARGCOMPLETE_SUPPRESS_SPACE" , "1" ) // TODO needed? relevant for nospace detection?
65
- // c.Setenv("_ARGCOMPLETE_COMP_WORDBREAKS", " ") // TODO set to space-only for multiparts?
66
- c .Setenv ("_ARGCOMPLETE" , "1" )
67
- c .Setenv ("COMP_LINE" , compLine )
68
- c .Setenv ("COMP_POINT" , strconv .Itoa (len (compLine )))
69
- nospace := false
70
- a := carapace .ActionExecCommand ("sh" , "-c" , command [0 ]+ " 8>&1 9>&2 1>/dev/null 2>/dev/null" )(func (output []byte ) carapace.Action {
71
- lines := strings .Split (string (output ), "\n " )
72
- vals := make ([]string , 0 )
73
- isFlag := strings .HasPrefix (c .Value , "-" )
74
- for _ , line := range lines [:len (lines )- 1 ] {
75
- if ! isFlag && strings .HasPrefix (line , "-" ) {
76
- continue
77
- }
78
- if strings .HasSuffix (line , "=" ) ||
79
- strings .HasSuffix (line , "/" ) ||
80
- strings .HasSuffix (line , "," ) {
81
- nospace = true
82
- }
83
- if splitted := strings .SplitN (line , "\t " , 2 ); splitted [0 ] != "" {
84
- vals = append (vals , splitted ... )
85
- if len (splitted ) < 2 {
86
- vals = append (vals , "" )
56
+ compLine := command [0 ] + " " + strings .Join (append (args , current ), " " ) // TODO escape/quote special characters
57
+ c .Setenv ("_ARGCOMPLETE" , "1" )
58
+ c .Setenv ("_ARGCOMPLETE_DFS" , "\t " )
59
+ c .Setenv ("_ARGCOMPLETE_IFS" , "\n " )
60
+ c .Setenv ("_ARGCOMPLETE_SHELL" , "fish" )
61
+ c .Setenv ("_ARGCOMPLETE_SUPPRESS_SPACE" , "1" ) // TODO needed? relevant for nospace detection?
62
+ // c.Setenv("_ARGCOMPLETE_COMP_WORDBREAKS", " ") // TODO set to space-only for multiparts?
63
+ c .Setenv ("_ARGCOMPLETE" , "1" )
64
+ c .Setenv ("COMP_LINE" , compLine )
65
+ c .Setenv ("COMP_POINT" , strconv .Itoa (len (compLine )))
66
+ nospace := false
67
+ a := carapace .ActionExecCommand ("sh" , "-c" , command [0 ]+ " 8>&1 9>&2 1>/dev/null 2>/dev/null" )(func (output []byte ) carapace.Action {
68
+ lines := strings .Split (string (output ), "\n " )
69
+ vals := make ([]string , 0 )
70
+ isFlag := strings .HasPrefix (c .Value , "-" )
71
+ for _ , line := range lines [:len (lines )- 1 ] {
72
+ if ! isFlag && strings .HasPrefix (line , "-" ) {
73
+ continue
74
+ }
75
+ if strings .HasSuffix (line , "=" ) ||
76
+ strings .HasSuffix (line , "/" ) ||
77
+ strings .HasSuffix (line , "," ) {
78
+ nospace = true
79
+ }
80
+ if splitted := strings .SplitN (line , "\t " , 2 ); splitted [0 ] != "" {
81
+ vals = append (vals , splitted ... )
82
+ if len (splitted ) < 2 {
83
+ vals = append (vals , "" )
84
+ }
87
85
}
88
86
}
89
- }
90
87
91
- if len (vals ) == 0 {
92
- // fallback to file completions when no values returned
93
- if index := strings .Index (c .Value , "=" ); index > - 1 {
94
- return carapace .ActionFiles ().Invoke (carapace.Context {Value : c .Value [index + 1 :]}).ToA ()
88
+ if len (vals ) == 0 {
89
+ // fallback to file completions when no values returned
90
+ if index := strings .Index (c .Value , "=" ); index > - 1 {
91
+ return carapace .ActionFiles ().Invoke (carapace.Context {Value : c .Value [index + 1 :]}).ToA ()
92
+ }
93
+ return carapace .ActionFiles ()
95
94
}
96
- return carapace .ActionFiles ()
95
+ return carapace .ActionValuesDescribed (vals ... )
96
+ }).Invoke (c ).Prefix (prefix ).ToA () // re-add optarg prefix
97
+ if nospace {
98
+ return a .NoSpace ()
97
99
}
98
- return carapace .ActionValuesDescribed (vals ... )
99
- }).Invoke (c ).Prefix (prefix ).ToA () // re-add optarg prefix
100
- if nospace {
101
- return a .NoSpace ()
102
- }
103
- return a
100
+ return a
101
+ })
104
102
})
105
103
}
0 commit comments