summaryrefslogtreecommitdiffstats
path: root/vimrcs/statusline.vim
blob: 8ba5f122c8422a7470e10feaef64d0b224963a57 (plain) (blame)
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
" 标签样式
"  TabLineFill  tab pages line, where there are no labels
hi TabLineFill term=none
hi TabLineFill ctermfg=DarkGrey
hi TabLineFill guifg=#777777
"  TabLineSel   tab pages line, active tab page label
hi TabLineSel term=inverse
hi TabLineSel cterm=none ctermfg=yellow ctermbg=Black
hi TabLineSel gui=none guifg=yellow guibg=Black

set laststatus=2   " 显示状态栏(默认值为 1,无法显示状态栏)
set statusline=%f%m%r%h%w\ >\ %{ShowGit()}%=\ <\ %{&ff},\ %{&fenc}\ <\ %y\ <\ %l,%v\ <\ %p%%\ <\ lines=%L

set guitablabel=%{ShortTabLabel()}
set tabline=%!MyTabLine()
function ShortTabLabel ()
    let bufnrlist = tabpagebuflist (v:lnum)
    let label = bufname (bufnrlist[tabpagewinnr (v:lnum) -1])
    let filename = fnamemodify (label, ':t')
    return filename
endfunction

function MyTabLine()
    let s = ''
    for i in range(tabpagenr('$'))
        " 选择高亮
        if i + 1 == tabpagenr()
            let s .= '%#TabLineSel#'
        else
            let s .= '%#TabLine#'
        endif
        " 设置标签页号 (用于鼠标点击)
        let s .= '%' . (i + 1) . 'T'
        " MyTabLabel() 提供完整路径标签 MyShortTabLabel 提供文件名标签
        let s .= ' %{MyShortTabLabel(' . (i + 1) . ')} '
    endfor
    " 最后一个标签页之后用 TabLineFill 填充并复位标签页号
    let s .= '%#TabLineFill#%T'
    " 右对齐用于关闭当前标签页的标签
    if tabpagenr('$') > 1
        let s .= '%=%#TabLine#%999Xclose'
    endif
    return s
endfunction

" 文件名标签
function MyShortTabLabel(n)
    let buflist = tabpagebuflist(a:n)
    let label = bufname (buflist[tabpagewinnr (a:n) -1])
    let filename = fnamemodify (label, ':t')
    return filename
endfunction
" 完整路径标签
function MyTabLabel(n)
    let buflist = tabpagebuflist(a:n)
    let winnr = tabpagewinnr(a:n)
    return bufname(buflist[winnr - 1])
endfunction

" 状态栏显示git分支
function! ShowGit()
    let l:path = expand('%:p:h')
    let l:git = "git -C " . l:path
    let l:is_git_repo = system(l:git . " rev-parse --is-inside-work-tree 2>/dev/null")
    if match(l:is_git_repo, "true") == -1
        return ''
    endif
    " 查看当前所在分支
    let s:branch = system(l:git . " branch --no-color 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \\(.*\\)/\\1/' | tr -d '\n' | tr -d ' '")
    " 字符串匹配,匹配不到"HEAD detached at"或者”头指针在xxx分离“则返回branch
    if match(s:branch, 'HEADdetachedat') == -1 && match(s:branch, '分离') == -1
        return strlen(s:branch) ? 'b: '.s:branch.'> ' : ''
    endif
    " 查看当前是否在tag上,如果是则返回tag名
    let s:tag = system(l:git . " describe --tags --exact-match 2>/dev/null | tr -d '\n'")
    if strlen(s:tag)
        return 't: '.s:tag.'> '
    endif
    " 既不是分支也不是tag,返回当前的commit id
    let s:commit = system(l:git . " rev-parse --short HEAD 2>/dev/null | tr -d '\n'")
    return strlen(s:commit) ? 'c: '.s:commit.'> ' : ''
endfunction

function! CurDir()
    let curdir = substitute(getcwd(), '/Users/amir/', "~/", "g")
    return curdir
endfunction