Skip to content

Support searching gists #9704

@heaths

Description

@heaths

Describe the feature or problem you’d like to solve

I use gists for a lot of one-offs: showing people examples of something, storing my own https://play.rust-lang.org gists (so you can edit them), and so much more. The number is untenable, but GitHub offers no way to search them while limiting to some scope e.g., user. I would like a way to search them, and I think others might as well (hence not going straight to an extension).

Proposed solution

Add gh gist search with options to search all (default), --public, or --secret (mirroring gh gist list), and to "grep" just the description, file names, or file contents. A GraphQL query will be built to pull only what is needed, but search will page through all gists.

Examples:

gh gist search 'foo' # find all gists with "foo" in the description
gh gist search 'foo?' --public --filename # find all public gists with "fo" or "foo" in the description or filenames
gh gist search 'foo|bar' --secret --filename --code # find all secret gists with "foo" or "bar" in the description, filenames, or file content

Here I use --filename and --code to mirror some other command parameters in gh already. I think we should support regex from the start using Go's package (not great - no look-arounds, IIRC - but decent) so people aren't hampered when searching for code and don't request it later, creating a possible compatibility issue down the road.

Because the web site already has a search, I'm initially thinking this is scoped to a user. By default, it's effectively @me but you could pass --author {name} to scope to a different author's gists. IMO, we shouldn't error if --secret is specified since we tightly couple the code but could. The service won't return any results.

As for rendered output, this gets trickier. Ideally, gist search would show highlights of the search pattern; however, with the current table format of gist list we can't show multiple file names and certainly not content. Showing multiple file/names for possibly multiple gists would be hard to read. We could mimic the output of gh search code but add another level like:

{gist ID}  {gist description}
  {filename}
    {first or all found search pattern with a line or two above and below for context}

Of course, we'd have to handle highlighting the matches and context lines. Not hard, but starts to bloat the CLI even more.

Instead, we could just list the gists that contain the pattern found in whatever options they specify. They could use existing gist commands to view more. At least in my use cases, I generally know what I'm looking for - and always put a decent description in to help find things - to showing the gist ID and description would satisfy my use case. Curious about others'.

Alternative

Alternatively, we could add a gist subcommand to gh search but I know from experience in the code that all the plumbing for search is very different from what this command would do, so it may not be worth it. At the very least, it would completely bifurcate the code path.

Additional context

Currently, gh gist list | grep 'something' is about as close as you can get and that will only search the view fields you return including the ID (repo name, which is a GUID; and useless to search), the description, and other fields that are useless to search.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementa request to improve CLIgh-gistrelating to the gh gist commandgh-searchrelating to the gh search commandhelp wantedContributions welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions