refactor: #7 improve telescope support
parent
baa9c73818
commit
5b3525ad5e
|
|
@ -242,8 +242,7 @@ require("persisted").setup({
|
||||||
|
|
||||||
A *session* table is exposed to the callback functions and has the following properties:
|
A *session* table is exposed to the callback functions and has the following properties:
|
||||||
* name - The filename of the session.
|
* name - The filename of the session.
|
||||||
* branch - The git branch of the session.
|
* branch - The git branch of the session; *and*
|
||||||
* pwd - The present working directory of the session; *and*
|
|
||||||
* file_path - The file path to the session.
|
* file_path - The file path to the session.
|
||||||
|
|
||||||
## :rocket: Usage
|
## :rocket: Usage
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ end
|
||||||
---Get the session that was saved last
|
---Get the session that was saved last
|
||||||
---@return string
|
---@return string
|
||||||
local function get_last()
|
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)
|
table.sort(sessions, function(a, b)
|
||||||
return vim.loop.fs_stat(a).mtime.sec > vim.loop.fs_stat(b).mtime.sec
|
return vim.loop.fs_stat(a).mtime.sec > vim.loop.fs_stat(b).mtime.sec
|
||||||
end)
|
end)
|
||||||
|
|
@ -160,6 +160,18 @@ function M.delete()
|
||||||
end
|
end
|
||||||
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
|
---List all of the sessions in the session directory
|
||||||
---@return table
|
---@return table
|
||||||
function M.list()
|
function M.list()
|
||||||
|
|
@ -174,33 +186,14 @@ function M.list()
|
||||||
:gsub(vim.fn.expand("~"), utils.get_dir_pattern())
|
:gsub(vim.fn.expand("~"), utils.get_dir_pattern())
|
||||||
:gsub("//", "")
|
: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, {
|
table.insert(sessions, {
|
||||||
["name"] = session_name,
|
["name"] = utils.split_str(session_name, "_")[1],
|
||||||
["file_path"] = session,
|
["file_path"] = session,
|
||||||
["branch"] = branch,
|
["branch"] = utils.split_str(session_name, "_")[2]:gsub(".vim", ""),
|
||||||
["pwd"] = pwd,
|
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
return sessions
|
return sessions
|
||||||
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
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
|
|
@ -6,14 +6,28 @@ local entry_display = require("telescope.pickers.entry_display")
|
||||||
local action_state = require("telescope.actions.state")
|
local action_state = require("telescope.actions.state")
|
||||||
|
|
||||||
local config = require("persisted.config").options
|
local config = require("persisted.config").options
|
||||||
local sessions = require("persisted").list()
|
|
||||||
|
|
||||||
local function search_sessions(opts)
|
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({
|
local displayer = entry_display.create({
|
||||||
separator = " │ ",
|
separator = " │ ",
|
||||||
items = {
|
items = {
|
||||||
{ width = 50 },
|
{ width = telescope_width - branch_width - name_width },
|
||||||
{ width = 10 },
|
{ width = branch_width },
|
||||||
{ remaining = true },
|
{ remaining = true },
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
@ -31,7 +45,6 @@ local function search_sessions(opts)
|
||||||
|
|
||||||
name = item.name,
|
name = item.name,
|
||||||
branch = item.branch,
|
branch = item.branch,
|
||||||
pwd = item.pwd,
|
|
||||||
file_path = item.file_path,
|
file_path = item.file_path,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
@ -40,7 +53,7 @@ local function search_sessions(opts)
|
||||||
prompt_title = "Sessions",
|
prompt_title = "Sessions",
|
||||||
sorter = conf.generic_sorter(opts),
|
sorter = conf.generic_sorter(opts),
|
||||||
finder = finders.new_table({
|
finder = finders.new_table({
|
||||||
results = sessions,
|
results = require("persisted").list(),
|
||||||
entry_maker = make_entry,
|
entry_maker = make_entry,
|
||||||
}),
|
}),
|
||||||
attach_mappings = function(prompt_bufnr)
|
attach_mappings = function(prompt_bufnr)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue