1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 17:30:04 +08:00

Add support multiple notification (#3624)

This commit is contained in:
Wang Shidong 2020-07-12 23:35:26 +08:00 committed by GitHub
parent bf9f97ced5
commit c114050114
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,10 +9,12 @@
" Global values, this can be used between different notification " Global values, this can be used between different notification
let s:shown = [] let s:notifications = {}
" dictionary values and functions
let s:self = {} let s:self = {}
let s:self.message = []
let s:self.winid = -1 let s:self.winid = -1
let s:self.bufnr = -1 let s:self.bufnr = -1
let s:self.border = {} let s:self.border = {}
@ -22,6 +24,7 @@ let s:self.borderchars = ['─', '│', '─', '│', '┌', '┐', '┘', '└'
let s:self.title = '' let s:self.title = ''
let s:self.win_is_open = 0 let s:self.win_is_open = 0
let s:self.timeout = 3000 let s:self.timeout = 3000
let s:self.hashkey = ''
if has('nvim') if has('nvim')
let s:self.__floating = SpaceVim#api#import('neovim#floating') let s:self.__floating = SpaceVim#api#import('neovim#floating')
@ -29,6 +32,7 @@ else
let s:self.__floating = SpaceVim#api#import('vim#floating') let s:self.__floating = SpaceVim#api#import('vim#floating')
endif endif
let s:self.__buffer = SpaceVim#api#import('vim#buffer') let s:self.__buffer = SpaceVim#api#import('vim#buffer')
let s:self.__password = SpaceVim#api#import('password')
function! s:self.draw_border(title, width, height) abort function! s:self.draw_border(title, width, height) abort
let top = self.borderchars[4] . let top = self.borderchars[4] .
@ -69,44 +73,77 @@ endfunction
function! s:self.close(...) dict function! s:self.close(...) dict
if len(s:shown) == 1 if !empty(self.message)
call remove(self.message, 0)
let self.notification_width = max(map(deepcopy(self.message), 'strwidth(v:val)'))
endif
if len(self.message) == 0
noautocmd call self.__floating.win_close(self.border.winid, v:true) noautocmd call self.__floating.win_close(self.border.winid, v:true)
noautocmd call self.__floating.win_close(self.winid, v:true) noautocmd call self.__floating.win_close(self.winid, v:true)
call remove(s:notifications, self.hashkey)
let self.win_is_open = v:false let self.win_is_open = v:false
endif endif
if !empty(s:shown) for hashkey in keys(s:notifications)
call remove(s:shown, 0) call s:notifications[hashkey].redraw_windows()
endif endfor
endfunction endfunction
function! s:self.notification(msg, color) abort function! s:self.notification(msg, color) abort
call add(s:shown, a:msg) call add(self.message, a:msg)
let self.notification_color = a:color
if !bufexists(self.border.bufnr) if !bufexists(self.border.bufnr)
let self.border.bufnr = self.__buffer.create_buf(0, 0) let self.border.bufnr = self.__buffer.create_buf(0, 0)
endif endif
if !bufexists(self.bufnr) if !bufexists(self.bufnr)
let self.bufnr = self.__buffer.create_buf(0, 0) let self.bufnr = self.__buffer.create_buf(0, 0)
endif endif
call self.__buffer.buf_set_lines(self.border.bufnr, 0 , -1, 0, self.draw_border(self.title, strwidth(a:msg), len(s:shown))) if empty(self.hashkey)
call self.__buffer.buf_set_lines(self.bufnr, 0 , -1, 0, s:shown) let self.hashkey = self.__password.generate_simple(10)
endif
call self.redraw_windows()
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 extend(s:notifications, {self.hashkey : self})
call timer_start(self.timeout, self.close, {'repeat' : 1})
endfunction
function! s:self.redraw_windows() abort
if empty(self.message)
return
endif
let self.notification_width = max(map(deepcopy(self.message), 'strwidth(v:val)'))
call self.__buffer.buf_set_lines(self.border.bufnr, 0 , -1, 0, self.draw_border(self.title, self.notification_width, len(self.message)))
call self.__buffer.buf_set_lines(self.bufnr, 0 , -1, 0, self.message)
let self.begin_row = 2
for hashkey in keys(s:notifications)
if hashkey !=# self.hashkey
let self.begin_row += len(s:notifications[hashkey].message) + 2
else
break
endif
endfor
if self.win_is_open if self.win_is_open
call self.__floating.win_config(self.winid, call self.__floating.win_config(self.winid,
\ { \ {
\ 'relative': 'editor', \ 'relative': 'editor',
\ 'width' : strwidth(a:msg), \ 'width' : self.notification_width,
\ 'height' : len(s:shown), \ 'height' : len(self.message),
\ 'row': 3, \ 'row': self.begin_row + 1,
\ 'highlight' : a:color, \ 'highlight' : self.notification_color,
\ 'focusable' : v:false, \ 'focusable' : v:false,
\ 'col': &columns - strwidth(a:msg) - 1, \ 'col': &columns - self.notification_width - 1,
\ }) \ })
call self.__floating.win_config(self.border.winid, call self.__floating.win_config(self.border.winid,
\ { \ {
\ 'relative': 'editor', \ 'relative': 'editor',
\ 'width' : strwidth(a:msg) + 2, \ 'width' : self.notification_width + 2,
\ 'height' : len(s:shown) + 2, \ 'height' : len(self.message) + 2,
\ 'row': 2, \ 'row': self.begin_row,
\ 'col': &columns - strwidth(a:msg) - 2, \ 'col': &columns - self.notification_width - 2,
\ 'highlight' : 'VertSplit', \ 'highlight' : 'VertSplit',
\ 'focusable' : v:false, \ 'focusable' : v:false,
\ }) \ })
@ -114,32 +151,25 @@ function! s:self.notification(msg, color) abort
let self.winid = self.__floating.open_win(self.bufnr, v:false, let self.winid = self.__floating.open_win(self.bufnr, v:false,
\ { \ {
\ 'relative': 'editor', \ 'relative': 'editor',
\ 'width' : strwidth(a:msg), \ 'width' : self.notification_width,
\ 'height' : len(s:shown), \ 'height' : len(self.message),
\ 'row': 3, \ 'row': self.begin_row + 1,
\ 'highlight' : a:color, \ 'highlight' : self.notification_color,
\ 'col': &columns - strwidth(a:msg) - 1, \ 'col': &columns - self.notification_width - 1,
\ 'focusable' : v:false, \ 'focusable' : v:false,
\ }) \ })
let self.border.winid = self.__floating.open_win(self.border.bufnr, v:false, let self.border.winid = self.__floating.open_win(self.border.bufnr, v:false,
\ { \ {
\ 'relative': 'editor', \ 'relative': 'editor',
\ 'width' : strwidth(a:msg) + 2, \ 'width' : self.notification_width + 2,
\ 'height' : len(s:shown) + 2, \ 'height' : len(self.message) + 2,
\ 'row': 2, \ 'row': self.begin_row,
\ 'col': &columns - strwidth(a:msg) - 2, \ 'col': &columns - self.notification_width - 2,
\ 'highlight' : 'VertSplit', \ 'highlight' : 'VertSplit',
\ 'focusable' : v:false, \ 'focusable' : v:false,
\ }) \ })
let self.win_is_open = v:true let self.win_is_open = v:true
endif 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 endfunction