-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheirline.lua
425 lines (387 loc) · 9.97 KB
/
heirline.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
local conditions = require("heirline.conditions")
local utils = require("heirline.utils")
local function setup_colours()
return {
bright_bg = utils.get_highlight("Folded").bg,
bright_fg = utils.get_highlight("Folded").fg,
red = utils.get_highlight("DiagnosticError").fg,
dark_red = utils.get_highlight("DiffDelete").bg,
green = utils.get_highlight("DiagnosticOk").fg,
blue = utils.get_highlight("Function").fg,
gray = utils.get_highlight("NonText").fg,
orange = utils.get_highlight("Constant").fg,
purple = utils.get_highlight("Conditional").fg,
cyan = utils.get_highlight("Special").fg,
diag_warn = utils.get_highlight("DiagnosticWarn").fg,
diag_error = utils.get_highlight("DiagnosticError").fg,
diag_hint = utils.get_highlight("DiagnosticHint").fg,
diag_info = utils.get_highlight("DiagnosticInfo").fg,
git_del = utils.get_highlight("diffDeleted").fg,
git_add = utils.get_highlight("diffAdded").fg,
git_change = utils.get_highlight("diffChanged").fg,
}
end
vim.api.nvim_create_augroup("Heirline", { clear = true })
vim.api.nvim_create_autocmd("ColorScheme", {
callback = function()
utils.on_colorscheme(setup_colours)
end,
group = "Heirline",
})
local Align = { provider = "%=", hl = { bg = "bright_bg" } }
local Space = { provider = " ", hl = { bg = "bright_bg" } }
-- Status Line Setup
local ViMode = {
init = function(self)
self.mode = vim.fn.mode(1)
end,
static = {
mode_names = { -- change the strings if you like it vvvvverbose!
n = "NORMAL",
no = "NORMAL",
nov = "NORMAL",
noV = "NORMAL",
["no\22"] = "NORMAL",
niI = "INSERT",
niR = "REPLACE",
niV = "VISUAL",
nt = "TERMINAL",
v = "VISUAL",
vs = "VISUAL",
V = "V-LINE",
Vs = "V-BLOCK",
["\22"] = "V-BLOCK",
["\22s"] = "V-BLOCK",
s = "SELECT",
S = "SELECT LINE",
["\19"] = "SELECT",
i = "INSERT",
ic = "INSERT",
ix = "INSERT",
R = "REPLACE",
Rc = "REPLACE",
Rx = "REPLACE",
Rv = "V-REPLACE",
Rvc = "V-REPLACE",
Rvx = "V-REPLACE",
c = "COMMAND",
cv = "COMMAND",
r = "...",
rm = "MORE",
["r?"] = "CONFIRM",
["!"] = "SHELL",
t = "TERMINAL",
},
mode_colours = {
n = "blue",
i = "green",
v = "purple",
V = "purple",
["\22"] = "purple",
c = "orange",
s = "cyan",
S = "cyan",
["\19"] = "cyan",
R = "orange",
r = "orange",
["!"] = "red",
t = "red",
},
},
provider = function(self)
return "▎ %2(" .. self.mode_names[self.mode] .. "%)"
end,
hl = function(self)
local mode = self.mode:sub(1, 1)
return { fg = self.mode_colours[mode], bold = true }
end,
update = {
"ModeChanged",
pattern = "*:*",
callback = vim.schedule_wrap(function()
vim.cmd("redrawstatus")
end),
},
}
local Git = {
condition = conditions.is_git_repo,
init = function(self)
self.status_dict = vim.b.gitsigns_status_dict
self.has_changes = self.status_dict.added ~= 0 or self.status_dict.removed ~= 0 or self.status_dict.changed ~= 0
end,
hl = { fg = "green" },
{ -- git branch name
provider = function(self)
return " " .. self.status_dict.head
end,
hl = { bold = true },
},
}
local Ruler = {
-- %l = current line number
-- %L = number of lines in the buffer
-- %c = column number
-- %P = percentage through file of displayed window
provider = "%7(%l/%3L%):%2c %P",
hl = { bg = "bright_bg" },
}
local ScrollBar = {
static = {
sbar = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" },
-- Another variant, because the more choice the better.
-- sbar = { '🭶', '🭷', '🭸', '🭹', '🭺', '🭻' }
},
provider = function(self)
local curr_line = vim.api.nvim_win_get_cursor(0)[1]
local lines = vim.api.nvim_buf_line_count(0)
local i = math.floor((curr_line - 1) / lines * #self.sbar) + 1
return string.rep(self.sbar[i], 2)
end,
hl = { fg = "blue", bg = "bright_bg" },
}
local LSPActive = {
condition = conditions.lsp_attached,
update = { "LspAttach", "LspDetach" },
-- You can keep it simple,
-- provider = " [LSP]",
-- Or complicate things a bit and get the servers names
provider = function()
local names = {}
for i, server in pairs(vim.lsp.get_active_clients({ bufnr = 0 })) do
table.insert(names, server.name)
end
return table.concat(names, " ")
end,
hl = { fg = "green", bold = true },
}
local FileNameBlock = {
-- let's first set up some attributes needed by this component and it's children
init = function(self)
self.filename = vim.api.nvim_buf_get_name(0)
end,
}
local FileFlags = {
{
condition = function()
return vim.bo.modified
end,
provider = "[+]",
hl = { fg = "green" },
},
{
condition = function()
return not vim.bo.modifiable or vim.bo.readonly
end,
provider = "",
hl = { fg = "orange" },
},
}
local FileType = {
provide = function()
return string.upper(vim.bo.filetype)
end,
hl = { fg = utils.get_highlight("Type").fg, bold = true },
}
local FileName = {
init = function(self)
self.lfilename = vim.fn.fnamemodify(self.filename, ":.")
if self.lfilename == "" then
self.lfilename = "[No Name]"
end
end,
hl = { fg = utils.get_highlight("Directory").fg },
flexible = 2,
{
provider = function(self)
return self.lfilename
end,
},
{
provider = function(self)
return vim.fn.pathshorten(self.lfilename)
end,
},
}
local FileNameModifer = {
hl = function()
if vim.bo.modified then
-- use `force` because we need to override the child's hl foreground
return { fg = "cyan", bold = true, force = true }
end
end,
}
-- Buffer Line Setup
local TablineBufnr = {
provider = function(self)
return tostring(self.bufnr) .. ". "
end,
hl = "Comment",
}
local TablineFileName = {
provider = function(self)
local filename = self.filename
filename = filename == "" and "[No Name]" or vim.fn.fnamemodify(filename, ":t")
return filename
end,
hl = function(self)
return { fg = "gray", bold = self.is_active or self.is_visible, italic = true }
end,
}
local TablineFileFlags = {
{
condition = function(self)
return vim.api.nvim_get_option_value("modified", { buf = self.bufnr })
end,
provider = "[+]",
hl = { fg = "green" },
},
{
condition = function(self)
return not vim.api.nvim_get_option_value("modifiable", { buf = self.bufnr })
or vim.api.nvim_get_option_value("readonly", { buf = self.bufnr })
end,
provider = function(self)
if vim.api.nvim_get_option_value("buftype", { buf = self.bufnr }) == "terminal" then
return " "
else
return ""
end
end,
hl = { fg = "orange" },
},
}
local TablineFileNameBlock = {
init = function(self)
self.filename = vim.api.nvim_buf_get_name(self.bufnr)
end,
hl = function(self)
if self.is_active then
return "TabLineSel"
else
return "TabLine"
end
end,
on_click = {
callback = function(_, minwid, _, button)
if button == "m" then
vim.schedule(function()
vim.api.nvim_buf_delete(minwid, { force = false })
end)
else
vim.api.nvim_win_set_buf(0, minwid)
end
end,
minwid = function(self)
return self.bufnr
end,
name = "heirline_tabline_buffer_callback",
},
-- TablineBufnr,
TablineFileName,
TablineFileFlags,
}
-- a nice "x" button to close the buffer
local TablineCloseButton = {
condition = function(self)
return not vim.api.nvim_get_option_value("modified", { buf = self.bufnr })
end,
{ provider = " " },
{
provider = "",
hl = { fg = "gray" },
on_click = {
callback = function(_, minwid)
vim.schedule(function()
vim.api.nvim_buf_delete(minwid, { force = false })
vim.cmd.redrawtabline()
end)
end,
minwid = function(self)
return self.bufnr
end,
name = "heirline_tabline_close_buffer_callback",
},
},
}
-- The final touch!
local TablineBufferBlock = utils.surround({ "", "" }, function(self)
if self.is_active then
return utils.get_highlight("TabLineSel").bg
else
return utils.get_highlight("TabLine").bg
end
end, { TablineFileNameBlock, TablineCloseButton })
-- and here we go
local BufferLine = utils.make_buflist(
TablineBufferBlock,
{ provider = "", hl = { fg = "gray" } }, -- left truncation, optional (defaults to "<")
{ provider = "", hl = { fg = "gray" } } -- right trunctation, also optional (defaults to ...... yep, ">")
-- by the way, open a lot of buffers and try clicking them ;)
)
-- this is the default function used to retrieve buffers
local get_bufs = function()
return vim.tbl_filter(function(bufnr)
return vim.api.nvim_get_option_value("buflisted", { buf = bufnr })
end, vim.api.nvim_list_bufs())
end
-- initialize the buflist cache
local buflist_cache = {}
-- setup an autocmd that updates the buflist_cache every time that buffers are added/removed
vim.api.nvim_create_autocmd({ "VimEnter", "UIEnter", "BufAdd", "BufDelete" }, {
callback = function()
vim.schedule(function()
local buffers = get_bufs()
for i, v in ipairs(buffers) do
buflist_cache[i] = v
end
for i = #buffers + 1, #buflist_cache do
buflist_cache[i] = nil
end
-- check how many buffers we have and set showtabline accordingly
if #buflist_cache > 1 then
vim.o.showtabline = 2 -- always
elseif vim.o.showtabline ~= 1 then -- don't reset the option if it's already at default value
vim.o.showtabline = 1 -- only when #tabpages > 1
end
end)
end,
})
local BufferLine = utils.make_buflist(
TablineBufferBlock,
{ provider = " ", hl = { fg = "gray" } },
{ provider = " ", hl = { fg = "gray" } },
-- out buf_func simply returns the buflist_cache
function()
return buflist_cache
end,
-- no cache, as we're handling everything ourselves
false
)
FileNameBlock = utils.insert(FileNameBlock, utils.insert(FileNameModifer, FileName), FileFlags, { provider = "%<" })
local StatusLine = {
ViMode,
Space,
Space,
Git,
Space,
Space,
FileNameBlock,
Align,
LSPActive,
Space,
FileType,
Space,
Ruler,
Space,
ScrollBar,
}
local TabLine = { BufferLine }
require("heirline").setup({
statusline = StatusLine,
tabline = TabLine,
opts = {
colors = setup_colours,
},
})
vim.o.showtabline = 2
vim.cmd([[au FileType * if index(['wipe', 'delete'], &bufhidden) >= 0 | set nobuflisted | endif]])