2019-10-20 16:02:28 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/cheat/cheat/internal/config"
|
2020-06-26 00:21:51 +02:00
|
|
|
"github.com/cheat/cheat/internal/display"
|
2019-10-20 16:02:28 +02:00
|
|
|
"github.com/cheat/cheat/internal/sheets"
|
|
|
|
)
|
|
|
|
|
|
|
|
// cmdView displays a cheatsheet for viewing.
|
|
|
|
func cmdView(opts map[string]interface{}, conf config.Config) {
|
|
|
|
|
|
|
|
cheatsheet := opts["<cheatsheet>"].(string)
|
|
|
|
|
|
|
|
// load the cheatsheets
|
|
|
|
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
|
|
|
if err != nil {
|
2022-08-05 02:38:49 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
|
2019-10-20 16:02:28 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter cheatcheats by tag if --tag was provided
|
|
|
|
if opts["--tag"] != nil {
|
|
|
|
cheatsheets = sheets.Filter(
|
|
|
|
cheatsheets,
|
|
|
|
strings.Split(opts["--tag"].(string), ","),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-27 22:39:34 +01:00
|
|
|
// if --all was passed, display cheatsheets from all cheatpaths
|
|
|
|
if opts["--all"].(bool) {
|
|
|
|
// iterate over the cheatpaths
|
2020-11-28 17:18:16 +01:00
|
|
|
out := ""
|
2020-11-27 22:39:34 +01:00
|
|
|
for _, cheatpath := range cheatsheets {
|
|
|
|
|
|
|
|
// if the cheatpath contains the specified cheatsheet, display it
|
|
|
|
if sheet, ok := cheatpath[cheatsheet]; ok {
|
|
|
|
|
|
|
|
// identify the matching cheatsheet
|
2020-11-28 17:18:16 +01:00
|
|
|
out += fmt.Sprintf("%s %s\n",
|
2022-08-05 12:38:08 +02:00
|
|
|
sheet.Title,
|
2020-11-28 04:50:55 +01:00
|
|
|
display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf),
|
2020-11-28 17:18:16 +01:00
|
|
|
)
|
2020-11-27 22:39:34 +01:00
|
|
|
|
|
|
|
// apply colorization if requested
|
|
|
|
if conf.Color(opts) {
|
|
|
|
sheet.Colorize(conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// display the cheatsheet
|
2020-11-28 17:18:16 +01:00
|
|
|
out += display.Indent(sheet.Text) + "\n"
|
2020-11-27 22:39:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 17:18:16 +01:00
|
|
|
// display and exit
|
|
|
|
display.Write(strings.TrimSuffix(out, "\n"), conf)
|
2020-11-27 22:39:34 +01:00
|
|
|
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)
|
2019-10-20 16:02:28 +02:00
|
|
|
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)
|
2020-06-26 00:38:03 +02:00
|
|
|
os.Exit(2)
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 22:11:15 +01:00
|
|
|
// apply colorization if requested
|
|
|
|
if conf.Color(opts) {
|
|
|
|
sheet.Colorize(conf)
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 22:11:15 +01:00
|
|
|
// display the cheatsheet
|
2020-11-28 04:35:24 +01:00
|
|
|
display.Write(sheet.Text, conf)
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|