Skip to content

Commit 7232fab

Browse files
authored
small stylistic improvements? eg (#102)
- define defaults for filetype, encoding - use helper functions to avoid inserting lualine elements after sections = {..} definition with table.insert - use cond= deternmine visibilty instead of including that in display logic.
1 parent 756d8d5 commit 7232fab

File tree

1 file changed

+109
-81
lines changed

1 file changed

+109
-81
lines changed

lua/vnext/plugins/statusline.lua

Lines changed: 109 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,78 @@
1+
-- Lualine configuration with helper functions
2+
3+
-- Configuration constants
4+
local DEFAULT_ENCODING = "utf-8"
5+
local DEFAULT_FILEFORMAT = "unix"
6+
local WORDCOUNT_FILETYPES = {
7+
latex = true,
8+
tex = true,
9+
text = true,
10+
markdown = true,
11+
vimwiki = true,
12+
rst = true,
13+
asciidoc = true,
14+
org = true,
15+
}
16+
17+
-- Helper functions with better error handling and robustness
18+
local function is_not_unix_fileformat()
19+
local format = vim.bo.fileformat
20+
return format and format ~= DEFAULT_FILEFORMAT
21+
end
22+
23+
local function is_not_utf8_coding()
24+
local encoding = vim.bo.fileencoding
25+
-- Handle empty encoding (which defaults to utf-8 in nvim)
26+
return encoding and encoding ~= "" and encoding ~= DEFAULT_ENCODING
27+
end
28+
29+
local function is_macro_recording()
30+
local rec = vim.fn.reg_recording()
31+
return rec and rec ~= ""
32+
end
33+
34+
local function get_macro_recording()
35+
local reg = vim.fn.reg_recording()
36+
if not reg or reg == "" then
37+
return ""
38+
end
39+
return "󰑋 " .. reg
40+
end
41+
42+
local function get_word_count()
43+
-- Protected call to handle potential errors
44+
local ok, wc = pcall(vim.fn.wordcount)
45+
if not ok or not wc then
46+
return ""
47+
end
48+
49+
-- Handle visual mode selection
50+
if wc.visual_words and wc.visual_chars then
51+
return string.format("%d Words/%d Chars (Vis)", wc.visual_words, wc.visual_chars)
52+
end
53+
54+
-- Handle regular document word count
55+
if wc.words then
56+
return string.format("%d Words", wc.words)
57+
end
58+
return ""
59+
end
60+
61+
local function is_document_filetype()
62+
local filetype = vim.bo.filetype
63+
return filetype and WORDCOUNT_FILETYPES[filetype] or false
64+
end
65+
66+
-- Main configuration
167
return {
268
"nvim-lualine/lualine.nvim",
369
event = "BufReadPost",
470
opts = {
571
extensions = { "lazy", "quickfix", "neo-tree" },
672
options = {
7-
disabled_filetypes = { statusline = { "neo-tree", "Outline", "snacks_picker_list" } },
73+
disabled_filetypes = {
74+
statusline = { "neo-tree", "Outline", "snacks_picker_list" },
75+
},
876
},
977
sections = {
1078
lualine_a = {}, -- hide mode
@@ -17,92 +85,52 @@ return {
1785
lualine_c = {
1886
{
1987
"filename",
20-
file_status = true, -- Displays file status (readonly status, modified status)
21-
newfile_status = true, -- Display new file status (new file means no write after created)
22-
path = 3, -- 0: Just the filename
23-
-- 1: Relative path
24-
-- 2: Absolute path
25-
-- 3: Absolute path, with tilde as the home directory
26-
-- 4: Filename and parent dir, with tilde as the home directory
88+
file_status = true,
89+
newfile_status = true,
90+
--- path 0: filename 1: rel path 2: abs path 3: abs path with ~ 4: fn with parent dir
91+
path = 3,
2792
},
2893
},
29-
lualine_x = { "searchcount", "filetype" },
30-
lualine_y = { "progress" },
94+
lualine_x = {
95+
{
96+
"encoding",
97+
cond = is_not_utf8_coding,
98+
show_bomb = true,
99+
},
100+
{
101+
"fileformat",
102+
cond = is_not_unix_fileformat,
103+
},
104+
"searchcount",
105+
"filetype",
106+
{
107+
get_macro_recording,
108+
cond = is_macro_recording,
109+
color = { fg = "#333333", bg = "#ff6666" },
110+
separator = { left = "", right = "" },
111+
},
112+
},
113+
lualine_y = {
114+
{
115+
get_word_count,
116+
cond = is_document_filetype,
117+
icon = "󰈭",
118+
},
119+
"progress",
120+
},
31121
lualine_z = { "location" },
32122
},
123+
-- inactive sections to match
124+
inactive_sections = {
125+
lualine_a = {},
126+
lualine_b = {},
127+
lualine_c = { "filename" },
128+
lualine_x = { "location" },
129+
lualine_y = {},
130+
lualine_z = {},
131+
},
33132
},
34133
config = function(_, opts)
35-
-- Show info when recording a macro
36-
local function is_macro_recording()
37-
local reg = vim.fn.reg_recording()
38-
if reg == "" then
39-
return ""
40-
end
41-
return "󰑋 " .. reg
42-
end
43-
44-
table.insert(opts.sections.lualine_x, 1, {
45-
is_macro_recording,
46-
color = { fg = "#333333", bg = "#ff6666" },
47-
separator = { left = "", right = "" },
48-
cond = function()
49-
return is_macro_recording() ~= ""
50-
end,
51-
})
52-
53-
-- Don't display encoding if encoding is UTF-8
54-
local function encoding()
55-
local ret, _ = (vim.bo.fenc or vim.go.enc):gsub("^utf%-8$", "")
56-
return ret
57-
end
58-
59-
table.insert(opts.sections.lualine_x, 1, {
60-
encoding,
61-
cond = function()
62-
return encoding() ~= ""
63-
end,
64-
})
65-
66-
-- Don't display fileformat if fileformat is unix
67-
local function fileformat()
68-
local ret, _ = vim.bo.fileformat:gsub("^unix$", "")
69-
return ret
70-
end
71-
72-
table.insert(opts.sections.lualine_x, 1, {
73-
fileformat,
74-
cond = function()
75-
return fileformat() ~= ""
76-
end,
77-
})
78-
79-
local function wordCount()
80-
local wc = vim.fn.wordcount()
81-
if wc == nil then
82-
return ""
83-
end
84-
if wc["visual_words"] then -- text is selected in visual mode
85-
return wc["visual_words"] .. " Words/" .. wc["visual_chars"] .. " Chars (Vis)"
86-
else -- all of the document
87-
return wc["words"] .. " Words"
88-
end
89-
end
90-
91-
table.insert(opts.sections.lualine_y, 1, {
92-
wordCount,
93-
cond = function()
94-
local ft = vim.bo.filetype
95-
local count = {
96-
latex = true,
97-
tex = true,
98-
text = true,
99-
markdown = true,
100-
vimwiki = true,
101-
}
102-
return count[ft] ~= nil
103-
end,
104-
})
105-
106134
require("lualine").setup(opts)
107135
end,
108136
}

0 commit comments

Comments
 (0)