Skip to content

Instantly share code, notes, and snippets.

@navicore
Created December 6, 2024 00:55
Show Gist options
  • Save navicore/02fed60204d4ee92b3576c473dd836cd to your computer and use it in GitHub Desktop.
Save navicore/02fed60204d4ee92b3576c473dd836cd to your computer and use it in GitHub Desktop.
fun neovim lua telescope demo from Developer Voices https://www.youtube.com/watch?v=HXABdG3xJW4
local pickers = require('telescope.pickers')
local config = require('telescope.config').values
local finders = require('telescope.finders')
local previewers = require('telescope.previewers')
local utils = require('telescope.previewers.utils')
local actions = require('telescope.actions')
local actions_state = require('telescope.actions.state')
local log = require('plenary.log'):new()
log.level = 'debug'
local M = {}
local function flatten(tbl)
local result = {}
for _, v in ipairs(tbl) do
if type(v) == "table" then
for _, v2 in ipairs(flatten(v)) do
table.insert(result, v2)
end
else
table.insert(result, v)
end
end
return result
end
M.show_docker_images = function(opts)
pickers.new(opts, {
finder = finders.new_async_job({
command_generator = function()
return { "docker", "images", "--format", "json" }
end,
entry_maker = function(entry)
local parsed = vim.json.decode(entry)
if parsed == nil then
return
end
return {
value = parsed,
display = parsed.Repository,
ordinal = parsed.Repository .. ':' .. parsed.Tag,
}
end,
}),
sorter = config.generic_sorter(opts),
previewer = previewers.new_buffer_previewer({
title = 'Docker Image Details',
define_preview = function(self, entry)
vim.api.nvim_buf_set_lines(
self.state.bufnr,
0,
0,
true,
flatten({
"# " .. entry.value.ID,
"",
"```lua",
vim.split(vim.inspect(entry.value), "\n"),
"```",
})
)
utils.highlighter(self.state.bufnr, "markdown")
end
}),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local selection = actions_state.get_selected_entry()
actions.close(prompt_bufnr)
local command = {
"edit",
"term://docker",
"run",
"-it",
selection.value.Repository,
}
vim.cmd(vim.fn.join(command, ' '))
end)
return true
end
}):find()
end
M.show_docker_images()
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment