all repos — dotfiles @ 958baf2410065c42870624c33823c1628fb49ec0

my *nix dotfiles

nvim: Rework statusline
Anirudh Oppiliappan x@icyphox.sh
Thu, 08 Jul 2021 10:19:42 +0530
commit

958baf2410065c42870624c33823c1628fb49ec0

parent

037cb472cc122197838a1c583d6202e556c23dff

4 files changed, 113 insertions(+), 71 deletions(-)

jump to
M config/nvim/init.luaconfig/nvim/init.lua

@@ -14,7 +14,7 @@ }

require('settings') require('maps') -require('statusline') +require('statusline.line') -- lsp setup -- require('lsp.yaml') shits too noisy
D config/nvim/lua/statusline.lua

@@ -1,70 +0,0 @@

--- set highlights for statusline sections -vim.api.nvim_exec( -[[ - hi PrimaryBlock ctermfg=06 ctermbg=00 - hi SecondaryBlock ctermfg=07 ctermbg=00 - hi Blanks ctermfg=08 ctermbg=00 - hi GitClean ctermfg=02 ctermbg=00 - hi GitDirty ctermfg=01 ctermbg=00 -]], false) - -local clean = '•' -local dirty = '×' - -local function are_we_in_git() - local handle = io.popen('git rev-parse --is-inside-work-tree 2> /dev/null') - local out = handle:read('*a'):gsub('\n', '') - if out == 'true' then - in_git = true - end -end - -local function is_clean() - if in_git then - local handle = io.popen('git status --porcelain 2> /dev/null') - local status = handle:read('*a') - - if status ~= '' then return false end - return true - end -end - -local function git_sym() - if in_git then - if is_clean() then - return ' %#GitClean#' .. clean - else - return ' %#GitDirty#' .. dirty - end - end -end - -local function git_branch() - if in_git then - local handle = io.popen('git branch --show-current 2> /dev/null') - local branch = handle:read('*a'):gsub('\n', '') - local rc = { handle:close() } - if rc[1] then - local out = ' ' .. branch .. git_sym() - return out - end - end - return '' -end - -are_we_in_git() -local stl = { - '%#PrimaryBlock#', - '%f', - '%#Blanks#', - '%m', - '%#SecondaryBlock#', - git_branch(), - '%=', - '%#SecondaryBlock#', - '%l,%c ', - '%#PrimaryBlock#', - '%{&filetype}', -} - -vim.opt_global.statusline = table.concat(stl)
A config/nvim/lua/statusline/git.lua

@@ -0,0 +1,77 @@

+local M = {} + +local git_branch +local clean = '•' +local dirty = '×' + +local sep = package.config:sub(1,1) + +local function find_git_dir() + local file_dir = vim.fn.expand('%:p:h') .. ';' + local git_dir = vim.fn.finddir('.git', file_dir) + if #git_dir > 0 then + M.in_git = true + end + return git_dir +end + +local function get_git_head(head_file) + local f_head = io.open(head_file) + if f_head then + local HEAD = f_head:read() + f_head:close() + local branch = HEAD:match('ref: refs/heads/(.+)$') + if branch then git_branch = branch + else git_branch = HEAD:sub(1,6) end + end + return nil +end + +-- event watcher to watch head file +local file_changed = vim.loop.new_fs_event() +local function watch_head() + file_changed:stop() + local git_dir = find_git_dir() + if #git_dir > 0 then + local head_file = git_dir..sep..'HEAD' + get_git_head(head_file) + file_changed:start(head_file, {}, vim.schedule_wrap(function() + -- reset file-watch + watch_head() + end)) + else + -- set to nil when git dir was not found + git_branch = nil + end +end + +function M.branch() + if not git_branch or #git_branch == 0 then return '' end + return git_branch +end + +local function is_clean() + if M.in_git then + local handle = io.popen('git status --porcelain 2> /dev/null') + local status = handle:read('*a') + + if status ~= '' then return false end + return true + end + return nil +end + +-- clean or dirty +function M.status() + if M.in_git then + if is_clean() then + return '%#GitClean#' .. clean + else + return '%#GitDirty#' .. dirty + end + end +end + +watch_head() + +return M
A config/nvim/lua/statusline/line.lua

@@ -0,0 +1,35 @@

+local git = require('statusline.git') + +-- set highlights for statusline sections +vim.api.nvim_exec( +[[ + hi PrimaryBlock ctermfg=06 ctermbg=00 + hi SecondaryBlock ctermfg=07 ctermbg=00 + hi Blanks ctermfg=08 ctermbg=00 + hi GitClean ctermfg=02 ctermbg=00 + hi GitDirty ctermfg=01 ctermbg=00 +]], false) + +local git_info +if git.in_git then + git_info = string.format(' %s %s', git.branch(), git.status()) +else + git_info = '' +end + + +local stl = { + '%#PrimaryBlock#', + '%f', + '%#Blanks#', + '%m', + '%#SecondaryBlock#', + git_info, + '%=', + '%#SecondaryBlock#', + '%l,%c ', + '%#PrimaryBlock#', + '%{&filetype}', +} + +vim.opt_global.statusline = table.concat(stl)