-
Notifications
You must be signed in to change notification settings - Fork 0
/
statusline.lua
405 lines (340 loc) · 8.58 KB
/
statusline.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
local statusline_augroup = vim.api.nvim_create_augroup("komisar_statusline", { clear = true })
local modes = {
["n"] = "NORMAL",
["no"] = "NORMAL",
["v"] = "VISUAL",
["V"] = "V·LINE",
[""] = "V·BLOCK",
["s"] = "SELECT",
["S"] = "S·LINE",
[""] = "S·BLOCK",
["i"] = "INSERT",
["ic"] = "INSERT",
["R"] = "REPLACE",
["Rv"] = "V·REPLACE",
["c"] = "COMMAND",
["cv"] = "VIM·EX",
["ce"] = "EX",
["r"] = "PROMPT",
["rm"] = "MORE",
["r?"] = "CONFIRM",
["!"] = "SHELL",
["t"] = "TERMINAL",
["nt"] = "TERMINAL",
}
--- @param severity integer
--- @return integer
local function get_lsp_diagnostics_count(severity)
if not rawget(vim, "lsp") then
return 0
end
local count = vim.diagnostic.count(0, { severity = severity })[severity]
-- local count = vim.diagnostic.count(0, { serverity = severity })[severity]
if count == nil then
return 0
end
return count
end
--- @param type string
--- @return integer
local function get_git_diff(type)
local gsd = vim.b.gitsigns_status_dict
if gsd and gsd[type] then
return gsd[type]
end
return 0
end
--- @return string
local function mode()
-- return string.format("%%#StatusLineMode# %s %%*", vim.api.nvim_get_mode().mode)
local current_mode = vim.api.nvim_get_mode().mode
return string.format("%%#StatusLineMode# %s %%*", modes[current_mode]:upper())
end
-- --- @return string
-- local function python_env()
-- if not rawget(vim, "lsp") then
-- return ""
-- end
--
-- local buf = vim.api.nvim_get_current_buf()
-- local buf_clients = vim.lsp.get_clients({ bufnr = buf })
-- if next(buf_clients) == nil then
-- return ""
-- end
--
-- for _, client in pairs(buf_clients) do
-- if client.name == "pyright" or client.name == "pylance" then
-- local virtual_env = os.getenv("VIRTUAL_ENV_PROMPT")
-- if virtual_env == nil then
-- return ""
-- end
--
-- virtual_env = virtual_env:gsub("%s+", "")
-- return string.format("%%#StatusLineMedium# %s%%*", virtual_env)
-- end
-- end
--
-- return ""
-- end
--- @return string
local function lsp_active()
if not rawget(vim, "lsp") then
return ""
end
local current_buf = vim.api.nvim_get_current_buf()
local clients = vim.lsp.get_clients({ bufnr = current_buf })
local space = "%#StatusLineMedium# %*"
if #clients > 0 then
return space .. "%#StatusLineLspActive#●%*" .. space .. "%#StatusLineMedium#LSP%*"
end
return ""
end
--- @return string
local function diagnostics_error()
local count = get_lsp_diagnostics_count(vim.diagnostic.severity.ERROR)
if count > 0 then
return string.format("%%#StatusLineLspError# ● %s%%*", count)
end
return ""
end
--- @return string
local function diagnostics_warns()
local count = get_lsp_diagnostics_count(vim.diagnostic.severity.WARN)
if count > 0 then
return string.format("%%#StatusLineLspWarn# ● %s%%*", count)
end
return ""
end
--- @return string
local function diagnostics_hint()
local count = get_lsp_diagnostics_count(vim.diagnostic.severity.HINT)
if count > 0 then
return string.format("%%#StatusLineLspHint# ● %s%%*", count)
end
return ""
end
--- @return string
local function diagnostics_info()
local count = get_lsp_diagnostics_count(vim.diagnostic.severity.INFO)
if count > 0 then
return string.format("%%#StatusLineLspInfo# ● %s%%*", count)
end
return ""
end
--- @class LspProgress
--- @field client vim.lsp.Client?
--- @field kind string?
--- @field title string?
--- @field percentage integer?
--- @field message string?
local lsp_progress = {
client = nil,
kind = nil,
title = nil,
percentage = nil,
message = nil,
}
vim.api.nvim_create_autocmd("LspProgress", {
group = statusline_augroup,
desc = "Update LSP progress in statusline",
pattern = { "begin", "report", "end" },
callback = function(args)
if not (args.data and args.data.client_id) then
return
end
lsp_progress = {
client = vim.lsp.get_client_by_id(args.data.client_id),
kind = args.data.params.value.kind,
message = args.data.params.value.message,
percentage = args.data.params.value.percentage,
title = args.data.params.value.title,
}
if lsp_progress.kind == "end" then
lsp_progress.title = nil
vim.defer_fn(function()
vim.cmd.redrawstatus()
end, 500)
else
vim.cmd.redrawstatus()
end
end,
})
--- @return string
local function lsp_status()
if not rawget(vim, "lsp") then
return ""
end
if vim.o.columns < 120 then
return ""
end
if not lsp_progress.client or not lsp_progress.title then
return ""
end
local title = lsp_progress.title or ""
local percentage = (lsp_progress.percentage and (lsp_progress.percentage .. "%%")) or ""
local message = lsp_progress.message or ""
local lsp_message = string.format("%s", title)
if message ~= "" then
lsp_message = string.format("%s %s", lsp_message, message)
end
if percentage ~= "" then
lsp_message = string.format("%s %s", lsp_message, percentage)
end
return string.format("%%#StatusLineLspMessages#%s%%* ", lsp_message)
end
--- @return string
local function git_diff_added()
local added = get_git_diff("added")
if added > 0 then
return string.format("%%#StatusLineGitDiffAdded#+%s%%*", added)
end
return ""
end
--- @return string
local function git_diff_changed()
local changed = get_git_diff("changed")
if changed > 0 then
return string.format("%%#StatusLineGitDiffChanged#~%s%%*", changed)
end
return ""
end
--- @return string
local function git_diff_removed()
local removed = get_git_diff("removed")
if removed > 0 then
return string.format("%%#StatusLineGitDiffRemoved#-%s%%*", removed)
end
return ""
end
--- @return string
local function git_branch_icon()
return "%#StatusLineGitBranchIcon#%*"
end
--- @return string
local function git_branch()
local branch = vim.b.gitsigns_head
if branch == "" or branch == nil then
return ""
end
return string.format("%%#StatusLineMedium#%s%%*", branch)
end
--- @return string
local function full_git()
local full = ""
local space = "%#StatusLineMedium# %*"
local branch = git_branch()
if branch ~= "" then
local icon = git_branch_icon()
full = full .. space .. icon .. space .. branch .. space
end
local added = git_diff_added()
if added ~= "" then
full = full .. added .. space
end
local changed = git_diff_changed()
if changed ~= "" then
full = full .. changed .. space
end
local removed = git_diff_removed()
if removed ~= "" then
full = full .. removed .. space
end
return full
end
--- @return string
local function file_percentage()
local current_line = vim.api.nvim_win_get_cursor(0)[1]
local lines = vim.api.nvim_buf_line_count(0)
return string.format("%%#StatusLineMedium# %d%%%% %%*", math.ceil(current_line / lines * 100))
end
--- @return string
local function total_lines()
local lines = vim.fn.line("$")
return string.format("%%#StatusLineMedium#of %s %%*", lines)
end
--- @param hlgroup string
local function formatted_filetype(hlgroup)
local filetype = vim.bo.filetype or vim.fn.expand("%:e", false)
return string.format("%%#%s# %s %%*", hlgroup, filetype)
end
--- @return string
local function filename()
-- Get the full path of the file
local file_path = vim.fn.expand("%:p")
-- Get the parent directory of the file
local folder_path = vim.fn.fnamemodify(file_path, ":h:t")
local file = vim.fn.expand("%:t")
if file == "" then
file = "[No Name]"
end
return string.format("%%#StatusLineFileName# %s/%s %%*", folder_path, file)
end
StatusLine = {}
StatusLine.inactive = function()
return table.concat({
formatted_filetype("StatusLineMode"),
})
end
local redeable_filetypes = {
["qf"] = true,
["help"] = true,
["tsplayground"] = true,
}
StatusLine.active = function()
local mode_str = vim.api.nvim_get_mode().mode
if mode_str == "t" or mode_str == "nt" then
return table.concat({
mode(),
"%=",
"%=",
file_percentage(),
total_lines(),
})
end
if redeable_filetypes[vim.bo.filetype] or vim.o.modifiable == false then
return table.concat({
formatted_filetype("StatusLineMode"),
"%=",
"%=",
file_percentage(),
total_lines(),
})
end
local statusline = {
mode(),
full_git(),
filename(),
"%=",
"%=",
"%S ",
lsp_status(),
diagnostics_error(),
diagnostics_warns(),
diagnostics_hint(),
diagnostics_info(),
lsp_active(),
-- python_env(),
file_percentage(),
total_lines(),
}
return table.concat(statusline)
end
vim.opt.statusline = "%!v:lua.StatusLine.active()"
vim.api.nvim_create_autocmd({ "WinEnter", "BufEnter", "FileType" }, {
group = statusline_augroup,
pattern = {
"NvimTree_1",
"NvimTree",
"TelescopePrompt",
"fzf",
"lspinfo",
"lazy",
"netrw",
"mason",
"noice",
"qf",
},
callback = function()
vim.opt_local.statusline = "%!v:lua.StatusLine.inactive()"
end,
})