From 8552270a6858f77b755bbbdac27a827b8aed6e6f Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Mon, 31 Oct 2022 16:19:12 +0100 Subject: [PATCH] added completion support --- cmd/root.go | 22 ++++++++++++++++++ cmd/tablizer.go | 40 +++++++++++++++++++++++++++++++++ tablizer.1 | 59 ++++++++++++++++++++++++++++++++++++++++++++++++- tablizer.pod | 48 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 26776f7..73947ca 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -54,6 +54,26 @@ func Execute() { sortmode cfg.Sortmode ) + var completionCmd = &cobra.Command{ + Use: "completion [bash|zsh|fish|powershell]", + Short: "Generate completion script", + DisableFlagsInUseLine: true, + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + Run: func(cmd *cobra.Command, args []string) { + switch args[0] { + case "bash": + cmd.Root().GenBashCompletion(os.Stdout) + case "zsh": + cmd.Root().GenZshCompletion(os.Stdout) + case "fish": + cmd.Root().GenFishCompletion(os.Stdout, true) + case "powershell": + cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) + } + }, + } + var rootCmd = &cobra.Command{ Use: "tablizer [regex] [file, ...]", Short: "[Re-]tabularize tabular data", @@ -81,6 +101,8 @@ func Execute() { }, } + rootCmd.AddCommand(completionCmd) + // options rootCmd.PersistentFlags().BoolVarP(&conf.Debug, "debug", "d", false, "Enable debugging") rootCmd.PersistentFlags().BoolVarP(&conf.NoNumbering, "no-numbering", "n", false, "Disable header numbering") diff --git a/cmd/tablizer.go b/cmd/tablizer.go index 4d3ae27..e3b5a2c 100644 --- a/cmd/tablizer.go +++ b/cmd/tablizer.go @@ -199,6 +199,46 @@ DESCRIPTION - comma separated list of columns to output, like -c - disable colorization of matches, like -N + COMPLETION + Bash: + source <(%[1]s completion bash) + + To load completions for each session, execute once: + + # Linux: + $ tablizer completion bash > /etc/bash_completion.d/%[1]s + + # macOS: + $ tablizer completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s + + Zsh: + If shell completion is not already enabled in your environment, you + will need to enable it. You can execute the following once: + + echo "autoload -U compinit; compinit" >> ~/.zshrc + + To load completions for each session, execute once: + + $ tablizer completion zsh > "${fpath[1]}/_%[1]s" + + You will need to start a new shell for this setup to take effect. + + fish: + tablizer completion fish | source + + To load completions for each session, execute once: + + tablizer completion fish > ~/.config/fish/completions/%[1]s.fish + + PowerShell: + tablizer completion powershell | Out-String | Invoke-Expression + + To load completions for every new session, run: + + tablizer completion powershell > tablizer.ps1 + + and source this file from your PowerShell profile. + BUGS In order to report a bug, unexpected behavior, feature requests or to submit a patch, please open an issue on github: diff --git a/tablizer.1 b/tablizer.1 index b8d3b91..4058147 100644 --- a/tablizer.1 +++ b/tablizer.1 @@ -133,7 +133,7 @@ .\" ======================================================================== .\" .IX Title "TABLIZER 1" -.TH TABLIZER 1 "2022-10-23" "1" "User Commands" +.TH TABLIZER 1 "2022-10-31" "1" "User Commands" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -370,6 +370,63 @@ precedence over environment variables. .IP "<\s-1NO_COLORS\s0> \- disable colorization of matches, like \fB\-N\fR" 4 .IX Item " - disable colorization of matches, like -N" .PD +.SS "\s-1COMPLETION\s0" +.IX Subsection "COMPLETION" +.IP "Bash:" 4 +.IX Item "Bash:" +.Vb 1 +\& source <(%[1]s completion bash) +.Ve +.Sp +To load completions for each session, execute once: +.Sp +.Vb 2 +\& # Linux: +\& $ tablizer completion bash > /etc/bash_completion.d/%[1]s +\& +\& # macOS: +\& $ tablizer completion bash > $(brew \-\-prefix)/etc/bash_completion.d/%[1]s +.Ve +.IP "Zsh:" 4 +.IX Item "Zsh:" +If shell completion is not already enabled in your environment, +you will need to enable it. You can execute the following once: +.Sp +.Vb 1 +\& echo "autoload \-U compinit; compinit" >> ~/.zshrc +.Ve +.Sp +To load completions for each session, execute once: +.Sp +.Vb 1 +\& $ tablizer completion zsh > "${fpath[1]}/_%[1]s" +.Ve +.Sp +You will need to start a new shell for this setup to take effect. +.IP "fish:" 4 +.IX Item "fish:" +.Vb 1 +\& tablizer completion fish | source +.Ve +.Sp +To load completions for each session, execute once: +.Sp +.Vb 1 +\& tablizer completion fish > ~/.config/fish/completions/%[1]s.fish +.Ve +.IP "PowerShell:" 4 +.IX Item "PowerShell:" +.Vb 1 +\& tablizer completion powershell | Out\-String | Invoke\-Expression +.Ve +.Sp +To load completions for every new session, run: +.Sp +.Vb 1 +\& tablizer completion powershell > tablizer.ps1 +.Ve +.Sp +and source this file from your PowerShell profile. .SH "BUGS" .IX Header "BUGS" In order to report a bug, unexpected behavior, feature requests diff --git a/tablizer.pod b/tablizer.pod index 312dd1d..fd7a238 100644 --- a/tablizer.pod +++ b/tablizer.pod @@ -227,6 +227,54 @@ precedence over environment variables. =back +=head2 COMPLETION + +=over + +=item Bash: + + source <(%[1]s completion bash) + +To load completions for each session, execute once: + + # Linux: + $ tablizer completion bash > /etc/bash_completion.d/%[1]s + + # macOS: + $ tablizer completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s + +=item Zsh: + +If shell completion is not already enabled in your environment, +you will need to enable it. You can execute the following once: + + echo "autoload -U compinit; compinit" >> ~/.zshrc + +To load completions for each session, execute once: + + $ tablizer completion zsh > "${fpath[1]}/_%[1]s" + +You will need to start a new shell for this setup to take effect. + +=item fish: + + tablizer completion fish | source + +To load completions for each session, execute once: + + tablizer completion fish > ~/.config/fish/completions/%[1]s.fish + +=item PowerShell: + + tablizer completion powershell | Out-String | Invoke-Expression + +To load completions for every new session, run: + + tablizer completion powershell > tablizer.ps1 + +and source this file from your PowerShell profile. + +=back =head1 BUGS