commit bd47b2d435363b260b1caa8e4c607b2b80c7f708
parent 815d90dfcfe120ff5e07e4fcb003d29edde75d24
Author: Sean Enck <sean@ttypty.com>
Date: Fri, 19 Apr 2024 22:40:29 -0400
handle completion types a common way
Diffstat:
3 files changed, 20 insertions(+), 10 deletions(-)
diff --git a/internal/app/completions.go b/internal/app/completions.go
@@ -5,6 +5,7 @@ import (
"bytes"
"errors"
"fmt"
+ "slices"
"strings"
"text/template"
@@ -123,7 +124,7 @@ func loadProfiles(exe string, canFilter bool) []Profile {
func GenerateCompletions(completionType string, isHelp bool, exe string) ([]string, error) {
if isHelp {
h := []string{"completions are available for:"}
- for _, s := range []string{CompletionsBashCommand, CompletionsFishCommand, CompletionsZshCommand} {
+ for _, s := range completionTypes {
h = append(h, fmt.Sprintf(" - %s", s))
}
h = append(h, "")
@@ -148,6 +149,9 @@ when %s=<unknown>
`, config.EnvironmentCompletionKey, config.EnvironmentCompletionKey, config.EnvironmentCompletionKey)))
return h, nil
}
+ if !slices.Contains(completionTypes, completionType) {
+ return nil, fmt.Errorf("unknown completion request: %s", completionType)
+ }
c := Completions{
Executable: exe,
InsertCommand: InsertCommand,
diff --git a/internal/app/completions_test.go b/internal/app/completions_test.go
@@ -12,6 +12,9 @@ func TestCompletions(t *testing.T) {
testCompletion(t, "bash")
testCompletion(t, "zsh")
testCompletion(t, "fish")
+ if _, err := app.GenerateCompletions("invalid", false, "lb"); err.Error() != "unknown completion request: invalid" {
+ t.Errorf("invalid error: %v", err)
+ }
}
func testCompletion(t *testing.T, completionMode string) {
diff --git a/internal/app/core.go b/internal/app/core.go
@@ -79,8 +79,11 @@ const (
textFile = ".txt"
)
-//go:embed doc/*
-var docs embed.FS
+var (
+ //go:embed doc/*
+ docs embed.FS
+ completionTypes = []string{CompletionsBashCommand, CompletionsFishCommand, CompletionsZshCommand}
+)
type (
// CommandOptions define how commands operate as an application
@@ -180,18 +183,18 @@ func commandText(args, name, desc string) string {
if len(args) > 0 {
arguments = fmt.Sprintf("[%s]", args)
}
- return fmt.Sprintf(" %-15s %-10s %s", name, arguments, desc)
+ return fmt.Sprintf(" %-18s %-10s %s", name, arguments, desc)
}
// Usage return usage information
func Usage(verbose bool, exe string) ([]string, error) {
var results []string
results = append(results, command(ClipCommand, "entry", "copy the entry's value into the clipboard"))
- results = append(results, command(CompletionsCommand, "<shell>", "generate completions via auto-detection of shell"))
- results = append(results, subCommand(CompletionsCommand, CompletionsBashCommand, "", "generate bash completions"))
- results = append(results, subCommand(CompletionsCommand, CompletionsFishCommand, "", "generate fish completions"))
- results = append(results, subCommand(CompletionsCommand, CompletionsHelpCommand, "", "show help information about completions"))
- results = append(results, subCommand(CompletionsCommand, CompletionsZshCommand, "", "generate zsh completions"))
+ results = append(results, command(CompletionsCommand, "<shell>", "generate completions via auto-detection"))
+ results = append(results, subCommand(CompletionsCommand, CompletionsHelpCommand, "", "show help information for completions"))
+ for _, c := range completionTypes {
+ results = append(results, subCommand(CompletionsCommand, c, "", fmt.Sprintf("generate %s completions", c)))
+ }
results = append(results, command(EnvCommand, "", "display environment variable information"))
results = append(results, command(HelpCommand, "", "show this usage information"))
results = append(results, subCommand(HelpCommand, HelpAdvancedCommand, "", "display verbose help information"))
@@ -208,7 +211,7 @@ func Usage(verbose bool, exe string) ([]string, error) {
results = append(results, subCommand(TOTPCommand, TOTPInsertCommand, "entry", "insert a new totp entry into the store"))
results = append(results, subCommand(TOTPCommand, TOTPListCommand, "", "list entries with totp settings"))
results = append(results, subCommand(TOTPCommand, TOTPOnceCommand, "entry", "display the first generated code"))
- results = append(results, subCommand(TOTPCommand, TOTPMinimalCommand, "entry", "display the first generated code (no details)"))
+ results = append(results, subCommand(TOTPCommand, TOTPMinimalCommand, "entry", "display one generated code (no details)"))
results = append(results, subCommand(TOTPCommand, TOTPShowCommand, "entry", "show the totp entry"))
results = append(results, command(VersionCommand, "", "display version information"))
sort.Strings(results)