REFACTOR: Opencode heavily optimized statusline

This commit is contained in:
2026-10-10 01:55:48 -04:00
parent 1356ec1860
commit 10ef727e6f
2 changed files with 165 additions and 182 deletions
@@ -1,203 +1,186 @@
local has_devicons, devicons = pcall(require, 'nvim-web-devicons') local ok, D = pcall(require, 'nvim-web-devicons')
local uv, api, b = vim.uv or vim.loop, vim.api, vim.b local a, u, S = vim.api, vim.uv or vim.loop, vim.diagnostic.severity
local SEV = vim.diagnostic.severity local C = a.nvim_get_current_buf
local R = function()
local function setup_highlights() vim.cmd.redrawstatus()
local bg = api.nvim_get_hl(0, { name = 'StatusLine' }).bg or 'none'
local function set(name, link, bold)
local hl = api.nvim_get_hl(0, { name = link, link = false })
api.nvim_set_hl(0, name, { fg = hl.fg, bg = bg, bold = bold or false })
end end
local M = '%#StatusLineMod#%m%*%#StatusLineRO#%r%* %='
set('StatusLineFilename', 'Normal', true) local FR = { '⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏' }
set('StatusLineMod', 'WarningMsg', true) local N, SPIN, LOAD = #FR, '%#StatusLspSpinner#', '%* %#StatusLspLoading#'
set('StatusLineRO', 'ErrorMsg', true) local c, T, JOB, TMR, IDX = {}, {}, nil, nil, 1
set('StatusDiagError', 'DiagnosticError', true) local function HI()
set('StatusDiagWarn', 'DiagnosticWarn', false) local bg = a.nvim_get_hl(0, { name = 'StatusLine' }).bg or 'none'
set('StatusDiagHint', 'DiagnosticHint', false) for _, t in ipairs({
set('StatusLspSpinner', 'DiagnosticWarn', true) { 'StatusLineFilename', 'Normal', true },
set('StatusLspLoading', 'DiagnosticInfo', false) { 'StatusLineMod', 'WarningMsg', true },
set('StatusLspReady', 'Comment', false) { 'StatusLineRO', 'ErrorMsg', true },
end { 'StatusDiagError', 'DiagnosticError', true },
{ 'StatusDiagWarn', 'DiagnosticWarn', false },
api.nvim_create_autocmd({ 'ColorScheme', 'FocusGained', 'VimEnter' }, { { 'StatusDiagHint', 'DiagnosticHint', false },
callback = function() { 'StatusLspSpinner', 'DiagnosticWarn', true },
setup_highlights() { 'StatusLspLoading', 'DiagnosticInfo', false },
vim.cmd('redrawstatus') { 'StatusLspReady', 'Comment', false },
end, }) do
}) local h = a.nvim_get_hl(0, { name = t[2], link = false })
setup_highlights() a.nvim_set_hl(0, t[1], { fg = h.fg, bg = bg, bold = t[3] })
local function update_file_info(buf)
local path = api.nvim_buf_get_name(buf)
if path == '' then
b[buf].stl_file_info = ' %#StatusLineFilename#[No Name]%*'
return
end
local fname = path:match('([^/\\]+)$') or path
local icon = ''
if has_devicons then
local ic, hl = devicons.get_icon(
fname,
path:match('%.([^./\\]+)$'),
{ default = true }
)
if ic then
icon = '%#' .. hl .. '#' .. ic .. ' %*'
end end
end end
b[buf].stl_file_info = ' ' local function snap(bb)
.. icon local p = a.nvim_buf_get_name(bb)
.. '%#StatusLineFilename#' local nm = ' %#StatusLineFilename#'
.. fname if p == '' then
.. '%*' nm = nm .. '[No Name]%*'
end
local function update_lsp_clients(buf)
local clients = vim.lsp.get_clients({ bufnr = buf })
if #clients == 0 then
b[buf].stl_lsp_ready = ''
return
end
local names = {}
for i = 1, #clients do
names[i] = clients[i].name
end
b[buf].stl_lsp_ready = '%#StatusLspReady#󰄭 '
.. table.concat(names, '/')
.. '%* '
end
local active_tokens = {}
local spinner_frames =
{ '⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏' }
local current_job, spinner_timer = nil, nil
local function sync_spinner()
current_job = nil
for _, tokens in pairs(active_tokens) do
for _, msg in pairs(tokens) do
current_job = msg
break
end
if current_job then
break
end
end
if current_job and not spinner_timer then
spinner_timer = uv.new_timer()
spinner_timer:start(
0,
100,
vim.schedule_wrap(function()
if current_job then
vim.cmd('redrawstatus')
elseif spinner_timer then
spinner_timer:stop()
spinner_timer:close()
spinner_timer = nil
end
end)
)
elseif not current_job and spinner_timer then
spinner_timer:stop()
spinner_timer:close()
spinner_timer = nil
vim.cmd('redrawstatus')
end
end
local group = api.nvim_create_augroup('MinimalStatusline', { clear = true })
api.nvim_create_autocmd({ 'BufEnter', 'BufFilePost' }, {
group = group,
callback = function(ev)
update_file_info(ev.buf)
update_lsp_clients(ev.buf)
end,
})
api.nvim_create_autocmd('DiagnosticChanged', {
group = group,
callback = function(ev)
if ev.buf == api.nvim_get_current_buf() then
vim.cmd('redrawstatus')
end
end,
})
api.nvim_create_autocmd({ 'LspAttach', 'LspDetach' }, {
group = group,
callback = function(ev)
if ev.event == 'LspDetach' then
active_tokens[ev.data.client_id] = nil
sync_spinner()
end
update_lsp_clients(ev.buf)
vim.cmd('redrawstatus')
end,
})
api.nvim_create_autocmd('LspProgress', {
group = group,
callback = function(ev)
local val = ev.data.params.value
if type(val) ~= 'table' then
return
end
local client_id = ev.data.client_id
local token = ev.data.params.token or 'default'
active_tokens[client_id] = active_tokens[client_id] or {}
if
val.kind == 'end'
or (val.kind == 'report' and val.percentage == 100)
then
active_tokens[client_id][token] = nil
else else
local msg = (val.title and val.title ~= '' and val.title) local f = p:match('([^/\\]+)$') or p
or val.message if ok then
or 'Loading...' local i, hl = D.get_icon(f, p:match('%.([^./\\]+)$'), { default = true })
if val.title and val.message and val.message ~= '' then if i then
msg = val.title .. ' ' .. val.message nm = nm .. '%#' .. hl .. '#' .. i .. ' %*'
end end
active_tokens[client_id][token] = msg
end end
nm = nm .. f .. '%*'
sync_spinner() end
end, local x = vim.diagnostic.count(bb)
}) local e, w, h = x[S.ERROR] or 0, x[S.WARN] or 0, x[S.HINT] or 0
local dg = ''
function MinimalStatusline() if e + w + h > 0 then
local cur_buf = api.nvim_get_current_buf() dg = (e > 0 and ('%#StatusDiagError#E' .. e .. '%* ') or '')
local vars = b[cur_buf]
local c = vim.diagnostic.count(cur_buf)
local e, w, h = c[SEV.ERROR] or 0, c[SEV.WARN] or 0, c[SEV.HINT] or 0
local diags = ''
if e > 0 or w > 0 or h > 0 then
diags = (e > 0 and ('%#StatusDiagError#E' .. e .. '%* ') or '')
.. (w > 0 and ('%#StatusDiagWarn#W' .. w .. '%* ') or '') .. (w > 0 and ('%#StatusDiagWarn#W' .. w .. '%* ') or '')
.. (h > 0 and ('%#StatusDiagHint#H' .. h .. '%* ') or '') .. (h > 0 and ('%#StatusDiagHint#H' .. h .. '%* ') or '')
end end
local rd, cs = '', vim.lsp.get_clients({ bufnr = bb })
local right = current_job if #cs > 0 then
and ('%#StatusLspSpinner#' .. spinner_frames[(math.floor( local nm2 = {}
uv.now() / 100 for i = 1, #cs do
) % 10) + 1] .. '%* %#StatusLspLoading#' .. current_job .. '%* ') nm2[i] = cs[i].name
or (vars.stl_lsp_ready or '')
return (vars.stl_file_info or '')
.. '%#StatusLineMod#%m%*%#StatusLineRO#%r%* %='
.. diags
.. '%='
.. right
end end
rd = '%#StatusLspReady#󰄭 ' .. table.concat(nm2, '/') .. '%* '
vim.opt.laststatus = 3 end
vim.opt.statusline = '%!v:lua.MinimalStatusline()' return nm .. M .. dg, rd
end
local function touch(bb, redraw)
local v = c[bb]
if v then
v.d = true
end
if redraw then
R()
end
end
local function SYNC()
JOB = nil
for _, t in pairs(T) do
for _, m in pairs(t) do
JOB = m
break
end
if JOB then
break
end
end
if JOB and not TMR then
IDX = math.floor(u.now() / 100) % N + 1
TMR = u.new_timer()
TMR:start(
0,
100,
vim.schedule_wrap(function()
if JOB then
IDX = IDX % N + 1
R()
elseif TMR then
TMR:stop()
TMR:close()
TMR = nil
end
end)
)
elseif not JOB and TMR then
TMR:stop()
TMR:close()
TMR = nil
R()
end
end
local G = a.nvim_create_augroup('STL5', { clear = true })
for _, s in ipairs({
{
{ 'BufEnter', 'BufFilePost' },
function(e)
touch(e.buf, true)
end,
},
{
'DiagnosticChanged',
function(e)
touch(e.buf, e.buf == C())
end,
},
{
{ 'LspAttach', 'LspDetach' },
function(e)
if e.event == 'LspDetach' then
T[e.data.client_id] = nil
SYNC()
end
touch(e.buf, true)
end,
},
{
'LspProgress',
function(e)
local v = e.data.params.value
if type(v) ~= 'table' then
return
end
local id, tk = e.data.client_id, e.data.params.token or 'default'
T[id] = T[id] or {}
if v.kind == 'end' or (v.kind == 'report' and v.percentage == 100) then
T[id][tk] = nil
else
local m = (v.title and v.title ~= '' and v.title) or v.message or 'Loading...'
if v.title and v.message and v.message ~= '' then
m = v.title .. ' ' .. v.message
end
T[id][tk] = m
end
SYNC()
end,
},
{
'BufUnload',
function(e)
c[e.buf] = nil
end,
},
{
'ColorScheme',
function()
HI()
R()
end,
},
{ 'FocusGained', R },
}) do
a.nvim_create_autocmd(s[1], { group = G, callback = s[2] })
end
HI()
local function Status()
local bb = C()
local v = c[bb]
if not v or v.d then
if not v then
v = {}
c[bb] = v
end
v.h, v.r = snap(bb)
v.stl = v.h .. v.r
v.d = nil
end
if JOB then
return v.h .. SPIN .. FR[IDX] .. LOAD .. JOB .. '%* '
end
return v.stl
end
_G.Status = Status
vim.opt.statusline = '%!v:lua.Status()'
+2 -2
View File
@@ -1,5 +1,5 @@
column_width = 80 column_width = 120
line_endings = "Unix" line_endings = "Unix"
indent_type = "Tabs" indent_type = "Tabs"
indent_width = 4 indent_width = 2
quote_style = "AutoPreferSingle" quote_style = "AutoPreferSingle"