refactor buffer winbar

This commit is contained in:
xfy
2025-02-11 14:27:54 +08:00
parent 9efadcf8a6
commit ffdb551edd

View File

@ -81,57 +81,43 @@ autocmd("BufReadPost", {
end, end,
}) })
-- This add the bar that shows the file path on the top right -- Function to get the full path and replace the home directory with ~
-- vim.opt.winbar = "%=%m %f" local function get_winbar_path()
-- local relative_filepath = vim.fn.expand("%:.")
-- Modified version of the bar, shows pathname on right, hostname left return relative_filepath
-- vim.opt.winbar = "%=" .. vim.fn.systemlist("hostname")[1] .. " %m %f" end
-- -- Function to get the number of open buffers using vim.fn.getbufinfo()
-- This shows pathname on the left and hostname on the right local function get_buffer_count()
-- vim.opt.winbar = "%m %f%=" .. vim.fn.systemlist("hostname")[1] local buffers = vim.fn.getbufinfo({ buflisted = 1 })
-- return #buffers
end
-- Function to get the hostname with error handling
local function get_hostname()
local hostname = vim.fn.systemlist("hostname")
if #hostname > 0 then
return hostname[1]
else
return "unknown"
end
end
-- Using different colors, defining the colors in this file -- Using different colors, defining the colors in this file
-- local colors = require("config.colors").load_colors() -- local colors = require("config.colors").load_colors()
-- vim.cmd(string.format([[highlight WinBar1 guifg=%s]], colors["linkarzu_color03"])) -- vim.cmd(string.format([[highlight WinBar1 guifg=%s]], colors["linkarzu_color03"]))
-- vim.cmd(string.format([[highlight WinBar2 guifg=%s]], colors["linkarzu_color02"])) -- vim.cmd(string.format([[highlight WinBar2 guifg=%s]], colors["linkarzu_color02"]))
-- Function to get the full path and replace the home directory with ~
local function get_winbar_path()
-- local filename = vim.fn.expand("%:t")
-- local absolute_filepath = vim.fn.expand("%:p")
local relative_filepath = vim.fn.expand("%:.")
-- local full_path = vim.fn.expand("%:p")
-- return full_path:gsub(vim.fn.expand("$HOME"), "~")
return relative_filepath
end
-- Function to get the number of open buffers using the :ls command
local function get_buffer_count()
local buffers = vim.fn.execute("ls")
local count = 0
-- Match only lines that represent buffers, typically starting with a number followed by a space
for line in string.gmatch(buffers, "[^\r\n]+") do
if string.match(line, "^%s*%d+") then
count = count + 1
end
end
return count
end
-- Function to update the winbar -- Function to update the winbar
local function update_winbar() local function update_winbar()
local home_replaced = get_winbar_path() local home_replaced = get_winbar_path()
local buffer_count = get_buffer_count() local buffer_count = get_buffer_count()
local ft = vim.bo.filetype local ft = vim.bo.filetype
local hostname = get_hostname()
if ft == "NvimTree" then if ft == "NvimTree" then
vim.opt.winbar = "RUA" vim.opt.winbar = "RUA"
return return
else else
vim.opt.winbar = "%#WinBar1#%m " local winbar_prefix = "%#WinBar1#%m "
.. "%#WinBar2#(" local buffer_count_str = "%#WinBar2#(" .. buffer_count .. ") "
.. buffer_count local winbar_suffix = "%*%=%#WinBar2#" .. hostname
.. ") " vim.opt.winbar = winbar_prefix .. buffer_count_str .. "%#WinBar1#" .. home_replaced .. winbar_suffix
.. "%#WinBar1#"
.. home_replaced
.. "%*%=%#WinBar2#"
.. vim.fn.systemlist("hostname")[1]
end end
end end
-- Autocmd to update the winbar on BufEnter and WinEnter events -- Autocmd to update the winbar on BufEnter and WinEnter events