aboutsummaryrefslogtreecommitdiff
path: root/nvim/.config/nvim/plugin/statusline.vim
blob: 3241fc2dbb7c47f7bdab5dce8a3ed9eb84f473c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
scriptencoding utf-8

" statusline

let g:currentmode={
            \ 'n'  : 'NORMAL ',
            \ 'no' : 'N·OPERATOR PENDING ',
            \ 'v'  : 'VISUAL ',
            \ 'V'  : 'V·LINE ',
            \ '' : 'V·BLOCK ',
            \ 's'  : 'SELECT ',
            \ 'S'  : 'S·LINE ',
            \ '' : 'S·BLOCK ',
            \ 'i'  : 'INSERT ',
            \ 'R'  : 'REPLACE ',
            \ 'Rv' : 'V·REPLACE ',
            \ 'c'  : 'COMMAND ',
            \ 'cv' : 'VIM EX ',
            \ 'ce' : 'EX ',
            \ 'r'  : 'PROMPT ',
            \ 'rm' : 'MORE ',
            \ 'r?' : 'CONFIRM ',
            \ '!'  : 'SHELL ',
            \ 't'  : 'TERMINAL '}

hi PrimaryBlock       ctermfg=00    ctermbg=6
hi SecondaryBlock     ctermfg=07    ctermbg=10
hi Blanks             ctermfg=08    ctermbg=0

hi User1             ctermfg=01    ctermbg=0
hi User2             ctermfg=02    ctermbg=0
hi User3             ctermfg=03    ctermbg=0
hi User4             ctermfg=12    ctermbg=0
hi User5             ctermfg=05    ctermbg=0
hi User6             ctermfg=06    ctermbg=0
hi User7             ctermfg=07    ctermbg=0
hi User8             ctermfg=08    ctermbg=0
hi User9             ctermfg=00    ctermbg=0

highlight EndOfBuffer ctermfg=black ctermbg=black

function! GitBranch()
    return system("git rev-parse --abbrev-ref HEAD 2>/dev/null | tr -d '\n'")
endfunction

function! StatuslineGit()
    let l:branchname = GitBranch()
    return strlen(l:branchname) > 0?l:branchname.' ':''
endfunction

function! ReadOnly() abort
    if !&modifiable && &readonly
        return ' RO'
    elseif &modifiable && &readonly
        return 'RO'
    elseif !&modifiable && !&readonly
        return ''
    else
        return ''
    endif
endfunction

function! Filepath() abort
    let l:basename = expand('%:h')
    let l:filename = expand('%:t')
    let l:extension = expand('%:e')
    let l:prefix = (l:basename ==# '' || l:basename ==# '.') ?
                \ '' : substitute(l:basename . '/', '\C^' . $HOME, '~', '')

    if empty(l:prefix) && empty(l:filename)
        return printf('%%4*%%f%%*%s %%m%%*', '%4*')
    elseif empty(l:prefix)
        return printf('%%4*%%f%%*%s %%m%%*', '%6*')
    else
        return printf('%%4*%s%%*%s%s%%*', l:prefix, &modified ? '%6*' : '%4*', l:filename)
    endif
endfunction

function! LinterStatus() abort
  let info = get(b:, 'coc_diagnostic_info', {})
  if empty(info) | return '' | endif
  let msgs = []
  if get(info, 'error', 0)
    call add(msgs, printf('%%5*%s×%%* ', info['error']))
  endif
  if get(info, 'warning', 0)
    call add(msgs, printf('%%3*%s!%%* ', info['warning']))
  endif
  return join(msgs, '')
endfunction

function! StatusLine(mode) abort
  let l:line=''

  " help or man pages
  if &filetype ==# 'help' || &filetype ==# 'man'
    let l:line.=' %#StatusLineNC# ['. &filetype .'] %f '
    return l:line
  endif

  " active
  if a:mode ==# 'active'
    let l:line.='%6*·%*' . ' '
    let l:line.='%7*%{StatuslineGit()}'
    let l:line.='%<'
    let l:line.=Filepath()

    let l:line.='%5*'
    let l:line.=' %{ReadOnly()} %w%*'
    let l:line.='%9* %=%*'

    let l:line.=' ' . '%7*%l,%c%*' . ' '
    let l:line.=LinterStatus()
    let l:line.='%4*'. &filetype

  else
    " inactive
    let l:line.='%5*·%*' . ' '
    let l:line.='%#Blanks#'
    let l:line.='%f %5*%m%*'
    let l:line.='%#Blanks# %=%*'
  endif

  let l:line.='%*'

  return l:line
endfunction

set statusline=%!StatusLine('active')
augroup MyStatusLine
  autocmd!
  autocmd WinEnter * setl statusline=%!StatusLine('active')
  autocmd WinLeave * setl statusline=%!StatusLine('inactive')
augroup END