From fa4d25a2eb9e5532633225b1e72c372b3190cbfc Mon Sep 17 00:00:00 2001 From: Wang Shidong Date: Sun, 27 Jan 2019 16:55:50 +0800 Subject: [PATCH] Add gtm support (#2512) --- .gitignore | 1 + autoload/SpaceVim/layers/VersionControl.vim | 68 ++++++++++++++++++--- docs/cn/layers/VersionControl.md | 4 ++ docs/layers/VersionControl.md | 6 ++ 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 513c8e524..e034da575 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ cscope.po.out build/ .coverage.covimerage *.swp +.gtm/ diff --git a/autoload/SpaceVim/layers/VersionControl.vim b/autoload/SpaceVim/layers/VersionControl.vim index aca249f01..dab8425aa 100644 --- a/autoload/SpaceVim/layers/VersionControl.vim +++ b/autoload/SpaceVim/layers/VersionControl.vim @@ -10,6 +10,8 @@ scriptencoding utf-8 let s:CMP = SpaceVim#api#import('vim#compatible') +let s:enable_gtm_status = 0 + function! SpaceVim#layers#VersionControl#plugins() abort let plugins = [] call add(plugins, ['mhinz/vim-signify', {'merged' : 0}]) @@ -31,28 +33,76 @@ function! SpaceVim#layers#VersionControl#config() abort \ 'version control info', 1) call SpaceVim#mapping#space#def('nnoremap', ['t', 'm', 'h'], 'call SpaceVim#layers#core#statusline#toggle_section("hunks")', \ 'toggle the hunks summary', 1) + let g:gtm_plugin_status_enabled = s:enable_gtm_status + if s:enable_gtm_status + augroup gtm_plugin + autocmd! + autocmd BufReadPost,BufWritePost,CursorMoved,CursorMovedI * silent call s:record() + augroup END + endif +endfunction + +function! SpaceVim#layers#VersionControl#set_variable(var) abort + let s:enable_gtm_status = get(a:var, + \ 'enable_gtm_status', + \ s:enable_gtm_status) endfunction "  master function! s:git_branch() abort if exists('g:loaded_fugitive') try - let l:head = fugitive#head() - if empty(l:head) - call fugitive#detect(getcwd()) let l:head = fugitive#head() - endif - if g:spacevim_statusline_unicode_symbols == 1 - return empty(l:head) ? '' : '  '.l:head . ' ' - else - return empty(l:head) ? '' : ' '.l:head . ' ' - endif + if empty(l:head) + call fugitive#detect(getcwd()) + let l:head = fugitive#head() + endif + if g:spacevim_statusline_unicode_symbols == 1 + return empty(l:head) ? '' : '  '.l:head . s:gtm_status() + else + return empty(l:head) ? '' : ' '.l:head . s:gtm_status() + endif catch endtry endif return '' endfunction + +function! s:gtm_status() abort + if s:enable_gtm_status + let status = s:gtm_statusline() + return empty(status) ? '' : ' (' . status . ') ' + else + return '' + endif +endfunction + +let s:last_update = 0 +let s:last_file = '' +let s:update_interval = 30 +let s:gtm_plugin_status = '' + +function! s:record() + let fpath = expand('%:p') + " record if file path has changed or last update is greater than update_interval + if (s:last_file != fpath || localtime() - s:last_update > s:update_interval) && filereadable(fpath) + let s:cmd = (s:enable_gtm_status == 1 ? 'gtm record --status' : 'gtm record') + let output=system(s:cmd . ' ' . shellescape(fpath)) + if v:shell_error + echoerr s:no_gtm_err + else + let s:gtm_plugin_status = (s:enable_gtm_status ? substitute(output, '\n\+$', '', '') : '') + endif + let s:last_update = localtime() + let s:last_file = fpath + endif +endfunction + +function! s:gtm_statusline() abort + return s:gtm_plugin_status +endfunction + " +0 ~0 -0 function! s:hunks() abort let hunks = [0,0,0] diff --git a/docs/cn/layers/VersionControl.md b/docs/cn/layers/VersionControl.md index fc3da7f90..d54a94ba1 100644 --- a/docs/cn/layers/VersionControl.md +++ b/docs/cn/layers/VersionControl.md @@ -10,6 +10,7 @@ lang: cn - [模块介绍](#模块介绍) - [启用模块](#启用模块) +- [模块选项](#模块选项) - [快捷键](#快捷键) @@ -30,6 +31,9 @@ lang: cn [[layers]] name = "VersionControl" ``` +## 模块选项 + +`enable-gtm-status`: 在状态栏展示当前分支工作的时间,这一特性需要安装 [gtm](https://github.com/git-time-metric/gtm) 命令。 ## 快捷键 diff --git a/docs/layers/VersionControl.md b/docs/layers/VersionControl.md index 135af92e8..73dcacb5a 100644 --- a/docs/layers/VersionControl.md +++ b/docs/layers/VersionControl.md @@ -10,6 +10,7 @@ description: "This layers provides general version control feature for vim. It s - [Description](#description) - [Features](#features) - [Install](#install) +- [Layer options](#layer-options) - [Key bindings](#key-bindings) @@ -33,6 +34,11 @@ To use this configuration layer, update custom configuration file with: name = "VersionControl" ``` +## Layer options + +`enable-gtm-status`: Enable diplaying time spent within SpaceVim's statusline. This feature need [gtm](https://github.com/git-time-metric/gtm) command to be installed. + + ## Key bindings | Key Binding | Description |