Skip to content

Support all flavors of search via gh search <foo>  #6945

Description

@nr-dbuckwalter

Describe the feature or problem you’d like to solve

Support all flavors of gh search <foo> as listed in the API. For example, gh search code 'somestring' would be useful from the CLI.

Proposed solution

Will provide a more consistent experience for search via CLI.

Additional context

$ gh version
gh version 2.22.1 (2023-01-27)
https://github.com/cli/cli/releases/tag/v2.22.1

gh api -X GET search/code -f q='somestring' is a hacky workaround

Activity

  1. samcoe commented on Feb 1, 2023

    @samcoe
    Contributor

    @nr-dbuckwalter Is your feature request specifically around code search? If so please checkout #6931 and the discussion there. We are open to adding other forms of search that we do not currently support as well. I would say that each form should get its own issue though instead of a single issue capturing all the flavors of search.

  2. added
    more-info-neededMore info needed from user/contributor
    and removed on Feb 1, 2023
  3. nr-dbuckwalter commented on Feb 1, 2023

    @nr-dbuckwalter
    Author

    I looked in open issues only for prior art, oops! 😅
    Code search consistent with the other functions the CLI already provides is the feature I was looking for. I use the CLI with GitHub Enterprise Server, so I don't think the forward-looking statements in #6931 about 'better search is coming (someday)' apply to me the same way.

  4. samcoe commented on Feb 1, 2023

    @samcoe
    Contributor

    I think that is fair point that waiting for the new code search on GHES has a whole different timeline. I don't know if/when it will be released on GHES. I will discuss with our team and see if we want to address this feature sooner for our GHES users.

  5. added
    discussFeature changes that require discussion primarily among the GitHub CLI team
    and removed
    more-info-neededMore info needed from user/contributor
    on Feb 1, 2023
  6. isaitgirl commented on Feb 27, 2023

    @isaitgirl

    I didn't find a way to use gh search repos <foo> where <foo> is the exact name of repo, even when using --match name, for example. It always get unwanted results.... searching for:

    gh search repos --include-forks only --match name `My-Repo`
    

    Returns forks for My-Other-Repo and My-Repo.

    Maybe I'm missing something...

    GH version: gh version 2.23.0 (2023-02-07) (installed via homebrew)

    Isadora

  7. vilmibm commented on Feb 27, 2023

    @vilmibm
    Contributor

    @isaitgirl hello! this issue is about adding new search types to gh search and it sounds like you have a question about gh search repos specifically -- could you open a new issue or discussion to track your comment?

    as far as supporting the current code search API, we'd be in favor of merging a PR adding such support. We don't have a super clear timeline of when the new code search stuff is coming and in the interim there is value in having a code search affordance in gh.

    Unfortunately there is no room on the core team to pick up this kind of work right now, so I'm marking help wanted.

    In terms of implementation and UX, gh search code should be similar enough to the existing code for the search types for someone to start by taking inspiration from there; let us know what questions arise.

  8. added and removed
    discussFeature changes that require discussion primarily among the GitHub CLI team
    on Feb 27, 2023
  9. joshkraft commented on Mar 26, 2023

    @joshkraft
    Contributor

    Hello @vilmibm 👋 I am interested in working on this, but noticed this blog post: Changes to the code search API. With these changes coming on April 10th, is there still value in adding functionality here or would it be better to wait for the changes to the code search API to be released?

  10. samcoe commented on Mar 26, 2023

    @samcoe
    Contributor

    @joshkraft I think it is okay to get started on this feature and just avoid adding in functionality to sort the search results which exists in other search commands. All the other changes should not have an affect on the implementation. We will probably want to wait till after April 10th to actually release the new search code command to make sure everything still works but that shouldn't block getting this work started.

  11. joshkraft commented on Apr 15, 2023

    @joshkraft
    Contributor

    I've made some progress here but running into a bit of a UX challenge. The code search API works by returning text fragments, as outlined here. The first two matches within each file are returned. For example, if we performed a search for function in this JavaScript file:

    function foo() {
        return bar;
    }
    
    function baz() {
        return qux;
    }

    We might get back something like this:

    {
      "text_matches": [
        {
          "fragment": "function foo() {\nreturn bar;\n}\n\nfunction baz() {\nreturn qux;\n}",
          "matches": [
            {
              "text": "function",
              "indices": [
                0,
                7
              ]
            },
            {
              "text": "function",
              "indices": [
                36,
                43
              ]
            }
          ]
        }
      ]
    }

    I am trying to figure out the best way to display this data within the confines of a row in a table, to be consistent with the other search commands. My first thought was to try to fit each search result into a row and highlight each match by rendering it in a different color. Here is what that might look like:

    gh-search-result

    In this case, the results look alright (to me), but the limited space we are working with means there are lots of situations where the results are not very usable. A few issues I have come across:

    • long fragments or long matches might truncated to a point where the 'area of interest' is cut off
    • compressing fragments that span multiple lines into a single line can result in strange spacing

    I am sure these issues can be improved to a degree, but before I start digging into options I wanted to check if it makes sense to try and make the table approach work, or if I should also explore alternative ways of displaying code search results to users. For example, we could try to mimic the functionality of the web-based code search results, where each fragment is rendered in its entirety with the matches highlighted, though this would probably add a good deal of complexity.

    Another thing I noticed - the --limit parameter limits the number of results returned, but each result can contain 2 matches. A query with a --limit of 2 might yield 2 results, with 2 matches in each result. Should we display all 4 matches to the user, or cut it off at 2 to respect the requested --limit?

  12. vilmibm commented on Apr 18, 2023

    @vilmibm
    Contributor

    The table result looks usable to me; I'd love to see some mock-ups of alternative ideas you have just to see if there's another approach worth considering.

    re: --limit; it's the case that a result has a maximum of 2 matches? If so I'm comfortable with having --limit govern matches and not results. A user should be able to use --limit to add a maximum to the number of rows displayed.

  13. joshkraft commented on Apr 21, 2023

    @joshkraft
    Contributor

    Here are a few possible formats I came up with for displaying the results. They each strike a different balance of brevity and readability. I like the latter two options for more complex queries where the context of the surrounding code is important, but I could see an argument for going the simple route for the CLI given how easy it is for users to open the results in the web and get access to the full Github search functionality for complex queries.

    Option 1: Normal tableprinter table with code compressed into a single cell
    image

    Option 2: tableprinter table with code split across multiple rows
    image

    Option 3: Code blocks rendered full width, similar to the web search results
    image

    I need to do some more testing on the 'matches' behavior. It seems like you can get up to 2 matches per file, but each match might contain the search term multiple times. So we have the ability to apply the limit based on number of overall results, number of matches, or number of appearances of the search term.

  14. mislav commented on Apr 21, 2023

    @mislav
    Contributor

    @joshkraft Thanks for those visualizations! Very useful.

    I personally think of GitHub code search primarily as if grep were operating on some server rather than locally. With that in mind, I think that the default output of code search could be a line-based format, which includes the table prototype you already proposed in this thread.

    Here is how grep displays matches:

    Here is how my favorite search utility ag displays results to a terminal:

    And here is how ag prints results for scripts:

    Of course, these utilities support a lot of flags to control the format of the output (for example, I've activated line numbers for grep output), but whatever the default is, I would vote for a line-based format to be developed first, perhaps with these fields:

    <filepath>:<linenumber>:  <match>
    

    and for terminal/rich output, I would like to see something like ag display for terminals, i.e. a file name is a header and a code snippet is rendered with line numbers. I see no need for rendering a faux GUI with ------- or | for drawing boxes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementa request to improve CLIhelp wantedContributions welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions