forked from moyiz/git-dev.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmini.lua
More file actions
98 lines (93 loc) · 2.81 KB
/
Copy pathmini.lua
File metadata and controls
98 lines (93 loc) · 2.81 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
local config = require("git-dev.pickers").config.history
local picker_utils = require("git-dev.pickers").utils
local pickers = {}
---Generates a history picker for mini.pick
---@param local_opts HistoryLocalOpts
function pickers.history(local_opts)
local minipick = require "mini.pick"
local ns = vim.api.nvim_create_namespace "GitDevPickers"
minipick.start {
source = {
name = config.title,
items = config.get_entries,
choose = function(item)
minipick.stop()
config.select_entry(item)
end,
show = function(buf_id, items)
local widths = picker_utils.normalize_width(
vim.fn.winwidth(0), -- should deduct separator width
local_opts.entry.ratios,
{
local_opts.entry.repo_url.width,
local_opts.entry.ref.width,
local_opts.entry.selected_path.width,
}
)
local entries_parts = vim.tbl_map(function(item)
local parts = config.label_parts(item)
return {
picker_utils.fit_string(
parts[1],
widths[1],
{ align = "left", truncate = "left" }
),
local_opts.separator.text or " ",
picker_utils.fit_string(
parts[2],
widths[2],
{ align = "center", truncate = "right" }
),
local_opts.separator.text or " ",
picker_utils.fit_string(
parts[3],
widths[3],
{ align = "left", truncate = "left" }
),
}
end, items)
vim.api.nvim_buf_set_lines(
buf_id,
0,
-1,
false,
vim.tbl_map(function(parts)
return vim.iter(parts):join ""
end, entries_parts)
)
-- Highlight parts
local hl_groups = {
local_opts.entry.repo_url.hl_group,
local_opts.separator.hl_group,
local_opts.entry.ref.hl_group,
local_opts.separator.hl_group,
local_opts.entry.selected_path.hl_group,
}
vim.api.nvim_buf_clear_namespace(buf_id, ns, 0, -1)
for i, entry in ipairs(entries_parts) do
local col = 0
for j, hl in ipairs(hl_groups) do
local end_col = col + string.len(entry[j])
if hl then
vim.api.nvim_buf_set_extmark(buf_id, ns, i - 1, col, {
end_row = i - 1,
end_col = end_col,
hl_group = hl,
priority = 202,
})
end
col = end_col
end
end
end,
preview = function(buf_id, item)
return config.preview(
buf_id,
minipick.get_picker_state().windows.main,
item
)
end,
},
}
end
return pickers