diff --git a/cmd/cheat/cmd_view.go b/cmd/cheat/cmd_view.go index 2f4c550..42a61f9 100644 --- a/cmd/cheat/cmd_view.go +++ b/cmd/cheat/cmd_view.go @@ -30,9 +30,37 @@ func cmdView(opts map[string]interface{}, conf config.Config) { ) } - // consolidate the cheatsheets found on all paths into a single map of - // `title` => `sheet` (ie, allow more local cheatsheets to override less - // local cheatsheets) + // if --all was passed, display cheatsheets from all cheatpaths + if opts["--all"].(bool) { + // iterate over the cheatpaths + for _, cheatpath := range cheatsheets { + + // if the cheatpath contains the specified cheatsheet, display it + if sheet, ok := cheatpath[cheatsheet]; ok { + + // identify the matching cheatsheet + fmt.Println(fmt.Sprintf("%s %s", + display.Underline(sheet.Title), + display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath)), + )) + + // apply colorization if requested + if conf.Color(opts) { + sheet.Colorize(conf) + } + + // display the cheatsheet + display.Display(display.Indent(sheet.Text), conf) + } + } + + // exit early + 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 diff --git a/cmd/cheat/docopt.txt b/cmd/cheat/docopt.txt index c841549..e12615e 100644 --- a/cmd/cheat/docopt.txt +++ b/cmd/cheat/docopt.txt @@ -3,11 +3,12 @@ Usage: Options: --init Write a default config file to stdout + -a --all Search among all cheatpaths -c --colorize Colorize output -d --directories List cheatsheet directories -e --edit= Edit -l --list List cheatsheets - -p --path= Return only sheets found on path + -p --path= Return only sheets found on cheatpath -r --regex Treat search as a regex -s --search= Search cheatsheets for -t --tag= Return only sheets matching diff --git a/cmd/cheat/str_usage.go b/cmd/cheat/str_usage.go index 86be7ea..0d69c57 100644 --- a/cmd/cheat/str_usage.go +++ b/cmd/cheat/str_usage.go @@ -12,11 +12,12 @@ func usage() string { Options: --init Write a default config file to stdout + -a --all Search among all cheatpaths -c --colorize Colorize output -d --directories List cheatsheet directories -e --edit= Edit -l --list List cheatsheets - -p --path= Return only sheets found on path + -p --path= Return only sheets found on cheatpath -r --regex Treat search as a regex -s --search= Search cheatsheets for -t --tag= Return only sheets matching diff --git a/internal/sheet/copy_test.go b/internal/sheet/copy_test.go index 63915dc..1cd868b 100644 --- a/internal/sheet/copy_test.go +++ b/internal/sheet/copy_test.go @@ -25,7 +25,7 @@ func TestCopyFlat(t *testing.T) { } // mock a cheatsheet struct - sheet, err := New("foo", src.Name(), []string{}, false) + sheet, err := New("foo", "community", src.Name(), []string{}, false) if err != nil { t.Errorf("failed to init cheatsheet: %v", err) } @@ -72,7 +72,13 @@ func TestCopyDeep(t *testing.T) { } // mock a cheatsheet struct - sheet, err := New("/cheat-tests/alpha/bravo/foo", src.Name(), []string{}, false) + sheet, err := New( + "/cheat-tests/alpha/bravo/foo", + "community", + src.Name(), + []string{}, + false, + ) if err != nil { t.Errorf("failed to init cheatsheet: %v", err) } diff --git a/internal/sheet/sheet.go b/internal/sheet/sheet.go index 9d6ade1..4471401 100644 --- a/internal/sheet/sheet.go +++ b/internal/sheet/sheet.go @@ -10,17 +10,19 @@ import ( // Sheet encapsulates sheet information type Sheet struct { - Title string - Path string - Text string - Tags []string - Syntax string - ReadOnly bool + Title string + CheatPath string + Path string + Text string + Tags []string + Syntax string + ReadOnly bool } // New initializes a new Sheet func New( title string, + cheatpath string, path string, tags []string, readOnly bool, @@ -46,11 +48,12 @@ func New( // initialize and return a sheet return Sheet{ - Title: title, - Path: path, - Text: text + "\n", - Tags: tags, - Syntax: fm.Syntax, - ReadOnly: readOnly, + Title: title, + CheatPath: cheatpath, + Path: path, + Text: text + "\n", + Tags: tags, + Syntax: fm.Syntax, + ReadOnly: readOnly, }, nil } diff --git a/internal/sheet/sheet_test.go b/internal/sheet/sheet_test.go index 6ab3ad2..fd69eb1 100644 --- a/internal/sheet/sheet_test.go +++ b/internal/sheet/sheet_test.go @@ -13,6 +13,7 @@ func TestSheetSuccess(t *testing.T) { // initialize a sheet sheet, err := New( "foo", + "community", mock.Path("sheet/foo"), []string{"alpha", "bravo"}, false, @@ -61,6 +62,7 @@ func TestSheetFailure(t *testing.T) { // initialize a sheet _, err := New( "foo", + "community", mock.Path("/does-not-exist"), []string{"alpha", "bravo"}, false, @@ -77,6 +79,7 @@ func TestSheetFrontMatterFailure(t *testing.T) { // initialize a sheet _, err := New( "foo", + "community", mock.Path("sheet/bad-fm"), []string{"alpha", "bravo"}, false, diff --git a/internal/sheets/load.go b/internal/sheets/load.go index 9ea85fb..409f505 100644 --- a/internal/sheets/load.go +++ b/internal/sheets/load.go @@ -59,7 +59,13 @@ func Load(cheatpaths []cp.Cheatpath) ([]map[string]sheet.Sheet, error) { } // parse the cheatsheet file into a `sheet` struct - s, err := sheet.New(title, path, cheatpath.Tags, cheatpath.ReadOnly) + s, err := sheet.New( + title, + cheatpath.Name, + path, + cheatpath.Tags, + cheatpath.ReadOnly, + ) if err != nil { return fmt.Errorf( "failed to load sheet: %s, path: %s, err: %v",