nvim-telescope / telescope.nvim

Find, Filter, Preview, Pick. All lua, all the time.
MIT License
15.81k stars 833 forks source link

how to open file by telescope.nvim and pipe to glow.nvim #1179

Closed ayoubelmhamdi closed 3 years ago

ayoubelmhamdi commented 3 years ago

I would use this plugin to pick a file and open it by glow.nvim (MarkdownPreview) like this:

enter image description here

kkharji commented 3 years ago

hmmmmm you can do something like this

local action_set = require "telescope.actions.set"
local actions = require "telescope.actions"

local glowPreviewer = function() 
  return require'telescope.builtin'.fd {
      attach_mappings = function(prompt_bufnr)
      action_set.select:replace(function(_, cmd)
        local selection = action_state.get_selected_entry()
        actions.close(prompt_bufnr)
        if cmd == "default" or cmd == "horizontal" then
          vim.cmd("term glow " .. selection.value)
        elseif cmd == "vertical" then
          vim.cmd("vs | term glow " .. selection.value)
      end)
      return true
    end,
}
end

If you want more and would like to preview with glow on telescope buffer.

local previewers = require('telescope.previewers')
local glow_previewer = function() 
  return require'telescope.builtin'.fd {
    previewer = previewers.new_termopen_previewer {
       get_command = function(selection)
         return { 'glow',  selection.value }
      end
   }
}
end

Screen Shot 2021-08-28 at 2 44 11 AM

BenCzu commented 1 month ago

Just want to follow up on this issue. I am new to configuring telescope.

How would you actually go about applying this custom previewer to the builtin.find_files but only for markdown files. Just wondering what the best way to implement this would be?

I have tried to find a resource on how do this, but no luck.

Some guidance would be much appreciated

jamestrew commented 1 month ago

To use glow.nvim, could probably utilize the TelescopePreviewLoaded autocommand event.

I haven't tested this and I barely know anything about glow but maybe something like this could work?

vim.api.nvim_create_autocmd("User", {
  pattern = "TelescopePreviewerLoaded",
  callback = function(args)
    if args.data.filetype == "markdown" then
      vim.cmd("Glow")
    end
  end,
})
BenCzu commented 1 month ago

Just tried this auto-command and found that it throws an error saying: attempt to index field 'data' (a nil value). From the "args" table.

Is there a simple way of implementing @kkharji 's code:

local previewers = require('telescope.previewers')
local glow_previewer = function() 
  return require'telescope.builtin'.fd {
    previewer = previewers.new_termopen_previewer {
       get_command = function(selection)
         return { 'glow',  selection.value }
      end
   }
}
end

into my telescope configuration?

How would I setup my telescope previewer to run this function?

I just don't know how to setup up custom previewers in telescope. (Maybe I should ask this in telescope.nvim repo)