forked from moyiz/git-dev.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.lua
More file actions
75 lines (64 loc) · 1.64 KB
/
session.lua
File metadata and controls
75 lines (64 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---@class GitDevSessionRepo
---@field repo string
---@field ref? GitRef
---@field repo_dir string
---@field ephemeral_autocmd_id? number
---@field read_only_autocmd_id? number
---@field set_session_autocmd_id? number
---@field history_key? Key
---@alias GitDevSessionRepos table<number, GitDevSessionRepo>
---@class GitDevSession
---@field repos GitDevSessionRepo
local Session = {}
---@param o? GitDevSession
function Session:init(o)
o = o or {}
setmetatable(o, self)
self.__index = self
self.repos = {}
return o
end
---@param repo_session GitDevSessionRepo
---@return string @key
function Session:set_repo(repo_session)
local key = self:key(repo_session)
self.repos[key] = repo_session
return key
end
function Session:get_repo(key)
return self.repos[key]
end
function Session:key(repo_session)
return repo_session.repo_dir
end
function Session:remove(key)
self.repos[key] = nil
end
---@alias FilterType "equal"|"contains"
---@class _Filter
---@field name string
---@field value any
---@field type FilterType
---@param filters _Filter[] A list of filters to apply on each repository.
function Session:find(filters)
local results = {}
for ctx_id, repo_ctx in pairs(self.repos) do
local match = true
for _, filter in ipairs(filters) do
if
filter.type == "equal"
and not vim.deep_equal(repo_ctx[filter.name], filter.value)
or filter.type == "contains"
and not repo_ctx[filter.name]:find(filter.value, 1, true)
then
match = false
break
end
end
if match and ctx_id then
table.insert(results, ctx_id)
end
end
return results
end
return Session