@@ -39,42 +39,73 @@ func main() {
39
39
}
40
40
}
41
41
42
- func run (ctx context.Context , appName , appVersion string , cliargs []string ) error {
42
+ func run (ctx context.Context , name , version string , cliargs []string ) error {
43
43
bg := colors .FromColor (color .Transparent )
44
+ var (
45
+ bashCompletion bool
46
+ zshCompletion bool
47
+ fishCompletion bool
48
+ powershellCompletion bool
49
+ noDescriptions bool
50
+ )
44
51
c := & cobra.Command {
45
- Use : appName + " [flags] <image1> [image2, ..., imageN]" ,
46
- Short : appName + ", a command-line image viewer using terminal graphics" ,
47
- Version : appVersion ,
48
- RunE : func (cmd * cobra.Command , args []string ) error {
49
- return do (os .Stdout , & Params {
50
- BG : bg ,
51
- Args : args ,
52
- })
52
+ Use : name + " [flags] <image1> [image2, ..., imageN]" ,
53
+ Short : name + ", a command-line image viewer using terminal graphics" ,
54
+ Version : version ,
55
+ SilenceErrors : true ,
56
+ SilenceUsage : false ,
57
+ RunE : func (cmd * cobra.Command , cliargs []string ) error {
58
+ // completions and short circuits
59
+ switch {
60
+ case bashCompletion :
61
+ return cmd .GenBashCompletionV2 (os .Stdout , ! noDescriptions )
62
+ case zshCompletion :
63
+ if noDescriptions {
64
+ return cmd .GenZshCompletionNoDesc (os .Stdout )
65
+ }
66
+ return cmd .GenZshCompletion (os .Stdout )
67
+ case fishCompletion :
68
+ return cmd .GenFishCompletion (os .Stdout , ! noDescriptions )
69
+ case powershellCompletion :
70
+ if noDescriptions {
71
+ return cmd .GenPowerShellCompletion (os .Stdout )
72
+ }
73
+ return cmd .GenPowerShellCompletionWithDesc (os .Stdout )
74
+ }
75
+ return do (os .Stdout , bg , cliargs )
53
76
},
54
77
}
55
- c .Flags ().Var (bg .Pflag (), "bg" , "background color" )
56
78
c .SetVersionTemplate ("{{ .Name }} {{ .Version }}\n " )
57
79
c .InitDefaultHelpCmd ()
58
80
c .SetArgs (cliargs [1 :])
59
- c .SilenceErrors , c .SilenceUsage = true , false
81
+ flags := c .Flags ()
82
+ flags .Var (bg .Pflag (), "bg" , "background color" )
83
+ // completions
84
+ flags .BoolVar (& bashCompletion , "completion-script-bash" , false , "output bash completion script and exit" )
85
+ flags .BoolVar (& zshCompletion , "completion-script-zsh" , false , "output zsh completion script and exit" )
86
+ flags .BoolVar (& fishCompletion , "completion-script-fish" , false , "output fish completion script and exit" )
87
+ flags .BoolVar (& powershellCompletion , "completion-script-powershell" , false , "output powershell completion script and exit" )
88
+ flags .BoolVar (& noDescriptions , "no-descriptions" , false , "disable descriptions in completion scripts" )
89
+ // mark hidden
90
+ for _ , name := range []string {
91
+ "completion-script-bash" , "completion-script-zsh" , "completion-script-fish" ,
92
+ "completion-script-powershell" , "no-descriptions" ,
93
+ } {
94
+ flags .Lookup (name ).Hidden = true
95
+ }
60
96
return c .ExecuteContext (ctx )
61
97
}
62
98
63
- type Params struct {
64
- BG color.Color
65
- Args []string
66
- }
67
-
68
99
// do renders the specified files to w.
69
- func do (w io.Writer , params * Params ) error {
100
+ func do (w io.Writer , bg color. Color , args [] string ) error {
70
101
if ! rasterm .Available () {
71
102
return rasterm .ErrTermGraphicsNotAvailable
72
103
}
73
- resvg .WithBackground (params . BG )(resvg .Default )
104
+ resvg .WithBackground (bg )(resvg .Default )
74
105
// collect files
75
106
var files []string
76
- for i := 0 ; i < len (params . Args ); i ++ {
77
- v , err := open (params . Args [i ])
107
+ for i := 0 ; i < len (args ); i ++ {
108
+ v , err := open (args [i ])
78
109
if err != nil {
79
110
fmt .Fprintf (w , "error: unable to open arg %d: %v\n " , i , err )
80
111
}
0 commit comments