Skip to content

Instantly share code, notes, and snippets.

@s1n7ax
Created May 22, 2022 09:44
Show Gist options
  • Save s1n7ax/3d2b476ecbde72693572b8652044e5a4 to your computer and use it in GitHub Desktop.
Save s1n7ax/3d2b476ecbde72693572b8652044e5a4 to your computer and use it in GitHub Desktop.
Show file path in the winbar
vim.o.winbar = "%{%v:lua.require'nvim.utils.nvim.winbar'.eval()%}"
local M = {}
vim.api.nvim_set_hl(0, 'WinBarPath', { bg = '#dedede', fg = '#363636' })
vim.api.nvim_set_hl(0, 'WinBarModified', { bg = '#dedede', fg = '#ff3838' })
function M.eval()
local file_path = vim.api.nvim_eval_statusline('%f', {}).str
local modified = vim.api.nvim_eval_statusline('%M', {}).str == '+' and '⊚' or ''
file_path = file_path:gsub('/', ' ➤ ')
return '%#WinBarPath#'
.. file_path
.. '%*'
.. '%#WinBarModified#'
.. modified
.. '%*'
end
return M
@voyeg3r
Copy link

voyeg3r commented Jun 23, 2022

How do we add a space before "modified"?

Just adding ' ' ..?

@s1n7ax
Copy link
Author

s1n7ax commented Jun 24, 2022

.. ' ' .. modified

@voyeg3r
Copy link

voyeg3r commented Jun 29, 2022

I have changed some stuff, added "WinbarFiletypeExclude" and Last modification date, etc. Thanks for sharing your knowledge!

-- Filename: winbar.lua
-- Last Change: Thu, 23 Jun 2022 - 09:24

local M = {}

local winbar_filetype_exclude = {
    "help",
    "startify",
    "dashboard",
    "packer",
    "neogitstatus",
    "NvimTree",
    "Trouble",
    "alpha",
    "lir",
    "Outline",
    "spectre_panel",
    "toggleterm",
}

-- #4A3E4B
vim.api.nvim_set_hl(0, 'WinBarPath', { bg = '#4A3E4B', fg = '#C1ADC4' })
vim.api.nvim_set_hl(0, 'WinBarModified', { bg = '#dedede', fg = '#ff3838' })

function M.statusline()

    if vim.tbl_contains(winbar_filetype_exclude, vim.bo.filetype) then
        return ""
    end

    local file_path = vim.api.nvim_eval_statusline('%F', {}).str
    -- local modified = vim.api.nvim_eval_statusline('%M', {}).str == '+' and '⊚' or ''
    local modified = vim.api.nvim_eval_statusline('%m', {}).str
    local buffer_number = vim.api.nvim_eval_statusline('%n', {}).str
    local last_change = vim.fn.strftime('%a, %b %d %Y - %H:%M', vim.fn.getftime(vim.fn.expand('%')))

    file_path = file_path:gsub('/', '')
    file_path = file_path:gsub('~', ' $HOME')

    return '%#WinBarPath#'
     .. ' [' .. buffer_number .. '] '
     .. file_path .. ' '
     .. '%*'
     -- .. '%#WinBarModified#'
     .. ' ' .. modified
     .. '%*'
     .. 'Modified: ' .. last_change
end

return M

@s1n7ax
Copy link
Author

s1n7ax commented Jun 29, 2022

Nice

@derekthecool
Copy link

Thanks for this and your videos!
I've found the following function helpful

local function join_status_line(input, highlight, item)
  return input .. '%#' .. highlight .. '#' .. item .. '%*'
end

function M.eval()
    local winbar = ''
    -- do stuff.........
      winbar = join_status_line(winbar, 'WinBarPath', file_path)
      winbar = join_status_line(winbar, 'WinBarModified', modified)

     return winbar
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment