fish.go
This file
Path
completion/fish.go
93 lines · 1.9 KiB · Go
Last changed by
fix: wildcard escaping issue (#862)Abel Chalierbefore adoption85a2980
History of this fileTake it
The walk stopped after 200 commits: lines older than 6837ed2 point at it.
feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 1–81why, one step back1package completion23import (4 "fmt"5 "io"6 "strings"78 "github.com/alecthomas/kong"9)1011// Fish is a fish shell completion generator.12type Fish struct{}1314// Run generates fish completion script.15func (f Fish) Run(ctx *kong.Context) error {16 var buf strings.Builder17 buf.WriteString(`# Fish shell completion for gum18# Generated by gum completion1920# disable file completion unless explicitly enabled21complete -c gum -f2223`)24 node := ctx.Model.Node25 f.gen(&buf, node)26 _, err := fmt.Fprint(ctx.Stdout, buf.String())27 if err != nil {28 return fmt.Errorf("unable to generate fish completion: %w", err)29 }30 return nil31}3233func (f Fish) gen(buf io.StringWriter, cmd *kong.Node) {34 root := cmd35 for root.Parent != nil {36 root = root.Parent37 }38 rootName := root.Name39 if cmd.Parent == nil {40 _, _ = buf.WriteString(fmt.Sprintf("# %s\n", rootName))41 } else {42 _, _ = buf.WriteString(fmt.Sprintf("# %s\n", cmd.Path()))43 _, _ = buf.WriteString(44 fmt.Sprintf("complete -c %s -f -n '__fish_use_subcommand' -a %s -d '%s'\n",45 rootName,46 cmd.Name,47 cmd.Help,48 ),49 )50 }5152 for _, f := range cmd.Flags {53 if f.Hidden {54 continue55 }56 if cmd.Parent == nil {57 _, _ = buf.WriteString(58 fmt.Sprintf("complete -c %s -f",59 rootName,60 ),61 )62 } else {63 _, _ = buf.WriteString(64 fmt.Sprintf("complete -c %s -f -n '__fish_seen_subcommand_from %s'",65 rootName,66 cmd.Name,67 ),68 )69 }70 if !f.IsBool() {71 enums := flagPossibleValues(f)72 if len(enums) > 0 {73 _, _ = buf.WriteString(fmt.Sprintf(" -xa '%s'", strings.Join(enums, " ")))74 } else {75 _, _ = buf.WriteString(" -x")76 }77 }78 if f.Short != 0 {79 _, _ = buf.WriteString(fmt.Sprintf(" -s %c", f.Short))80 }81 _, _ = buf.WriteString(fmt.Sprintf(" -l %s", f.Name))fix: wildcard escaping issue (#862)Abel Chalierbefore adoption85a2980
line 82why, one step back82 _, _ = buf.WriteString(fmt.Sprintf(" -d \"%s\"", f.Help))feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 83–93why, one step back83 _, _ = buf.WriteString("\n")84 }85 _, _ = buf.WriteString("\n")8687 for _, c := range cmd.Children {88 if c == nil || c.Hidden {89 continue90 }91 f.gen(buf, c)92 }93}