dotfiles

personal configuration files and scripts
git clone https://tongong.net/git/dotfiles.git
Log | Files | Refs | README

statusline.lua (3266B)


      1 -- inspired by: https://nihilistkitten.me/nvim-lua-statusline/
      2 --              https://elianiva.my.id/post/neovim-lua-statusline
      3 --              lightline
      4 
      5 -- colorscheme
      6 local c_highlight  = 3 -- yellow
      7 local c_grey_light = 8
      8 local c_grey_dark  = 237
      9 local c_white      = 15
     10 -- for git diff counts
     11 local c_green      = 2
     12 local c_yellow     = 3
     13 local c_red        = 1
     14 
     15 local function highlight(group, fg, bg)
     16     vim.cmd("highlight " .. group .. " ctermfg=" .. fg .. " ctermbg=" .. bg
     17         .. " cterm=bold")
     18 end
     19 function status_line_highlights()
     20     vim.cmd("highlight StatusLine cterm=bold ctermfg=15 ctermbg=None")
     21     highlight("StatusHighlight", c_grey_dark, c_highlight)
     22     highlight("StatusLight",     c_white,     c_grey_light)
     23     highlight("StatusDark",      c_highlight, c_grey_dark)
     24     highlight("StatusNone",      "None",      "None")
     25     highlight("StatusGitAdd",    c_green,     c_grey_light)
     26     highlight("StatusGitChange", c_yellow,    c_grey_light)
     27     highlight("StatusGitRemove", c_red,       c_grey_light)
     28 end
     29 
     30 local function get_mode()
     31     local modes = {
     32         [ 'n']  = 'NORMAL',  [ 'no'] = 'N-PENDING', [ 'v']  = 'VISUAL',
     33         [ 'V']  = 'V-LINE',  [ ''] = 'V-BLOCK',   [ 's']  = 'SELECT',
     34         [ 'S']  = 'S-LINE',  [ ''] = 'S-BLOCK',   [ 'i']  = 'INSERT',
     35         [ 'ic'] = 'INSERT',  [ 'R']  = 'REPLACE',   [ 'Rv'] = 'V-REPLACE',
     36         [ 'c']  = 'COMMAND', [ 'cv'] = 'VIM-EX ',   [ 'ce'] = 'EX',
     37         [ 'r']  = 'PROMT',   [ 'rm'] = 'MORE',      [ 'r?'] = 'CONFIRM',
     38         [ '!']  = 'SHELL',   [ 't']  = 'TERMINAL',
     39     }
     40     local mode = vim.api.nvim_get_mode()
     41     if mode == nil then
     42         return "?"
     43     else
     44         return modes[mode.mode]
     45     end
     46 end
     47 
     48 local function git_counts()
     49     local summary = vim.api.nvim_call_function("GitGutterGetHunkSummary", {})
     50     if summary[1] + summary[2] + summary[3] == 0 then
     51         return ""
     52     else
     53         return "%#StatusGitAdd#"
     54             .. " +" .. summary[1]
     55             .. "%#StatusGitChange#"
     56             .. " ~" .. summary[2]
     57             .. "%#StatusGitRemove#"
     58             .. " -" .. summary[3] .. " "
     59     end
     60 end
     61 
     62 -- warn me about evil non-unix files
     63 local function fileformat()
     64     local ff = vim.bo.fileformat
     65     if ff ~= "unix" then
     66         return "%#StatusHighlight# " .. string.upper(ff) .. " "
     67             .. "%#StatusNone# "
     68     else
     69         return ""
     70     end
     71 end
     72 
     73 function status_line(active)
     74     local active_highlight = "%#StatusHighlight#"
     75     if active == 0 then
     76         active_highlight = "%#StatusDark#"
     77     end
     78     return active_highlight
     79         .. " " .. get_mode() .. " "
     80         .. "%#StatusLight#"
     81         .. " %f "
     82         .. "%#StatusNone#"
     83         .. "%="
     84         .. fileformat()
     85         .. git_counts()
     86         .. active_highlight
     87         .. " %l:%v "
     88 end
     89 
     90 status_line_highlights()
     91 vim.o.statusline = "%!v:lua.status_line(0)"
     92 
     93 vim.api.nvim_exec([[
     94     augroup Statusline
     95     au!
     96     au WinEnter,BufEnter * setlocal statusline=%!v:lua.status_line(1)
     97     au WinLeave,BufLeave * setlocal statusline=%!v:lua.status_line(0)
     98 
     99     au FocusGained * setlocal statusline=%!v:lua.status_line(1)
    100     au FocusLost   * setlocal statusline=%!v:lua.status_line(0)
    101     au User GoyoLeave nested lua status_line_highlights()
    102     augroup END
    103 ]], false)