Skip to content

Commit

Permalink
feat: Add BOM indicator to encoding component (#1228)
Browse files Browse the repository at this point in the history
* feat: Add BOM indicator to encoding component

* feat: Make BOM indicator optional

* chore: Add more tests for encoding component
  • Loading branch information
sergiuser1 authored Jul 15, 2024
1 parent 6a40b53 commit be2d876
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,20 @@ sections = {
}
```

#### encoding component options

```lua
sections = {
lualine_a = {
{
'encoding',
-- Show '[BOM]' when the file has a byte-order mark
show_bomb = false,
}
}
}
```

#### searchcount component options

```lua
Expand Down
31 changes: 28 additions & 3 deletions lua/lualine/components/encoding.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
-- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details.
local function encoding()
return vim.opt.fileencoding:get()
local lualine_require = require('lualine_require')
local M = lualine_require.require('lualine.component'):extend()

local default_options = {
-- Show '[BOM]' when the file has a byte-order mark
show_bomb = false,
}

function M:init(options)
M.super.init(self, options)
self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options)
end

function M:update_status()
local show_bomb = self.options.show_bomb

local result = vim.opt.fileencoding:get()

if not show_bomb then
return result
end

if vim.opt.bomb:get() then
result = result .. ' [BOM]'
end

return result
end

return encoding
return M
31 changes: 31 additions & 0 deletions tests/spec/component_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,37 @@ describe('Encoding component', function()
vim.cmd('bd!')
os.remove(tmp_path)
end)
it('works with BOM and default config', function()
local opts = build_component_opts {
component_separators = { left = '', right = '' },
padding = 0,
}
local tmp_path = 'tmp.txt'
local tmp_fp = io.open(tmp_path, 'w')
tmp_fp:write('test file')
tmp_fp:close()
vim.cmd('e ' .. tmp_path)
vim.cmd('set bomb')
assert_component('encoding', opts, 'utf-8')
vim.cmd('bd!')
os.remove(tmp_path)
end)
it('works with BOM and option enabled', function()
local opts = build_component_opts {
component_separators = { left = '', right = '' },
padding = 0,
show_bomb = true
}
local tmp_path = 'tmp.txt'
local tmp_fp = io.open(tmp_path, 'w')
tmp_fp:write('test file')
tmp_fp:close()
vim.cmd('e ' .. tmp_path)
vim.cmd('set bomb')
assert_component('encoding', opts, 'utf-8 [BOM]')
vim.cmd('bd!')
os.remove(tmp_path)
end)
end)

describe('Fileformat component', function()
Expand Down

0 comments on commit be2d876

Please sign in to comment.