Skip to content

Use the most speciffic cheat files first, when using --all (display many cheat files) #787

@danieldc

Description

@danieldc

cheat allows many cheapaths, and the order in the config file is significant: the last cheatpaths take precedence over the earlier ones, so when there are many matching cheat files, the one that is displayed is the one in the latter cheatpath. This makes it possible to config a public and a private cheatpath, and list the private one at the end, thus a private cheats will always override the public one when a match is found in both of them.

chat also added an option to display all matching cheat files, via --all CLI parameter. However, the cheats are displayed in the cheatpaths listed order, which means the less important cheatfile will be displayed first rather than last. To use the example above, the private match will be displayed last (after the less important public one) rather than first (before the public one).

This means cheat foo displays the private foo cheat, and cheat -a foo displays the public one followed by the private one, rather than the private one first.

The fix is very localized and trivial: when using --alll, reverse the list so that any subsequent processing is done in priority order.

        // if --all was passed, display cheatsheets from all cheatpaths
        allFlag, _ := cmd.Flags().GetBool("all")
        if allFlag {
+               for i, j := 0, len(cheatsheets)-1; i < j; i, j = i+1, j-1 {
+                       cheatsheets[i], cheatsheets[j] = cheatsheets[j], cheatsheets[i]
+               }
+
                // iterate over the cheatpaths
                out := ""
                for _, cheatpath := range cheatsheets {

Would it be possible to make the above change in the mainline code base?

This makes it more consistent with the current spirit (latter cheatpaths override sooner ones, hence they are more important).
Furthermore, I think other people would expect this too: see the last comment that lead to implementation of --all in issue #548.

(happy to do a PR if you would prefer that, I put the code inline since is trivial and PRs are currently not accepted)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions