mirror of
https://github.com/cheat/cheat.git
synced 2026-03-07 03:03:32 +01:00
Replace docopt-go with spf13/cobra, giving cheat a built-in `--completion` flag that dynamically generates shell completions for bash, zsh, fish, and powershell. This replaces the manually-maintained static completion scripts and resolves the root cause of multiple completion issues (#768, #705, #633, #632, #476). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
87 lines
2.0 KiB
Go
87 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/cheat/cheat/internal/config"
|
|
"github.com/cheat/cheat/internal/display"
|
|
"github.com/cheat/cheat/internal/sheets"
|
|
)
|
|
|
|
// cmdView displays a cheatsheet for viewing.
|
|
func cmdView(cmd *cobra.Command, args []string, conf config.Config) {
|
|
|
|
cheatsheet := args[0]
|
|
|
|
colorize, _ := cmd.Flags().GetBool("colorize")
|
|
|
|
// load the cheatsheets
|
|
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
if cmd.Flags().Changed("tag") {
|
|
tagVal, _ := cmd.Flags().GetString("tag")
|
|
cheatsheets = sheets.Filter(
|
|
cheatsheets,
|
|
strings.Split(tagVal, ","),
|
|
)
|
|
}
|
|
|
|
// if --all was passed, display cheatsheets from all cheatpaths
|
|
allFlag, _ := cmd.Flags().GetBool("all")
|
|
if allFlag {
|
|
// iterate over the cheatpaths
|
|
out := ""
|
|
for _, cheatpath := range cheatsheets {
|
|
|
|
// if the cheatpath contains the specified cheatsheet, display it
|
|
if sheet, ok := cheatpath[cheatsheet]; ok {
|
|
|
|
// identify the matching cheatsheet
|
|
out += fmt.Sprintf("%s %s\n",
|
|
sheet.Title,
|
|
display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf.Color(colorize)),
|
|
)
|
|
|
|
// apply colorization if requested
|
|
if conf.Color(colorize) {
|
|
sheet.Colorize(conf)
|
|
}
|
|
|
|
// display the cheatsheet
|
|
out += display.Indent(sheet.Text) + "\n"
|
|
}
|
|
}
|
|
|
|
// display and exit
|
|
display.Write(strings.TrimSuffix(out, "\n"), conf)
|
|
os.Exit(0)
|
|
}
|
|
|
|
// otherwise, consolidate the cheatsheets found on all paths into a single
|
|
// map of `title` => `sheet` (ie, allow more local cheatsheets to override
|
|
// less local cheatsheets)
|
|
consolidated := sheets.Consolidate(cheatsheets)
|
|
|
|
// fail early if the requested cheatsheet does not exist
|
|
sheet, ok := consolidated[cheatsheet]
|
|
if !ok {
|
|
fmt.Printf("No cheatsheet found for '%s'.\n", cheatsheet)
|
|
os.Exit(2)
|
|
}
|
|
|
|
// apply colorization if requested
|
|
if conf.Color(colorize) {
|
|
sheet.Colorize(conf)
|
|
}
|
|
|
|
// display the cheatsheet
|
|
display.Write(sheet.Text, conf)
|
|
}
|