-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
lualine.lua
169 lines (150 loc) · 4.44 KB
/
lualine.lua
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
---@class lazyvim.util.lualine
local M = {}
function M.cmp_source(name, icon)
local started = false
local function status()
if not package.loaded["cmp"] then
return
end
for _, s in ipairs(require("cmp").core.sources) do
if s.name == name then
if s.source:is_available() then
started = true
else
return started and "error" or nil
end
if s.status == s.SourceStatus.FETCHING then
return "pending"
end
return "ok"
end
end
end
local colors = {
ok = LazyVim.ui.fg("Special"),
error = LazyVim.ui.fg("DiagnosticError"),
pending = LazyVim.ui.fg("DiagnosticWarn"),
}
return {
function()
return icon or require("lazyvim.config").icons.kinds[name:sub(1, 1):upper() .. name:sub(2)]
end,
cond = function()
return status() ~= nil
end,
color = function()
return colors[status()] or colors.ok
end,
}
end
---@param component any
---@param text string
---@param hl_group? string
---@return string
function M.format(component, text, hl_group)
text = text:gsub("%%", "%%%%")
if not hl_group or hl_group == "" then
return text
end
---@type table<string, string>
component.hl_cache = component.hl_cache or {}
local lualine_hl_group = component.hl_cache[hl_group]
if not lualine_hl_group then
local utils = require("lualine.utils.utils")
---@type string[]
local gui = vim.tbl_filter(function(x)
return x
end, {
utils.extract_highlight_colors(hl_group, "bold") and "bold",
utils.extract_highlight_colors(hl_group, "italic") and "italic",
})
lualine_hl_group = component:create_hl({
fg = utils.extract_highlight_colors(hl_group, "fg"),
gui = #gui > 0 and table.concat(gui, ",") or nil,
}, "LV_" .. hl_group) --[[@as string]]
component.hl_cache[hl_group] = lualine_hl_group
end
return component:format_hl(lualine_hl_group) .. text .. component:get_default_hl()
end
---@param opts? {relative: "cwd"|"root", modified_hl: string?, directory_hl: string?, filename_hl: string?}
function M.pretty_path(opts)
opts = vim.tbl_extend("force", {
relative = "cwd",
modified_hl = "MatchParen",
directory_hl = "",
filename_hl = "Bold",
modified_sign = "",
length = 3,
}, opts or {})
return function(self)
local path = vim.fn.expand("%:p") --[[@as string]]
if path == "" then
return ""
end
local root = LazyVim.root.get({ normalize = true })
local cwd = LazyVim.root.cwd()
if opts.relative == "cwd" and path:find(cwd, 1, true) == 1 then
path = path:sub(#cwd + 2)
else
path = path:sub(#root + 2)
end
local sep = package.config:sub(1, 1)
local parts = vim.split(path, "[\\/]")
if opts.length == 0 then
parts = parts
elseif #parts > opts.length then
parts = { parts[1], "…", table.concat({ unpack(parts, #parts - opts.length + 2, #parts) }, sep) }
end
if opts.modified_hl and vim.bo.modified then
parts[#parts] = parts[#parts] .. opts.modified_sign
parts[#parts] = M.format(self, parts[#parts], opts.modified_hl)
else
parts[#parts] = M.format(self, parts[#parts], opts.filename_hl)
end
local dir = ""
if #parts > 1 then
dir = table.concat({ unpack(parts, 1, #parts - 1) }, sep)
dir = M.format(self, dir .. sep, opts.directory_hl)
end
return dir .. parts[#parts]
end
end
---@param opts? {cwd:false, subdirectory: true, parent: true, other: true, icon?:string}
function M.root_dir(opts)
opts = vim.tbl_extend("force", {
cwd = false,
subdirectory = true,
parent = true,
other = true,
icon = " ",
color = LazyVim.ui.fg("Special"),
}, opts or {})
local function get()
local cwd = LazyVim.root.cwd()
local root = LazyVim.root.get({ normalize = true })
local name = vim.fs.basename(root)
if root == cwd then
-- root is cwd
return opts.cwd and name
elseif root:find(cwd, 1, true) == 1 then
-- root is subdirectory of cwd
return opts.subdirectory and name
elseif cwd:find(root, 1, true) == 1 then
-- root is parent directory of cwd
return opts.parent and name
else
-- root and cwd are not related
return opts.other and name
end
end
return {
function()
return (opts.icon and opts.icon .. " ") .. get()
end,
cond = function()
return type(get()) == "string"
end,
color = opts.color,
}
end
return M