From e917677a3ad90d1229c6b6cbbc8967a03a7ee9ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Augusto?= Date: Thu, 21 Oct 2021 10:16:12 -0300 Subject: [PATCH] enhance: change method of getting file encoding Currently, lualine gets the encoding using this: [[%{strlen(&fenc)?&fenc:&enc}]] My suggestion is to change this string to `vim.opt.fileencoding:get()` This suggestion is because I would like to use the option "fmt = string.upper", and with the current string it doesn't work, but using vim.opt function correctly --- lua/lualine/components/encoding.lua | 2 +- lua/tests/spec/component_spec.lua | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lua/lualine/components/encoding.lua b/lua/lualine/components/encoding.lua index f40aecc..80a5ece 100644 --- a/lua/lualine/components/encoding.lua +++ b/lua/lualine/components/encoding.lua @@ -1,7 +1,7 @@ -- Copyright (c) 2020-2021 hoob3rt -- MIT license, see LICENSE for more details. local function encoding() - return [[%{strlen(&fenc)?&fenc:&enc}]] + return vim.opt.fileencoding:get() end return encoding diff --git a/lua/tests/spec/component_spec.lua b/lua/tests/spec/component_spec.lua index 18ed4a8..1697ae9 100644 --- a/lua/tests/spec/component_spec.lua +++ b/lua/tests/spec/component_spec.lua @@ -210,7 +210,13 @@ describe('Encoding component', function() component_separators = { left = '', right = '' }, padding = 0, } - assert_component('encoding', opts, '%{strlen(&fenc)?&fenc:&enc}') + 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) + assert_component('encoding', opts, 'utf-8') + os.remove(tmp_path) end) end)