cmdView displays a cheatsheet for viewing.
(cmd *cobra.Command, args []string, conf config.Config)
| 14 | |
| 15 | // cmdView displays a cheatsheet for viewing. |
| 16 | func 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] |