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

telescope tries to show preview for binary files #223

Closed
ElPiloto opened this issue Nov 5, 2020 · 6 comments · Fixed by #224
Closed

telescope tries to show preview for binary files #223

ElPiloto opened this issue Nov 5, 2020 · 6 comments · Fixed by #224

Comments

@ElPiloto
Copy link

ElPiloto commented Nov 5, 2020

Would be helpful if telescope knew not to try to preview arbitrary binary files as it leaves a bunch of garbage characters on the screen. I've seen this functionality implemented by checking XDG MIME type, but unclear how general that solution can be on all platforms.

@Conni2461
Copy link
Member

Hey thanks for the report. Just to be clear this only happens when cat is used?!
I got a fix up and running. Could you test #224

@ElPiloto
Copy link
Author

ElPiloto commented Nov 5, 2020

Yep, it worked: https://snipboard.io/S0qDzF.jpg Although maybe you don't want that [Process exited 0] bit there. Either way, thanks for the quick fix 😃

@Conni2461
Copy link
Member

That is actually a thing of termopen, which is use to connect a "pseudo-terminal session" to a buffer (see :help termopen). When a command is done [Process exited 0] is printed, with the corresponding exit code. So previewing a small file with cat will also show this.

@ElPiloto
Copy link
Author

ElPiloto commented Nov 5, 2020

Oh, cool. TIL.

@gbrlsnchs
Copy link

This is happening again, now when using the tree-sitter preview. Depending on the size of the binary file, Neovim completely freezes.

@Conni2461
Copy link
Member

Yes i know because i haven't found the time to write a good implementation. I thought we should rely on a platform independent binary detection in lua. A way to configure this would be (because you can override the filereading):

local previewers = require('telescope.previewers')
local Job = require('plenary.job')
local new_maker = function(filepath, bufnr, opts)
  filepath = vim.fn.expand(filepath)
  Job:new({
    command = 'file',
    args = { '--mime-type', '-b', filepath },
    on_exit = function(j)
      local mime_type = vim.split(j:result()[1], '/')[1]
      if mime_type == "text" then
        previewers.buffer_previewer_maker(filepath, bufnr, opts)
      else
        -- maybe we want to write something to the buffer here
        vim.schedule(function()
          vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { 'BINARY' })
        end)
      end
    end
  }):sync()
end

require('telescope').setup {
  defaults = {
    buffer_previewer_maker = new_maker,
  }
}

I wanted to add some other things to buffer_previewer_maker so i might end up using this method and caching if file is binary or not. At least per picker run. So if i select the same file twice it will not run file --mime-type - b filepath twice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants