Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colored output on grep_string? #1240

Open
fsouza opened this issue Sep 13, 2021 · 11 comments
Open

Colored output on grep_string? #1240

fsouza opened this issue Sep 13, 2021 · 11 comments
Labels
enhancement Enhancement to performance, inner workings or existent features

Comments

@fsouza
Copy link

fsouza commented Sep 13, 2021

Is your feature request related to a problem? Please describe.

Hi there, finally came around to adopt telescope and one thing that I miss from my fzf days is the ability to filter colored output from from rg.

If I pass --color=always to rg, telescope doesn't display any results, and I assume that filtering on ansi codes isn't really supported?

Describe the solution you'd like

I'd like to be able to invoke grep_string() and process colored output from rg.

Describe alternatives you've considered

No colors? 🙈

Additional context

If this is something yall think makes sense, I can try to send a PR.

@fsouza fsouza added the enhancement Enhancement to performance, inner workings or existent features label Sep 13, 2021
@Conni2461
Copy link
Member

PR that wants to do that: #670

currently its not supported because then we would need to parse the shell color codes. I send @jesseleite a code snippet (a term color code parser) that a wrote some time ago, with that we could realize that. Maybe i should share this snippet and someone else can pick it up, i know he is very busy lately with a newborn :)

@fdschmidt93
Copy link
Member

fdschmidt93 commented Sep 14, 2021

Unsolicited and uninformed side comment: if that requires rg --json neovim/neovim#14871 would be great.

@TC72
Copy link
Contributor

TC72 commented Sep 15, 2021

Hey @Conni2461, I've been speaking to @jesseleite and wanted to try to help out with this. Could you please post the snippet for the color parser?

@Conni2461
Copy link
Member

json would be easier to implement no doubt. But we need to see how fast it will be in the end, when its done. We need to be able to run it in :help api-fast otherwise its useless for us. From what i've seen from this PR this should be possible because we will get a vim.json.* interface. None the less for now i think parsing color codes would be a good enough solution. We later can benchmark that version with a json version and decide which one we want to keep.

I share both the gist + my conversation because it contains my thoughts on how to solve this issue.
https://gist.github.com/Conni2461/6f0c9353b83ace6a98b2793d0b6fd6d1

image
image

@Conni2461
Copy link
Member

I suggest that you only tackle the highlight changes and not everything from raw_live_grep pr. Splitting it up and doing it in small chunks might probably be the better solution for this. :)

@TC72
Copy link
Contributor

TC72 commented Sep 16, 2021

@Conni2461 that's great and my tele-tabby.nvim extension already deals with highlighting without columns.
Probably not the perfect solution for this but at least I have some understanding of doing it before.

@TC72
Copy link
Contributor

TC72 commented Sep 20, 2021

@Conni2461
I've got a working version of this but it still needs some work.
I've asked @jesseleite about refactoring the code for live_grep_raw into an extension which'll make it much easier to work on and share progress. (but in the end it should definitely be builtin to telescope.)

I have a few issues right now.

  1. How to handle devicons being added to the start of the entry. Can I assume these will always take up 3 characters?
  2. Right now I've added an offset set to 3 which gets passed to interptet_termcodes(content,offset) to add this offset to every highlight's start and end position. Is there another way to do this, are there any utils built in to add an offset to a table of highlights?
  3. How to map rg colors onto Telescope highlights. There are colors for filename, line number and each match. Should this be done as opts for gen_from_vimgrep allowing the user to chose or are there obvious choices to use, such as TelescopeMatching for matches?
  4. My way of doing this inside the parse() function used by gen_from_vimgrep seems to have broken things. I don't see the correct number of results in the prompt and changing the prompt has strange behaviour, like adding results again so they're duplicated. I'm obviously doing something wrong.
  5. Can we use --max-columns and max-columns-preview to improve performance?

@Conni2461
Copy link
Member

offset seems like a good idea, we can then calculate that one dynamically.

Right now lets make those highlight groups const. Later we can say that you can change it via opts.

But all of this is easier to discuss with a WIP PR :)

@TC72
Copy link
Contributor

TC72 commented Sep 20, 2021

Sorry to ask such beginner questions but what's the best way to work on these kind of things?
Should I be configuring Neovim to use my fork of Telescope, with something like:

Plug 'https://github.com/TC72/telescope.nvim.git', { 'as': 'telescope.nvim','branch': 'my_branch' }

or should i try to package things as extensions where possible to avoid conflicts?

Feels like it might be time to put on my big boy pants and learn to use git properly.

@TC72
Copy link
Contributor

TC72 commented Sep 24, 2021

I've submitted a PR, #1281.

@Conni2461 I wasn't sure where to put your color_conv.lua file so it's just in the lua/telescope directory.

I made a few changes to the color_conv code. I changed the color code matching to use the full escape code '^[%['. Also increased the length of the match to be 2 characters. The format of the highlights used by entries is also slightly different and doesn't need lines.

Finally I'm passing in my own function from the entry maker to match color codes to Telescope Highlighting. For now it's just matching these:

    local ansi_to_hl = {
            [1] = 'TelescopeMatching',
            [2] = 'TelescopeResultsLineNr',
            [5] = 'TelescopeResultsIdentifier',
    }

@TC72
Copy link
Contributor

TC72 commented Sep 28, 2021

I've fixed the issue with Lua icons, this code is now working correctly and coloring rg results.

CleanShot 2021-09-28 at 23 19 18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement to performance, inner workings or existent features
Projects
None yet
Development

No branches or pull requests

4 participants