forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 3
/
badge.lua
187 lines (168 loc) · 4.04 KB
/
badge.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
vim.api.nvim_exec([[
augroup badge_lua_cache
autocmd!
autocmd BufWritePre,FileChangedShellPost,TextChanged,InsertLeave * unlet! b:badge_cache_trails
autocmd BufReadPost,BufFilePost,BufNewFile,BufWritePost * unlet! b:badge_cache_filepath | unlet! b:badge_cache_icon
augroup END
]], false)
local M = {}
function M.filepath(max_dirs, dir_max_chars)
return function()
local msg = ''
-- local ft = vim.bo.filetype
local name = vim.fn.expand('%:~:.')
local cache_key = 'badge_cache_filepath' -- _'..ft
local cache_ok, cache = pcall(vim.api.nvim_buf_get_var, 0, cache_key)
if cache_ok then
return cache
elseif name:len() < 1 then
return 'N/A'
end
local i = 0
local parts = {}
local iter = string.gmatch(name, '([^/]+)')
for dir in iter do
table.insert(parts, dir)
end
while #parts > 1 do
local dir = table.remove(parts, 1)
if #parts <= max_dirs then
dir = string.sub(dir, 0, dir_max_chars)
if i > 0 then
msg = msg .. '/'
end
msg = msg .. dir
i = i + 1
end
end
if i > 0 then
msg = msg .. '/'
end
msg = msg .. table.concat(parts, '/')
vim.api.nvim_buf_set_var(0, cache_key, msg)
return msg
end
end
function M.filemode(normal_symbol, readonly_symbol, zoom_symbol)
return function()
local msg = ''
if not (vim.bo.readonly or vim.t.zoomed) then
msg = msg .. normal_symbol
end
if vim.bo.buftype == '' and vim.bo.readonly then
msg = msg .. readonly_symbol
end
if vim.t.zoomed then
msg = msg .. zoom_symbol
end
return msg
end
end
function M.filemedia(separator)
return function()
local parts = {}
if vim.bo.fileformat ~= '' and vim.bo.fileformat ~= 'unix' then
table.insert(parts, vim.bo.fileformat)
end
if vim.bo.fileencoding ~= '' and vim.bo.fileencoding ~= 'utf-8' then
table.insert(parts, vim.bo.fileencoding)
end
if vim.bo.filetype ~= '' then
table.insert(parts, vim.bo.filetype)
end
return table.concat(parts, separator)
end
end
function M.modified(symbol)
return function()
if vim.bo.modified then
return symbol
end
return ''
end
end
function M.icon()
return function()
local ft = vim.bo.filetype
if #ft < 1 then
return ''
end
local cache_key = 'badge_cache_icon' -- _'..ft
local cache_ok, cache = pcall(vim.api.nvim_buf_get_var, 0, cache_key)
if cache_ok then
return cache
end
local icon = ''
-- TODO: Add general utilities icons
-- Try kyazdani42/nvim-web-devicons
local ok, devicons = pcall(require, 'nvim-web-devicons')
if ok then
local f_name, f_extension = vim.fn.expand('%:t'), vim.fn.expand('%:e')
icon, _ = devicons.get_icon(f_name, f_extension)
else
-- Try ryanoasis/vim-devicons
ok = vim.fn.exists('*WebDevIconsGetFileTypeSymbol')
if ok ~= 0 then
icon = vim.fn.WebDevIconsGetFileTypeSymbol()
else
-- Try lambdalisue/nerdfont.vim
ok = vim.fn.exists('*nerdfont#find')
if ok ~= 0 then
icon = vim.fn['nerdfont#find'](vim.fn.bufname())
end
end
end
vim.api.nvim_buf_set_var(0, cache_key, icon)
return icon
end
end
-- Detect trailing whitespace and cache result per buffer
function M.trails(symbol)
return function()
local cache_key = 'badge_cache_trails'
local cache_ok, cache = pcall(vim.api.nvim_buf_get_var, 0, cache_key)
if cache_ok then
return cache
end
local msg = ''
if
not vim.bo.readonly
and vim.bo.modifiable
and vim.fn.line('$') < 9000
then
local trailing = vim.fn.search('\\s$', 'nw')
if trailing > 0 then
local label = symbol or 'WS:'
msg = msg .. label .. trailing
end
end
vim.api.nvim_buf_set_var(0, cache_key, msg)
return msg
end
end
function M.progress()
return function()
return '%l/%2c%4p%%'
end
end
function M.session(symbol)
return function()
if vim.v.this_session then
return symbol
end
return ''
end
end
function M.utility_title()
return function()
local icons = {
Trouble = '',
DiffviewFiles = '',
NeogitStatus = '',
Outline = '',
}
local padding = vim.g.global_symbol_padding or ' '
return icons[vim.bo.filetype] .. padding .. '%y'
end
end
return M