Skip to content

Commit

Permalink
feat(ui): fancy status column
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 3, 2023
1 parent 3f868aa commit 364bcf3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lua/lazyvim/config/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ else
end
if vim.treesitter.foldtext then
vim.opt.foldtext = "v:lua.vim.treesitter.foldtext()"
if vim.fn.has("nvim-0.9.0") == 1 then
vim.opt.statuscolumn = [[%!v:lua.require'lazyvim.util.ui'.statuscolumn()]]
end

-- Fix markdown indentation settings
Expand Down
57 changes: 57 additions & 0 deletions lua/lazyvim/util/ui.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
local M = {}

---@alias Sign {name:string, text:string, texthl:string}

---@return Sign[]
function M.get_signs(win)
local buf = vim.api.nvim_win_get_buf(win)
---@diagnostic disable-next-line: no-unknown
return vim.tbl_map(function(sign)
return vim.fn.sign_getdefined(sign.name)[1]
end, vim.fn.sign_getplaced(buf, { group = "*", lnum = vim.v.lnum })[1].signs)
end

---@param sign? Sign
---@param len? number
function M.icon(sign, len)
sign = sign or {}
len = len or 1
local text = vim.fn.strcharpart(sign.text or "", 0, len, false) ---@type string
text = text .. string.rep(" ", len - vim.fn.strchars(text))
return sign.texthl and ("%#" .. sign.texthl .. "#" .. text .. "%*") or text
end

function M.statuscolumn()
local win = vim.g.statusline_winid
if vim.wo[win].signcolumn == "no" then
return ""
end

---@type Sign?,Sign?,Sign?
local left, right, fold
for _, s in ipairs(M.get_signs(win)) do
if s.name:find("GitSign") then
right = s
elseif not left then
left = s
end
end

if vim.fn.foldclosed(vim.v.lnum) >= 0 then
fold = { text = vim.opt.fillchars:get().foldclose or "", texthl = "Folded" }
end

local nu = ""
if vim.wo[win].number and vim.v.virtnum == 0 then
nu = vim.wo[win].relativenumber and vim.v.relnum ~= 0 and vim.v.relnum or vim.v.lnum
end

return table.concat({
M.icon(left),
[[%=]],
nu .. " ",
M.icon(fold or right, 2),
}, "")
end

return M

0 comments on commit 364bcf3

Please sign in to comment.