mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-01-23 13:00:04 +08:00
Add notification API (#3621)
This commit is contained in:
parent
f263660f8b
commit
fe5230a9e2
147
autoload/SpaceVim/api/notification.vim
Normal file
147
autoload/SpaceVim/api/notification.vim
Normal file
@ -0,0 +1,147 @@
|
||||
"=============================================================================
|
||||
" notification.vim --- notification api
|
||||
" Copyright (c) 2016-2019 Wang Shidong & Contributors
|
||||
" Author: Wang Shidong < wsdjeg@outlook.com >
|
||||
" URL: https://spacevim.org
|
||||
" License: GPLv3
|
||||
"=============================================================================
|
||||
|
||||
|
||||
" Global values, this can be used between different notification
|
||||
|
||||
let s:shown = []
|
||||
|
||||
let s:self = {}
|
||||
|
||||
let s:self.winid = -1
|
||||
let s:self.bufnr = -1
|
||||
let s:self.border = {}
|
||||
let s:self.border.winid = -1
|
||||
let s:self.border.bufnr = -1
|
||||
let s:self.borderchars = ['─', '│', '─', '│', '┌', '┐', '┘', '└']
|
||||
let s:self.title = ''
|
||||
let s:self.win_is_open = 0
|
||||
let s:self.timeout = 3000
|
||||
|
||||
if has('nvim')
|
||||
let s:self.__floating = SpaceVim#api#import('neovim#floating')
|
||||
else
|
||||
let s:self.__floating = SpaceVim#api#import('vim#floating')
|
||||
endif
|
||||
let s:self.__buffer = SpaceVim#api#import('vim#buffer')
|
||||
|
||||
function! s:self.draw_border(title, width, height) abort
|
||||
let top = self.borderchars[4] .
|
||||
\ repeat(self.borderchars[0], a:width) .
|
||||
\ self.borderchars[5]
|
||||
let mid = self.borderchars[3] .
|
||||
\ repeat(' ', a:width) .
|
||||
\ self.borderchars[1]
|
||||
let bot = self.borderchars[7] .
|
||||
\ repeat(self.borderchars[2], a:width) .
|
||||
\ self.borderchars[6]
|
||||
let top = self.string_compose(top, 1, a:title)
|
||||
let lines = [top] + repeat([mid], a:height) + [bot]
|
||||
return lines
|
||||
endfunction
|
||||
|
||||
function! s:self.string_compose(target, pos, source)
|
||||
if a:source == ''
|
||||
return a:target
|
||||
endif
|
||||
let pos = a:pos
|
||||
let source = a:source
|
||||
if pos < 0
|
||||
let source = strcharpart(a:source, -pos)
|
||||
let pos = 0
|
||||
endif
|
||||
let target = strcharpart(a:target, 0, pos)
|
||||
if strchars(target) < pos
|
||||
let target .= repeat(' ', pos - strchars(target))
|
||||
endif
|
||||
let target .= source
|
||||
" vim popup will pad the end of title but not begin part
|
||||
" so we build the title as ' floaterm idx/cnt'
|
||||
" therefore, we need to add a space here
|
||||
let target .= ' ' . strcharpart(a:target, pos + strchars(source) + 1)
|
||||
return target
|
||||
endfunction
|
||||
|
||||
|
||||
function! s:self.close(...) dict
|
||||
if len(s:shown) == 1
|
||||
noautocmd call self.__floating.win_close(self.border.winid, v:true)
|
||||
noautocmd call self.__floating.win_close(self.winid, v:true)
|
||||
let self.win_is_open = v:false
|
||||
endif
|
||||
if !empty(s:shown)
|
||||
call remove(s:shown, 0)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:self.notification(msg, color) abort
|
||||
call add(s:shown, a:msg)
|
||||
if !bufexists(self.border.bufnr)
|
||||
let self.border.bufnr = self.__buffer.create_buf(0, 0)
|
||||
endif
|
||||
if !bufexists(self.bufnr)
|
||||
let self.bufnr = self.__buffer.create_buf(0, 0)
|
||||
endif
|
||||
call self.__buffer.buf_set_lines(self.border.bufnr, 0 , -1, 0, self.draw_border(self.title, strwidth(a:msg), len(s:shown)))
|
||||
call self.__buffer.buf_set_lines(self.bufnr, 0 , -1, 0, s:shown)
|
||||
if self.win_is_open
|
||||
call self.__floating.win_config(self.winid,
|
||||
\ {
|
||||
\ 'relative': 'editor',
|
||||
\ 'width' : strwidth(a:msg),
|
||||
\ 'height' : len(s:shown),
|
||||
\ 'row': 3,
|
||||
\ 'highlight' : a:color,
|
||||
\ 'col': &columns - strwidth(a:msg) - 1,
|
||||
\ })
|
||||
call self.__floating.win_config(self.border.winid,
|
||||
\ {
|
||||
\ 'relative': 'editor',
|
||||
\ 'width' : strwidth(a:msg) + 2,
|
||||
\ 'height' : len(s:shown) + 2,
|
||||
\ 'row': 2,
|
||||
\ 'col': &columns - strwidth(a:msg) - 2,
|
||||
\ 'highlight' : 'VertSplit',
|
||||
\ })
|
||||
else
|
||||
let self.winid = self.__floating.open_win(self.bufnr, v:false,
|
||||
\ {
|
||||
\ 'relative': 'editor',
|
||||
\ 'width' : strwidth(a:msg),
|
||||
\ 'height' : len(s:shown),
|
||||
\ 'row': 3,
|
||||
\ 'highlight' : a:color,
|
||||
\ 'col': &columns - strwidth(a:msg) - 1
|
||||
\ })
|
||||
let self.border.winid = self.__floating.open_win(self.border.bufnr, v:false,
|
||||
\ {
|
||||
\ 'relative': 'editor',
|
||||
\ 'width' : strwidth(a:msg) + 2,
|
||||
\ 'height' : len(s:shown) + 2,
|
||||
\ 'row': 2,
|
||||
\ 'col': &columns - strwidth(a:msg) - 2,
|
||||
\ 'highlight' : 'VertSplit',
|
||||
\ })
|
||||
let self.win_is_open = v:true
|
||||
endif
|
||||
call setbufvar(self.bufnr, '&number', 0)
|
||||
call setbufvar(self.bufnr, '&relativenumber', 0)
|
||||
call setbufvar(self.bufnr, '&buftype', 'nofile')
|
||||
call setbufvar(self.border.bufnr, '&number', 0)
|
||||
call setbufvar(self.border.bufnr, '&relativenumber', 0)
|
||||
call setbufvar(self.border.bufnr, '&buftype', 'nofile')
|
||||
call timer_start(self.timeout, self.close, {'repeat' : 1})
|
||||
endfunction
|
||||
|
||||
|
||||
function! SpaceVim#api#notification#get()
|
||||
|
||||
return deepcopy(s:self)
|
||||
|
||||
endfunction
|
||||
|
@ -81,6 +81,24 @@ function! s:self.bufadd(name) abort
|
||||
return nr
|
||||
endif
|
||||
endfunction
|
||||
if exists('*nvim_create_buf')
|
||||
function! s:self.create_buf(listed, scratch) abort
|
||||
return nvim_create_buf(a:listed, a:scratch)
|
||||
endfunction
|
||||
else
|
||||
function! s:self.create_buf(listed, scratch) abort
|
||||
let bufnr = self.bufadd('')
|
||||
" in vim, a:listed must be number, what the fuck!
|
||||
" why can not use v:true and v:false
|
||||
call setbufvar(bufnr, '&buflisted', a:listed ? 1 : 0)
|
||||
if a:scratch
|
||||
call setbufvar(bufnr, '&swapfile', 0)
|
||||
call setbufvar(bufnr, '&bufhidden', 'hide')
|
||||
call setbufvar(bufnr, '&buftype', 'nofile')
|
||||
endif
|
||||
return bufnr
|
||||
endfunction
|
||||
endif
|
||||
|
||||
function! s:self.open(opts) abort
|
||||
let buf = get(a:opts, 'bufname', '')
|
||||
|
@ -529,7 +529,7 @@ function! s:winopen() abort " {{{
|
||||
let pos = g:leaderGuide_position ==? 'topleft' ? 'topleft' : 'botright'
|
||||
if s:FLOATING.exists()
|
||||
if !bufexists(s:bufnr)
|
||||
let s:bufnr = s:BUFFER.bufadd('')
|
||||
let s:bufnr = s:BUFFER.create_buf(v:false, v:true)
|
||||
endif
|
||||
let s:winid = s:FLOATING.open_win(s:bufnr, v:true,
|
||||
\ {
|
||||
|
@ -730,7 +730,7 @@ function! SpaceVim#plugins#flygrep#open(argv) abort
|
||||
" set default handle func: s:flygrep
|
||||
let s:MPT._handle_fly = function('s:flygrep')
|
||||
if exists('*nvim_open_win')
|
||||
let s:buffer_id = s:BUFFER.bufadd('')
|
||||
let s:buffer_id = s:BUFFER.create_buf(v:false, v:true)
|
||||
let flygrep_win_height = 16
|
||||
let s:flygrep_win_id = s:FLOATING.open_win(s:buffer_id, v:true,
|
||||
\ {
|
||||
|
Loading…
Reference in New Issue
Block a user