Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fish completion for programs that output trailing empty lines #1284

Merged
merged 1 commit into from May 3, 2021

Conversation

@marckhouzam
Copy link
Contributor

@marckhouzam marckhouzam commented Nov 27, 2020

Fixes #1279

Some programs output extra empty lines at the end of their output, which affects the output of the __complete command.
goreleaser is such an case:
https://github.com/goreleaser/goreleaser/blob/f35534d9d7cbc2d69fcbd4edb78a5d668357fa60/cmd/root.go#L23

Those lines must be ignored for fish shell completion to work.
zsh and bash are not impacted.

To test this fix use the following commands for the program that follows.

$ go build -o testprog testprog.go
$ fish
$ ./testprog completion fish | source
$ ./testprog <TAB>

# Without this fix the completion directive :4 will be shown as a valid completion

Test program which adds extra empty lines to the output:

package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

var completionNoDesc = false

var completionCmd = &cobra.Command{
	Use:   "completion [bashzsh|fish|powershell]",
	Short: "Generate completion script",
	Long: `To load completions:

Bash:

$ source <(testprog completion bash)

# To load completions for each session, execute once:
Linux:
  $ testprog completion bash > /etc/bash_completion.d/testprog
MacOS:
  $ testprog completion bash > /usr/local/etc/bash_completion.d/testprog

Zsh:

$ source <(testprog completion zsh)
$ compdef _testprog testprog

# To load completions for each session, execute once:
$ testprog completion zsh > "${fpath[1]}/_testprog"

Fish:

$ testprog completion fish | source

# To load completions for each session, execute once:
$ testprog completion fish > ~/.config/fish/completions/testprog.fish

PowerShell:

PS C:\> testprog completion powershell | Out-String | Invoke-Expression

To load completions for every new session, add the output of the above command
to your powershell profile.
`,
	DisableFlagsInUseLine: true,
	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		return []string{"bash\tBash completion", "zsh", "fish", "powershell"}, cobra.ShellCompDirectiveNoFileComp
	},
	Args: cobra.ExactValidArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		switch args[0] {
		case "bash":
			cmd.Root().GenBashCompletion(os.Stdout)
		case "zsh":
			if !completionNoDesc {
				cmd.Root().GenZshCompletion(os.Stdout)
			} else {
				cmd.Root().GenZshCompletionNoDesc(os.Stdout)
			}
		case "fish":
			cmd.Root().GenFishCompletion(os.Stdout, !completionNoDesc)
		}
	},
}

var rootCmd = &cobra.Command{
	Use: "testprog",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("rootCmd called")
	},
}

func setFlags() {
	rootCmd.PersistentFlags().String("theme", "", "theme to use (located in /themes/THEMENAME/)")
}

func main() {
	completionCmd.Flags().BoolVar(
		&completionNoDesc,
		"no-descriptions", false,
		"disable completion description for shells that support it")
	rootCmd.AddCommand(completionCmd)

	setFlags()

	fmt.Println("")
	fmt.Println("")
	rootCmd.Execute()
	fmt.Println("     ")
	fmt.Println("")
}
Copy link

@caarlos0 caarlos0 left a comment

Lgtm

@jharshman jharshman added this to the Next milestone Dec 18, 2020
fish_completions.go Outdated Show resolved Hide resolved
@marckhouzam marckhouzam force-pushed the VilledeMontreal:fix/compEmptyLastLine branch from 9aa385a to 3ca9688 Jan 30, 2021
@marckhouzam
Copy link
Contributor Author

@marckhouzam marckhouzam commented Jan 30, 2021

Thanks @krobelus for the suggestion. I have included your suggested change, squashed the two commits and added you as co-author.

@marckhouzam marckhouzam force-pushed the VilledeMontreal:fix/compEmptyLastLine branch from 3ca9688 to 33141b2 Feb 14, 2021
Some programs may output extra empty lines after the directive.
Those lines must be ignored for fish shell completion to work.
zsh and bash are not impacted.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
Co-authored-by: Johannes Altmanninger <aclopte@gmail.com>
@marckhouzam marckhouzam force-pushed the VilledeMontreal:fix/compEmptyLastLine branch from 33141b2 to 1f1a103 Feb 15, 2021
@github-actions
Copy link

@github-actions github-actions bot commented Apr 17, 2021

This PR is being marked as stale due to a long period of inactivity

@marckhouzam
Copy link
Contributor Author

@marckhouzam marckhouzam commented Apr 17, 2021

Waiting for review

@krobelus
Copy link
Contributor

@krobelus krobelus commented Apr 17, 2021

Looks great FWIW.

@jpmcb
jpmcb approved these changes May 3, 2021
Copy link
Collaborator

@jpmcb jpmcb left a comment

LGTM - thanks for this PR and thanks for the patience! Glad to see we are getting some fish adoption after all the work that went into adding support for it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

5 participants