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
|
local M = {}
-- Highlighting Shortcut
local hi_pattern = '%%#%s#%s%%*'
M.hi_pattern = hi_pattern
-- Use icons?
M.use_icons = false
local use_icons = M.use_icons
-- Utilities
local utils = {}
-- The width of the statusline.
utils.linewidth = function ()
if vim.o.laststatus == 3 then
return vim.o.columns
else
return vim.api.nvim_win_get_width(0)
end
end
utils.component_takes_percentage = function (comp_width, percentage)
return comp_width > (percentage/100) * utils.linewidth()
end
M.utils = utils
-- Pre-made Statusline Components
M.components = {}
M.components.mode = function (args)
args = args or {}
local before = args.before or ""
local after = args.after or ""
-- Convert mode to string
local mode_to_str = {
['n'] = 'NORMAL',
['no'] = 'OP-PENDING',
['nov'] = 'OP-PENDING',
['noV'] = 'OP-PENDING',
['no\22'] = 'OP-PENDING',
['niI'] = 'NORMAL',
['niR'] = 'NORMAL',
['niV'] = 'NORMAL',
['nt'] = 'NORMAL',
['ntT'] = 'NORMAL',
['v'] = 'VISUAL',
['vs'] = 'VISUAL',
['V'] = 'VISUAL',
['Vs'] = 'VISUAL',
['\22'] = 'VISUAL',
['\22s'] = 'VISUAL',
['s'] = 'SELECT',
['S'] = 'SELECT',
['\19'] = 'SELECT',
['i'] = 'INSERT',
['ic'] = 'INSERT',
['ix'] = 'INSERT',
['R'] = 'REPLACE',
['Rc'] = 'REPLACE',
['Rx'] = 'REPLACE',
['Rv'] = 'VIRT REPLACE',
['Rvc'] = 'VIRT REPLACE',
['Rvx'] = 'VIRT REPLACE',
['c'] = 'COMMAND',
['cv'] = 'VIM EX',
['ce'] = 'EX',
['r'] = 'PROMPT',
['rm'] = 'MORE',
['r?'] = 'CONFIRM',
['!'] = 'SHELL',
['t'] = 'TERMINAL',
}
-- Get the respective string to display.
local mode = mode_to_str[vim.api.nvim_get_mode().mode] or 'UNKNOWN'
-- Set the highlight group and text.
local mode_text = "?"
local hl = 'DiagnosticWarn'
if mode:find 'NORMAL' then
hl = 'DiagnosticInfo'
mode_text = "N"
elseif mode:find 'PENDING' then
hl = 'DiagnosticInfo'
mode_text = "P"
elseif mode:find 'VISUAL' then
hl = 'DiagnosticHint'
mode_text = "V"
elseif mode:find 'REPLACE' then
hl = 'DiagnosticError'
mode_text = "R"
elseif mode:find 'INSERT' or mode:find 'SELECT' then
hl = 'DiagnosticOk'
mode_text = "I"
elseif mode:find 'COMMAND' or mode:find 'TERMINAL' or mode:find 'EX' then
hl = 'DiagnosticHint'
mode_text = "C"
end
-- Construct the component.
return hi_pattern:format(hl, string.format(before..'%s'..after, mode_text))
end
M.components.location = function (args)
args = args or {}
local before = args.before or ""
local after = args.after or ""
return before.."%(%l/%L%): %c"..after
end
M.components.progress = function (args)
args = args or {}
local before = args.before or ""
local after = args.after or ""
-- Neovide breaks this component.
if vim.g.neovide then return "" end
local sbar = { '🭶', '🭷', '🭸', '🭹', '🭺', '🭻' }
local curr_line = vim.api.nvim_win_get_cursor(0)[1] or 0
local lines = vim.api.nvim_buf_line_count(0) or 0
local i = math.floor((curr_line - 1) / lines * #sbar) + 1
-- if i == nil then return "" end
local prog = string.rep(sbar[i], 2)
return before..hi_pattern:format("Visual", prog)..after
end
M.components.cwd = function (args)
args = args or {}
local before = args.before or ""
local after = args.after or ""
local cwd = vim.fn.getcwd(0)
cwd = vim.fn.fnamemodify(cwd, ":~")
if utils.component_takes_percentage(#cwd, 40) then cwd = vim.fn.pathshorten(cwd) end
if utils.component_takes_percentage(#cwd, 80) then return "" end
local trail = cwd:sub(-1) == '/' and '' or "/"
return before..cwd..trail..after
end
M.components.diagnostics = function (args)
args = args or {}
local before = args.before or ""
local after = args.after or ""
-- Define icons
local error_icon = use_icons and " " or "E"
local warning_icon = use_icons and " " or "W"
local info_icon = use_icons and " " or "I"
local hint_icon = use_icons and " " or "H"
-- Create empty diagnostics table
local diagnostics = {}
-- Count diagnostics
local errors = #vim.diagnostic.get(0, { severity = 1 })
if errors > 0 then
table.insert(diagnostics, hi_pattern:format("DiagnosticSignError", ("%s%s"):format(error_icon, errors)))
end
local warnings = #vim.diagnostic.get(0, { severity = 2 })
if warnings > 0 then
table.insert(diagnostics, hi_pattern:format("DiagnosticSignWarn", ("%s%s"):format(warning_icon, warnings)))
end
local infos = #vim.diagnostic.get(0, { severity = 3 })
if infos > 0 then
table.insert(diagnostics, hi_pattern:format("DiagnosticSignInfo", ("%s%s"):format(info_icon, infos)))
end
local hints = #vim.diagnostic.get(0, { severity = 4 })
if hints > 0 then
table.insert(diagnostics, hi_pattern:format("DiagnosticSignHint", ("%s%s"):format(hint_icon, hints)))
end
-- Don't show diagnostics in insert mode.
if vim.api.nvim_get_mode().mode:find "i" then
return ""
end
local icon = use_icons and '' or 'diag: '
local status = hi_pattern:format("Statusline", table.concat(diagnostics, " "))
return before..icon..status..after
end
M.components.git_branch = function (args)
args = args or {}
local before = args.before or ""
local after = args.after or ""
if not vim.b.minigit_summary then return "" end
local branch = vim.b.minigit_summary.head_name or ""
local icon = use_icons and " " or "branch: "
return before..icon..branch..after
end
M.components.git_status = function (args)
args = args or {}
local before = args.before or ""
local after = args.after or ""
if not vim.b.minidiff_summary then return "" end
local summary = vim.b.minidiff_summary
local status = {}
local add_icon = use_icons and " " or "+"
local change_icon = use_icons and " " or "~"
local delete_icon = use_icons and " " or "-"
if (summary.add or 0) > 0 then
table.insert(status, hi_pattern:format("Added", ("%s%s"):format(add_icon, summary.add)))
end
if (summary.change or 0) > 0 then
table.insert(status, hi_pattern:format("Changed", ("%s%s"):format(change_icon, summary.change)))
end
if (summary.delete or 0) > 0 then
table.insert(status, hi_pattern:format("Removed", ("%s%s"):format(delete_icon, summary.delete)))
end
return before..table.concat(status, " ")..after
end
M.components.tab_counter = function (args)
args = args or {}
local before = args.before or ""
local after = args.after or ""
local tab_list = vim.api.nvim_list_tabpages()
local num_tabs = #tab_list
local current_tab_name = vim.api.nvim_get_current_tabpage()
local current_tab_index = 1
for i = 1, #tab_list, 1 do
if tab_list[i] == current_tab_name then
current_tab_index = i
end
end
if num_tabs == 1 then return "" end
local icon = use_icons and " " or "tab: "
return before..icon..current_tab_index.."/"..num_tabs..after
end
M.components.session_name = function (args)
args = args or {}
local before = args.before or ""
local after = args.after or ""
if not vim.g.loaded_auto_session then
return ""
end
return before..require("auto-session.lib").current_session_name(true)..after
end
M.components.markdown_word_count = function (args)
args = args or {}
local before = args.before or ""
local after = args.after or ""
if vim.bo.filetype ~= "markdown" then
return ""
end
local word_count = vim.fn.wordcount().visual_words or vim.fn.wordcount().words
return before..word_count.." words"..after
end
-- Setup the statusline
function M.setup(opts)
opts = opts or {}
local statusline_components = opts.components or {
M.components.mode({before = " "}),
M.components.git_branch(),
M.components.cwd(),
"%=",
M.components.session_name(),
M.components.markdown_word_count(),
M.components.tab_counter(),
M.components.location({after = vim.g.neovide and " " or ""}),
M.components.progress({after = ""}),
}
local gaps = opts.gaps or " "
-- Global function to construct the line
Statusline_builder = function ()
--get a table of all the statusline strings
local statusline_strings = statusline_components
-- Remove empty strings to prevent concat issues
for i = #statusline_strings, 1, -1 do
if statusline_strings[i] == "" then
table.remove(statusline_strings, i)
end
end
-- Concatentate strings with gaps
return table.concat(statusline_strings, gaps)
end
-- Tell vim to call the function to construct the line
vim.o.statusline = "%{%v:lua.Statusline_builder()%}"
end
return M
|