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

[BUG] typescript (ts) files are not previewed with custom mime_hook for images #3068

Closed
pinarruiz opened this issue Apr 22, 2024 · 7 comments · Fixed by #3083
Closed

[BUG] typescript (ts) files are not previewed with custom mime_hook for images #3068

pinarruiz opened this issue Apr 22, 2024 · 7 comments · Fixed by #3083
Labels
bug Something isn't working

Comments

@pinarruiz
Copy link

pinarruiz commented Apr 22, 2024

Description

Setting up the mimehook to the example shown in Configuration-Recipes#use-terminal-image-viewer-to-preview-images makes ts files not being rendered

Neovim version

NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1693350652

Operating system and version

NixOs unstable

Telescope version / branch / rev

1-unstable-2024-04-21

checkhealth telescope

telescope: require("telescope.health").check()

Checking for required plugins ~
- OK plenary installed.
- OK nvim-treesitter installed.

Checking external dependencies ~
- OK rg: found ripgrep 14.1.0
- OK fd: found fd 8.7.1

===== Installed extensions ===== ~

Telescope Extension: `fzf` ~
- OK lib working as expected
- OK file_sorter correctly configured
- OK generic_sorter correctly configured

Telescope Extension: `media_files` ~
- No healthcheck provided

Telescope Extension: `ui-select` ~
- No healthcheck provided

Telescope Extension: `undo` ~
- No healthcheck provided

Steps to reproduce

Telescope find_files hidden=True

Expected behavior

No response

Actual behavior

image

Minimal config

function(filepath, bufnr, opts)
  local get_ext = function(filepath)
    local split_path = vim.split(filepath:lower(), '.', {plain=true})
    return split_path[#split_path]
  end
  local is_image = function(filepath)
    local image_extensions = {'png','jpg'}   -- Supported image formats
    return vim.tbl_contains(image_extensions, get_ext(filepath))
  end
  if is_image(filepath) then
    local term = vim.api.nvim_open_term(bufnr, {})
    local function send_output(_, data, _ )
      for _, d in ipairs(data) do
        vim.api.nvim_chan_send(term, d..'\r\n')
      end
    end
    vim.fn.jobstart({
      'chafa', filepath  -- Terminal image viewer command
    }, {on_stdout=send_output, stdout_buffered=true, pty=true})
  else
    require("telescope.previewers.utils").set_preview_message(bufnr, opts.winid, "Binary cannot be previewed (" .. get_ext(filepath) .. ")")
  end
end

Edit: Format

@pinarruiz pinarruiz added the bug Something isn't working label Apr 22, 2024
@jamestrew
Copy link
Contributor

Are you sure it's the image preview code that's affecting ts file previews?
ts file, or really any text files in don't reach the mime_hook code path so having this image preview hook shouldn't matter.

I'm also not able to reproduce this. I can get a ts file to show its preview.

img-preview.mp4

This was done using this minimal config. Granted I switched out chafa for catimg.

min.lua
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.uv.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  {
    "nvim-telescope/telescope.nvim",
    dependencies = {
      "nvim-lua/plenary.nvim",
      "nvim-tree/nvim-web-devicons",
    },
    config = function()
      require("telescope").setup({
        defaults = {
          preview = {
            mime_hook = function(filepath, bufnr, opts)
              local get_ext = function(filepath)
                local split_path = vim.split(filepath:lower(), ".", { plain = true })
                return split_path[#split_path]
              end
              local is_image = function(filepath)
                local image_extensions = { "png", "jpg" } -- Supported image formats
                return vim.tbl_contains(image_extensions, get_ext(filepath))
              end
              if is_image(filepath) then
                local term = vim.api.nvim_open_term(bufnr, {})
                local function send_output(a, data, c)
                  for _, d in ipairs(data) do
                    vim.api.nvim_chan_send(term, d .. "\r\n")
                  end
                end
                vim.fn.jobstart({
                  "catimg",
                  -- "chafa,"
                  filepath, -- Terminal image viewer command
                }, { on_stdout = send_output, stdout_buffered = true, pty = true })
              else
                require("telescope.previewers.utils").set_preview_message(
                bufnr,
                opts.winid,
                "Binary cannot be previewed (" .. get_ext(filepath) .. ")"
                )
              end
            end,
          },
        },
      })
    end,
  },
}

require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

I'd like you to try this minimal config.
This also could be like a Neovim installation issue or something if the minimal config is still giving you issues. Telescope is failing to get the filetype of the typescript file through various mechanisms. The main one being Neovim's built-in vim.filetype.match function. There's been cases in the past where a botched install would cause something like this and reinstalling neovim helped.

@pinarruiz
Copy link
Author

Hi @jamestrew,

I cannot try it right now, as i am answering from my phone, but I will try asap.

Are you sure it's the image preview code that's affecting ts file previews?

Removing the mimehook makes the ts files preview return, also if you notice on the screenshot that I provided the extension is present on the Binary cannot be previewed message, and since this extension on the message was added by me, I am sure that the mime hook is somehow affectinc the ts files preview.

@jamestrew
Copy link
Contributor

I am sure that the mime hook is somehow affectinc the ts files preview.

That's strange. Seems to me like a couple of things have to go very wrong for this to be the case..

  1. vim.filetype.match fails to identify the file type based on filepath -> I just learned that vim.filetype.match for the .ts extension doesn't return a match so I guess this condition passes
  2. file --mime-type -b <filepath> also thinks that your typescript files are not a text/inode/json file

local output = capture(string.format([[file --mime-type -b "%s"]], filepath))
local mime_type = vim.split(output, "/")
if mime_type[1] ~= "text" and mime_type[1] ~= "inode" and mime_type[2] ~= "json" then
if type(opts.preview.mime_hook) == "function" then
opts.preview.mime_hook(filepath, bufnr, opts)

I'd ask you to try the file command in your terminal against one of the ts files. Maybe we're getting some result that telescope isn't expecting. In which case, we can try to fix it.

@pinarruiz
Copy link
Author

Running against the same file that is on the screenshot:

$ file --mime-type -b en.ts
application/javascript

Doing the same against a tsx file returns the same result, weird:

$ file --mime-type -b index.tsx
application/javascript

@jamestrew
Copy link
Contributor

Interesting... never really questioned how file determines the mime type but a simple "hello world" typescript file gives text/plain for me but proper production ts file gives application/javascript.

We can fix this though.

@jamestrew
Copy link
Contributor

If we can give the linked PR a try, that would be helpful.

@pinarruiz
Copy link
Author

Just tried it and its working now:
image
It was blurred for privacy reasons.

Thanks for that, cant wait for it to merge 😄, just to confirm, without #3083 it does not work still:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants