|
| 1 | +local has_telescope, telescope = pcall(require, "telescope") |
| 2 | + |
| 3 | +if not has_telescope then |
| 4 | + return |
| 5 | +end |
| 6 | + |
| 7 | +local rest = require("rest-nvim") |
| 8 | + |
| 9 | +local state = require("telescope.actions.state") |
| 10 | + |
| 11 | +local action_state = require("telescope.actions.state") |
| 12 | +local actions = require("telescope.actions") |
| 13 | +local finders = require("telescope.finders") |
| 14 | +local pickers = require("telescope.pickers") |
| 15 | +local conf = require("telescope.config").values |
| 16 | + |
| 17 | +local config = require("rest-nvim.config") |
| 18 | + |
| 19 | +local function rest_env_select(opt) |
| 20 | + local pattern = config.get("env_pattern") |
| 21 | + local edit = config.get("env_edit_command") |
| 22 | + |
| 23 | + local command = string.format("fd -H '%s'", pattern) |
| 24 | + local result = io.popen(command):read("*a") |
| 25 | + |
| 26 | + local lines = {} |
| 27 | + for line in result:gmatch("[^\r\n]+") do |
| 28 | + table.insert(lines, line) |
| 29 | + end |
| 30 | + |
| 31 | + pickers |
| 32 | + .new({}, { |
| 33 | + prompt_title = "Select Evn", |
| 34 | + finder = finders.new_table({ |
| 35 | + results = lines, |
| 36 | + }), |
| 37 | + attach_mappings = function(prompt_bufnr, map) |
| 38 | + actions.select_default:replace(function() |
| 39 | + local selection = action_state.get_selected_entry() |
| 40 | + if selection == nil then |
| 41 | + return |
| 42 | + end |
| 43 | + actions.close(prompt_bufnr) |
| 44 | + rest.select_env(selection[1]) |
| 45 | + end) |
| 46 | + map("i", "<c-o>", function() |
| 47 | + actions.close(prompt_bufnr) |
| 48 | + local selection = state.get_selected_entry(prompt_bufnr) |
| 49 | + if selection == nil then |
| 50 | + return |
| 51 | + end |
| 52 | + vim.api.nvim_command(edit .. " " .. selection[1]) |
| 53 | + end) |
| 54 | + return true |
| 55 | + end, |
| 56 | + previewer = conf.grep_previewer({}), |
| 57 | + }) |
| 58 | + :find() |
| 59 | +end |
| 60 | + |
| 61 | +return telescope.register_extension({ |
| 62 | + exports = { |
| 63 | + select_env = rest_env_select, |
| 64 | + }, |
| 65 | +}) |
0 commit comments