Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add BOM indicator to encoding component #1228

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading