Skip to content

Commit be2d876

Browse files
authored
feat: Add BOM indicator to encoding component (#1228)
* feat: Add BOM indicator to encoding component * feat: Make BOM indicator optional * chore: Add more tests for encoding component
1 parent 6a40b53 commit be2d876

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,20 @@ sections = {
670670
}
671671
```
672672

673+
#### encoding component options
674+
675+
```lua
676+
sections = {
677+
lualine_a = {
678+
{
679+
'encoding',
680+
-- Show '[BOM]' when the file has a byte-order mark
681+
show_bomb = false,
682+
}
683+
}
684+
}
685+
```
686+
673687
#### searchcount component options
674688

675689
```lua

lua/lualine/components/encoding.lua

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
-- Copyright (c) 2020-2021 hoob3rt
22
-- MIT license, see LICENSE for more details.
3-
local function encoding()
4-
return vim.opt.fileencoding:get()
3+
local lualine_require = require('lualine_require')
4+
local M = lualine_require.require('lualine.component'):extend()
5+
6+
local default_options = {
7+
-- Show '[BOM]' when the file has a byte-order mark
8+
show_bomb = false,
9+
}
10+
11+
function M:init(options)
12+
M.super.init(self, options)
13+
self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options)
14+
end
15+
16+
function M:update_status()
17+
local show_bomb = self.options.show_bomb
18+
19+
local result = vim.opt.fileencoding:get()
20+
21+
if not show_bomb then
22+
return result
23+
end
24+
25+
if vim.opt.bomb:get() then
26+
result = result .. ' [BOM]'
27+
end
28+
29+
return result
530
end
631

7-
return encoding
32+
return M

tests/spec/component_spec.lua

+31
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,37 @@ describe('Encoding component', function()
242242
vim.cmd('bd!')
243243
os.remove(tmp_path)
244244
end)
245+
it('works with BOM and default config', function()
246+
local opts = build_component_opts {
247+
component_separators = { left = '', right = '' },
248+
padding = 0,
249+
}
250+
local tmp_path = 'tmp.txt'
251+
local tmp_fp = io.open(tmp_path, 'w')
252+
tmp_fp:write('test file')
253+
tmp_fp:close()
254+
vim.cmd('e ' .. tmp_path)
255+
vim.cmd('set bomb')
256+
assert_component('encoding', opts, 'utf-8')
257+
vim.cmd('bd!')
258+
os.remove(tmp_path)
259+
end)
260+
it('works with BOM and option enabled', function()
261+
local opts = build_component_opts {
262+
component_separators = { left = '', right = '' },
263+
padding = 0,
264+
show_bomb = true
265+
}
266+
local tmp_path = 'tmp.txt'
267+
local tmp_fp = io.open(tmp_path, 'w')
268+
tmp_fp:write('test file')
269+
tmp_fp:close()
270+
vim.cmd('e ' .. tmp_path)
271+
vim.cmd('set bomb')
272+
assert_component('encoding', opts, 'utf-8 [BOM]')
273+
vim.cmd('bd!')
274+
os.remove(tmp_path)
275+
end)
245276
end)
246277

247278
describe('Fileformat component', function()

0 commit comments

Comments
 (0)