feat(telescope): #7 delete session files

main
olimorris 2022-05-13 10:48:13 +01:00
parent db46d40d45
commit 040cf43524
4 changed files with 75 additions and 56 deletions

View File

@ -281,7 +281,8 @@ The plugin comes with a number of commands:
The Telescope extension may be opened via `:Telescope persisted`.
Once opened, the available keymaps are:
* `<CR>` - Source the selected session file
* `<CR>` - Source the session file
* `<C-d>` - Delete the session file
### Lazy loading

View File

@ -1,4 +1,5 @@
local actions_state = require("telescope.actions.state")
local transform_mod = require("telescope.actions.mt").transform_mod
local M = {}
@ -11,7 +12,15 @@ end
---Delete the selected session from disk
--@return string
M.delete_session = function()
print(get_selected_session().file_path)
-- local session = get_selected_session().file_path
local session = get_selected_session()
local path = session.file_path
local confirm = vim.fn.input("Delete " .. session.name .. "? ", "yes"):lower()
if confirm == "yes" or confirm == "y" then
os.remove(path)
print("Session deleted: " .. path)
end
end
return M
return transform_mod(M)

View File

@ -0,0 +1,56 @@
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local entry_display = require("telescope.pickers.entry_display")
local M = {}
M.session_finder = function(sessions)
-- Layout borrowed from:
---https://github.com/LinArcX/telescope-env.nvim/blob/master/lua/telescope/_extensions/env.lua
local cols = vim.o.columns
local telescope_width = conf.width
or conf.layout_config.width
or conf.layout_config[conf.layout_strategy].width
or cols
if type(telescope_width) == "function" then
telescope_width = telescope_width(_, cols, _)
end
if telescope_width < 1 then
telescope_width = math.floor(cols * telescope_width)
end
local branch_width = 30
local name_width = math.floor(cols * 0.05)
local displayer = entry_display.create({
separator = "",
items = {
{ width = telescope_width - branch_width - name_width },
{ width = branch_width },
{ remaining = true },
},
})
local make_display = function(session)
return displayer({
session.name,
session.branch,
})
end
return finders.new_table({
results = sessions,
entry_maker = function(session)
session.ordinal = session.name
session.display = make_display
session.name = session.name
session.branch = session.branch
session.file_path = session.file_path
return session
end,
})
end
return M

View File

@ -1,74 +1,27 @@
local actions = require("telescope.actions")
local finders = require("telescope.finders")
local pickers = require("telescope.pickers")
local conf = require("telescope.config").values
local entry_display = require("telescope.pickers.entry_display")
local action_state = require("telescope.actions.state")
local _actions = require("telescope._extensions.actions")
local _finders = require("telescope._extensions.finders")
local function search_sessions(opts)
local config = require("persisted.config").options
-- Layout borrowed from:
---https://github.com/LinArcX/telescope-env.nvim/blob/master/lua/telescope/_extensions/env.lua
local cols = vim.o.columns
local telescope_width = conf.width
or conf.layout_config.width
or conf.layout_config[conf.layout_strategy].width
or cols
if type(telescope_width) == "function" then
telescope_width = telescope_width(_, cols, _)
end
if telescope_width < 1 then
telescope_width = math.floor(cols * telescope_width)
end
local branch_width = 30
local name_width = math.floor(cols * 0.05)
local displayer = entry_display.create({
separator = "",
items = {
{ width = telescope_width - branch_width - name_width },
{ width = branch_width },
{ remaining = true },
},
})
local make_display = function(entry)
return displayer({
entry.name,
entry.branch,
})
end
local make_entry = function(item)
return {
ordinal = item.name,
display = make_display,
name = item.name,
branch = item.branch,
file_path = item.file_path,
}
end
pickers.new(opts, {
prompt_title = "Sessions",
sorter = conf.generic_sorter(opts),
finder = finders.new_table({
results = require("persisted").list(),
entry_maker = make_entry,
}),
finder = _finders.session_finder(require("persisted").list()),
attach_mappings = function(prompt_bufnr, map)
local refresh_sessions = function()
local picker = action_state.get_current_picker(prompt_bufnr)
local finder = require("persisted").list()
picker:refresh(finder, { reset_prompt = true })
picker:refresh(_finders.session_finder(require("persisted").list()), { reset_prompt = true })
end
_actions.delete_session:enhance({ post = refresh_sessions })
map("i", "<c-d>", _actions.delete_session)
actions.select_default:replace(function()