Skip to content

Commit c783098

Browse files
committed
feat(store): toggle sort
1 parent dbfd004 commit c783098

3 files changed

Lines changed: 83 additions & 56 deletions

File tree

lua/fzf-lua-extra/providers/lazy.lua

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,25 @@ local p_do = function(cb)
1313
end
1414
end
1515

16-
local State = {}
17-
State.state = {
18-
all_name = function()
19-
State.encode = function(p) return p.name end
20-
end,
21-
all_repo = function()
22-
---@param p LazyPlugin
23-
---@return string
24-
State.encode = function(p)
25-
local fullname = p[1]
26-
if not fullname then
27-
local url = p.url
28-
if not url then
29-
fullname = 'unknown/' .. p.name -- dummy name
30-
else
31-
local url_slice = vim.split(url, '/')
32-
local username = url_slice[#url_slice - 1]
33-
local repo = url_slice[#url_slice]
34-
fullname = username .. '/' .. repo
35-
end
36-
end
37-
return fullname
16+
local fmt_repo = function(p)
17+
local fullname = p[1]
18+
if not fullname then
19+
local url = p.url
20+
if not url then
21+
fullname = 'unknown/' .. p.name -- dummy name
22+
else
23+
local url_slice = vim.split(url, '/')
24+
local username = url_slice[#url_slice - 1]
25+
local repo = url_slice[#url_slice]
26+
fullname = username .. '/' .. repo
3827
end
39-
end,
40-
}
41-
State.cycle = function()
42-
State.key = next(State.state, State.key)
43-
if not State.key then State.key = next(State.state, State.key) end
44-
State.state[State.key]()
28+
end
29+
return fullname
4530
end
4631

47-
---@return function, function
48-
State.get = function() return State.filter, State.encode end
49-
State.cycle()
32+
local state = require('fzf-lua-extra.state').new()
33+
state:put('fmt', 'full', function(p) return p.name end)
34+
state:put('fmt', 'repo', fmt_repo)
5035

5136
---@class fle.config.Lazy: fzf-lua.config.Base
5237
local __DEFAULT__ = {
@@ -68,18 +53,15 @@ local __DEFAULT__ = {
6853
['ctrl-r'] = p_do(
6954
function(p) require('lazy.core.loader')[p._ and p._.loaded and 'reload' or 'load'](p) end
7055
),
71-
['ctrl-g'] = { fn = State.cycle, reload = true },
56+
['ctrl-g'] = { fn = function() state:cycle() end, reload = true },
7257
},
7358
}
7459

7560
return function(opts)
7661
assert(__DEFAULT__)
7762
local contents = function(fzf_cb)
78-
local filter, encode = State.get()
79-
vim
80-
.iter(utils.get_lazy_plugins())
81-
:filter(filter or function() return true end)
82-
:each(function(_, p) fzf_cb(encode(p)) end)
63+
local fmt = state:get()
64+
vim.iter(utils.get_lazy_plugins()):each(function(_, p) fzf_cb(fmt(p)) end)
8365
fzf_cb()
8466
end
8567
return FzfLua.fzf_exec(contents, opts)

lua/fzf-lua-extra/providers/store.lua

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,20 @@ local function format_repository_info(repo, compact)
8282
return table.concat(parts, ' ')
8383
end
8484

85-
local State = {}
86-
State.state = {
87-
all = function()
88-
State.encode = function(p) return format_repository_info(p) end
89-
end,
90-
compat = function()
91-
State.encode = function(p) return format_repository_info(p, true) end
92-
end,
93-
}
94-
State.cycle = function()
95-
State.key = next(State.state, State.key)
96-
if not State.key then State.key = next(State.state, State.key) end
97-
State.state[State.key]()
85+
-- format_repository_info
86+
local state = require('fzf-lua-extra.state').new()
87+
state:put('fmt', 'detail', function(p) return format_repository_info(p) end)
88+
state:put('fmt', 'compat', function(p) return format_repository_info(p, true) end)
89+
local sort_by_stars = function(a, b)
90+
return a.stars > b.stars or (a.stars == b.stars and a.issues > b.issues)
91+
end
92+
local sort_by_issues = function(a, b)
93+
return a.issues > b.issues or (a.issues == b.issues and a.stars > b.stars)
9894
end
9995

100-
---@return function, function
101-
State.get = function() return State.filter, State.encode end
102-
State.cycle()
96+
state:put('sort', 'no_sort', false)
97+
state:put('sort', 'stars', sort_by_stars)
98+
state:put('sort', 'issues', sort_by_issues)
10399

104100
---@class fle.config.Store: fzf-lua.config.Base
105101
local __DEFAULT__ = {
@@ -120,6 +116,7 @@ local __DEFAULT__ = {
120116
items = {},
121117
},
122118
fzf_opts = {
119+
['--no-sort'] = true,
123120
['--delimiter'] = '\t',
124121
['--with-nth'] = '2..',
125122
['--no-hscroll'] = true,
@@ -153,7 +150,8 @@ local __DEFAULT__ = {
153150
['ctrl-r'] = p_do(
154151
function(p) require('lazy.core.loader')[p._ and p._.loaded and 'reload' or 'load'](p) end
155152
),
156-
['ctrl-g'] = { fn = State.cycle, reload = true },
153+
['ctrl-g'] = { fn = function() state:cycle('fmt') end, reload = true },
154+
['ctrl-s'] = { fn = function() state:cycle('sort') end, reload = true },
157155
},
158156
}
159157

@@ -217,12 +215,15 @@ return function(opts)
217215
{ cache_path = utils.path('store.json'), cache_invalid = opts.cache_invalid }
218216
).stdout or ''
219217
)
218+
local fmt = state:get('fmt')
219+
local sort = state:get('sort')
220220
local items = db.items
221+
-- TODO: maybe we also need "toggle-sort"
222+
if sort then table.sort(items, sort) end
221223
opts.previewer.items = opts.previewer.items or {}
222224
for _, item in ipairs(items) do
223225
opts.previewer.items[item.full_name] = item
224-
local filter, encode = State.get()
225-
if not filter or filter(item) then cb(encode(item)) end
226+
cb(fmt(item))
226227
end
227228
cb(nil)
228229
end)

lua/fzf-lua-extra/state.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---@class fle.State
2+
---@field private iterables table<string, table<string, any>> iterator
3+
---@field private keys table<string, string>
4+
local M = {}
5+
6+
M.__index = M
7+
8+
M.new = function()
9+
return setmetatable({
10+
iterables = {},
11+
keys = {},
12+
}, M)
13+
end
14+
15+
---@param ns string
16+
---@param k any
17+
---@param v any
18+
function M:put(ns, k, v)
19+
if not self.iterables[ns] then
20+
self.iterables[ns] = {}
21+
self.keys[ns] = k
22+
end
23+
self.iterables[ns][k] = v
24+
end
25+
26+
---@param ns? string
27+
function M:cycle(ns)
28+
ns = ns or assert(next(self.iterables), 'No namespaces available to cycle')
29+
local iters = self.iterables[ns]
30+
local key = self.keys[ns]
31+
local nextkey = next(iters, key)
32+
if nextkey == nil then nextkey = next(iters) end
33+
self.keys[ns] = nextkey
34+
end
35+
36+
---@param ns? string
37+
---@return any, string
38+
function M:get(ns)
39+
ns = ns or assert(next(self.iterables), 'No namespaces available to get')
40+
local key = assert(self.keys[ns])
41+
return self.iterables[ns][key], key
42+
end
43+
44+
return M

0 commit comments

Comments
 (0)