local pickers = require("telescope.pickers") local finders = require("telescope.finders") local conf = require("telescope.config").values local actions = require("telescope.actions") local action_state = require("telescope.actions.state") local utils = require("telescope.utils") local entry_display = require "telescope.pickers.entry_display" local M = {} M.setup = function(opts) opts = opts or {} -- TODO: get report directory from settings or maybe reportctl ini file? M.report_dir = opts.report_dir or "~/reports" -- do nothing end -- Function to get directories in the current directory M.get_reports = function() local report_dir = M.report_dir or "~/reports" -- insecure local handle = io.popen("find " .. report_dir .. " -maxdepth 2 -mindepth 2 -type d") if not handle then return {} end local result = handle:read("*a") handle:close() return vim.split(result, "\n", { trimempty = true }) end local debug = function(msg, title) msg = msg or "" title = title or "" vim.notify(msg, "debug", { style = "compact", timeout = 10000, title = title }) end local info = function(msg, title) msg = msg or "" title = title or "" vim.notify(msg, "info", { style = "compact", timeout = 10000, title = title }) end M.directory_picker = function(opts) opts = opts or {} local dirs = M.get_reports() local displayer = entry_display.create({ separator = "", items = { { width = 25 }, { remaining = true }, } }) local make_display = function(e) -- debug(vim.inspect(e.value)) local parts = vim.split(e.value, '/', { trimempty = true, plain = true }) local kunde = parts[#parts - 1] local report = parts[#parts] local display = kunde .. " " .. report local kundeStart = 0 local kundeEnd = #kunde local reportStart = kundeEnd + 1 local reportEnd = reportStart + #report return displayer { { kunde, "TelescopeResultsComment" }, { report, "TelescopeResultsIdentifier" }, } end local reportFinder = finders.new_table({ results = dirs, entry_maker = function(entry) return { value = entry, ordinal = entry, display = make_display, } end }) pickers.new(opts, { prompt_title = "Select report", finder = reportFinder, sorter = conf.generic_sorter(opts), attach_mappings = function(prompt_bufnr, map) local function set_cwd() local selection = action_state.get_selected_entry() if selection then local selected_dir = selection.value vim.cmd("cd " .. selected_dir) info(selected_dir, "Switched report") end actions.close(prompt_bufnr) end map("i", "", set_cwd) map("n", "", set_cwd) return true end, }):find() end return M