MCPcopy Index your code
hub / github.com/cheat/cheat / cmdView

Function cmdView

cmd/cheat/cmd_view.go:16–86  ·  view source on GitHub ↗

cmdView displays a cheatsheet for viewing.

(cmd *cobra.Command, args []string, conf config.Config)

Source from the content-addressed store, hash-verified

14
15// cmdView displays a cheatsheet for viewing.
16func cmdView(cmd *cobra.Command, args []string, conf config.Config) {
17
18 cheatsheet := args[0]
19
20 colorize, _ := cmd.Flags().GetBool("colorize")
21
22 // load the cheatsheets
23 cheatsheets, err := sheets.Load(conf.Cheatpaths)
24 if err != nil {
25 fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
26 os.Exit(1)
27 }
28 if cmd.Flags().Changed("tag") {
29 tagVal, _ := cmd.Flags().GetString("tag")
30 cheatsheets = sheets.Filter(
31 cheatsheets,
32 strings.Split(tagVal, ","),
33 )
34 }
35
36 // if --all was passed, display cheatsheets from all cheatpaths
37 allFlag, _ := cmd.Flags().GetBool("all")
38 if allFlag {
39 // iterate over the cheatpaths
40 out := ""
41 for _, cheatpath := range cheatsheets {
42
43 // if the cheatpath contains the specified cheatsheet, display it
44 if sheet, ok := cheatpath[cheatsheet]; ok {
45
46 // identify the matching cheatsheet
47 out += fmt.Sprintf("%s %s\n",
48 sheet.Title,
49 display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf.Color(colorize)),
50 )
51
52 // apply colorization if requested
53 if conf.Color(colorize) {
54 sheet.Colorize(conf)
55 }
56
57 // display the cheatsheet
58 out += display.Indent(sheet.Text) + "\n"
59 }
60 }
61
62 // display and exit
63 display.Write(strings.TrimSuffix(out, "\n"), conf)
64 os.Exit(0)
65 }
66
67 // otherwise, consolidate the cheatsheets found on all paths into a single
68 // map of `title` => `sheet` (ie, allow more local cheatsheets to override
69 // less local cheatsheets)
70 consolidated := sheets.Consolidate(cheatsheets)
71
72 // fail early if the requested cheatsheet does not exist
73 sheet, ok := consolidated[cheatsheet]

Callers 1

runFunction · 0.85

Calls 8

LoadFunction · 0.92
FilterFunction · 0.92
FaintFunction · 0.92
IndentFunction · 0.92
WriteFunction · 0.92
ConsolidateFunction · 0.92
ColorMethod · 0.80
ColorizeMethod · 0.80

Tested by

no test coverage detected