refactor: #7 improve telescope support

main
olimorris 2022-04-24 12:58:11 +01:00
parent baa9c73818
commit 5b3525ad5e
3 changed files with 34 additions and 29 deletions

View File

@ -242,8 +242,7 @@ require("persisted").setup({
A *session* table is exposed to the callback functions and has the following properties:
* name - The filename of the session.
* branch - The git branch of the session.
* pwd - The present working directory of the session; *and*
* branch - The git branch of the session; *and*
* file_path - The file path to the session.
## :rocket: Usage

View File

@ -46,7 +46,7 @@ end
---Get the session that was saved last
---@return string
local function get_last()
local sessions = M.list()
local sessions = vim.fn.glob(config.options.dir .. "*.vim", true, true)
table.sort(sessions, function(a, b)
return vim.loop.fs_stat(a).mtime.sec > vim.loop.fs_stat(b).mtime.sec
end)
@ -160,6 +160,18 @@ function M.delete()
end
end
---Determines whether to load, start or stop a session
---@return function
function M.toggle()
if vim.g.persisting == nil then
return M.load()
end
if vim.g.persisting then
return M.stop()
end
return M.start()
end
---List all of the sessions in the session directory
---@return table
function M.list()
@ -174,33 +186,14 @@ function M.list()
:gsub(vim.fn.expand("~"), utils.get_dir_pattern())
:gsub("//", "")
local branch = utils.get_last_item(utils.split_str(session_name, "_")):gsub(".vim", "")
local pwd = vim.fn.expand("~") .. utils.get_dir_pattern() .. session_name
local branch_name = "_" .. utils.get_last_item(utils.split_str(pwd, "_"))
pwd = pwd:gsub(branch_name, "")
table.insert(sessions, {
["name"] = session_name,
["name"] = utils.split_str(session_name, "_")[1],
["file_path"] = session,
["branch"] = branch,
["pwd"] = pwd,
["branch"] = utils.split_str(session_name, "_")[2]:gsub(".vim", ""),
})
end
return sessions
end
---Determines whether to load, start or stop a session
---@return function
function M.toggle()
if vim.g.persisting == nil then
return M.load()
end
if vim.g.persisting then
return M.stop()
end
return M.start()
end
return M

View File

@ -6,14 +6,28 @@ local entry_display = require("telescope.pickers.entry_display")
local action_state = require("telescope.actions.state")
local config = require("persisted.config").options
local sessions = require("persisted").list()
local function search_sessions(opts)
-- 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 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 = 50 },
{ width = 10 },
{ width = telescope_width - branch_width - name_width },
{ width = branch_width },
{ remaining = true },
},
})
@ -31,7 +45,6 @@ local function search_sessions(opts)
name = item.name,
branch = item.branch,
pwd = item.pwd,
file_path = item.file_path,
}
end
@ -40,7 +53,7 @@ local function search_sessions(opts)
prompt_title = "Sessions",
sorter = conf.generic_sorter(opts),
finder = finders.new_table({
results = sessions,
results = require("persisted").list(),
entry_maker = make_entry,
}),
attach_mappings = function(prompt_bufnr)