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

Improve notify api (#4355)

This commit is contained in:
Wang Shidong 2021-08-06 19:19:00 +08:00 committed by GitHub
parent 7a6b6ff659
commit 2d6bfd39a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 120 additions and 62 deletions

View File

@ -5,7 +5,7 @@
" URL: https://spacevim.org
" License: GPLv3
"=============================================================================
scriptencoding utf-8
let s:self = {}
let s:self.__dict = SpaceVim#api#import('data#dict')
@ -88,6 +88,10 @@ function! s:self.win_config(winid, opt) abort
return id
endfunction
function! s:self.get_width(winid) abort
return nvim_win_get_width(a:winid)
endfunction
function! s:self.win_close(id, focuce) abort
return nvim_win_close(a:id, a:focuce)

View File

@ -1,13 +1,13 @@
"=============================================================================
" notification.vim --- notification api
" notify.vim --- notify api
" Copyright (c) 2016-2020 Wang Shidong & Contributors
" Author: Wang Shidong < wsdjeg@outlook.com >
" URL: https://spacevim.org
" License: GPLv3
"=============================================================================
scriptencoding utf-8
" Global values, this can be used between different notification
" Global values, this can be used between different notify
let s:notifications = {}
@ -15,6 +15,7 @@ let s:notifications = {}
let s:self = {}
let s:self.message = []
let s:self.notification_width = 1
let s:self.winid = -1
let s:self.bufnr = -1
let s:self.border = {}
@ -25,6 +26,15 @@ let s:self.title = ''
let s:self.win_is_open = 0
let s:self.timeout = 3000
let s:self.hashkey = ''
let s:self.config = {}
let s:self.config.icons = {
\ 'ERROR' : '',
\ 'WARN' : '',
\ 'INFO' : '',
\ 'DEBUG' : '',
\ 'TRACE' : '✎',
\ }
let s:self.config.title = 'SpaceVim'
if has('nvim')
let s:self.__floating = SpaceVim#api#import('neovim#floating')
@ -49,7 +59,18 @@ function! s:self.draw_border(title, width, height) abort
return lines
endfunction
function! s:self.string_compose(target, pos, source)
function! s:self.increase_window(...) abort
" let self.notification_width = self.__floating.get_width(self.winid)
if self.notification_width <= &columns * 0.3
let self.notification_width += min([float2nr((&columns * 0.3 - self.notification_width) * 1 / 10), float2nr(&columns * 0.3)])
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.redraw_windows()
call timer_start(30, self.increase_window, {'repeat' : 1})
endif
endfunction
function! s:self.string_compose(target, pos, source) abort
if a:source == ''
return a:target
endif
@ -72,25 +93,25 @@ function! s:self.string_compose(target, pos, source)
endfunction
function! s:self.close(...) dict
function! s:self.close(...) abort
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.winid, v:true)
call remove(s:notifications, self.hashkey)
let self.win_is_open = v:false
let self.notification_width = 1
endif
for hashkey in keys(s:notifications)
call s:notifications[hashkey].redraw_windows()
call s:notifications[hashkey].redraw_windows()
endfor
endfunction
function! s:self.notification(msg, color) abort
function! s:self.notify(msg, ...) abort
call add(self.message, a:msg)
let self.notification_color = a:color
let self.notification_color = get(a:000, 0, 'Normal')
if !bufexists(self.border.bufnr)
let self.border.bufnr = self.__buffer.create_buf(0, 0)
endif
@ -102,12 +123,15 @@ function! s:self.notification(msg, color) abort
endif
call self.redraw_windows()
call setbufvar(self.bufnr, '&number', 0)
call setbufvar(self.bufnr, '&cursorline', 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 setbufvar(self.border.bufnr, '&cursorline', 0)
call extend(s:notifications, {self.hashkey : self})
call self.increase_window()
call timer_start(self.timeout, self.close, {'repeat' : 1})
endfunction
@ -115,7 +139,6 @@ function! s:self.redraw_windows() abort
if empty(self.message)
return
endif
let self.notification_width = max(map(deepcopy(self.message), 'strwidth(v:val)'))
let self.begin_row = 2
for hashkey in keys(s:notifications)
if hashkey !=# self.hashkey
@ -127,55 +150,57 @@ function! s:self.redraw_windows() abort
if self.win_is_open
call self.__floating.win_config(self.winid,
\ {
\ 'relative': 'editor',
\ 'width' : self.notification_width,
\ 'height' : len(self.message),
\ 'row': self.begin_row + 1,
\ 'highlight' : self.notification_color,
\ 'focusable' : v:false,
\ 'col': &columns - self.notification_width - 1,
\ })
\ 'relative': 'editor',
\ 'width' : self.notification_width,
\ 'height' : len(self.message),
\ 'row': self.begin_row + 1,
\ 'highlight' : self.notification_color,
\ 'focusable' : v:false,
\ 'col': &columns - self.notification_width - 1,
\ })
call self.__floating.win_config(self.border.winid,
\ {
\ 'relative': 'editor',
\ 'width' : self.notification_width + 2,
\ 'height' : len(self.message) + 2,
\ 'row': self.begin_row,
\ 'col': &columns - self.notification_width - 2,
\ 'highlight' : 'VertSplit',
\ 'focusable' : v:false,
\ })
\ 'relative': 'editor',
\ 'width' : self.notification_width + 2,
\ 'height' : len(self.message) + 2,
\ 'row': self.begin_row,
\ 'col': &columns - self.notification_width - 2,
\ 'highlight' : 'VertSplit',
\ 'focusable' : v:false,
\ })
else
let self.winid = self.__floating.open_win(self.bufnr, v:false,
\ {
\ 'relative': 'editor',
\ 'width' : self.notification_width,
\ 'height' : len(self.message),
\ 'row': self.begin_row + 1,
\ 'highlight' : self.notification_color,
\ 'col': &columns - self.notification_width - 1,
\ 'focusable' : v:false,
\ })
\ 'relative': 'editor',
\ 'width' : self.notification_width,
\ 'height' : len(self.message),
\ 'row': self.begin_row + 1,
\ 'highlight' : self.notification_color,
\ 'col': &columns - self.notification_width - 1,
\ 'focusable' : v:false,
\ })
let self.border.winid = self.__floating.open_win(self.border.bufnr, v:false,
\ {
\ 'relative': 'editor',
\ 'width' : self.notification_width + 2,
\ 'height' : len(self.message) + 2,
\ 'row': self.begin_row,
\ 'col': &columns - self.notification_width - 2,
\ 'highlight' : 'VertSplit',
\ 'focusable' : v:false,
\ })
\ 'relative': 'editor',
\ 'width' : self.notification_width + 2,
\ 'height' : len(self.message) + 2,
\ 'row': self.begin_row,
\ 'col': &columns - self.notification_width - 2,
\ 'highlight' : 'VertSplit',
\ 'focusable' : v:false,
\ })
let self.win_is_open = v:true
endif
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.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)
endfunction
function! SpaceVim#api#notification#get()
function! SpaceVim#api#notify#get() abort
return deepcopy(s:self)
endfunction

View File

@ -54,7 +54,7 @@ let s:flutter_job_id = 0
let s:dart_sdk_path = ''
let s:JOB = SpaceVim#api#import('job')
let s:NOTI =SpaceVim#api#import('notification')
let s:NOTI = SpaceVim#api#import('notify')
function! SpaceVim#layers#lang#dart#plugins() abort
let plugins = []
@ -129,7 +129,7 @@ endfunction
function! s:flutter_run() abort
if s:flutter_job_id ==# 0
" call s:NOTI.notification(line, 'Normal')
" call s:NOTI.notify(line, 'Normal')
let s:flutter_job_id = s:JOB.start('flutter run',
\ {
\ 'on_stdout' : function('s:on_stdout'),
@ -142,7 +142,7 @@ endfunction
function! s:flutter_send(msg) abort
if s:flutter_job_id ==# 0
call s:NOTI.notification('Flutter is not running.', 'WarningMsg')
call s:NOTI.notify('Flutter is not running.', 'WarningMsg')
else
call s:JOB.send(s:flutter_job_id, a:msg)
endif
@ -150,13 +150,13 @@ endfunction
function! s:on_stdout(id, data, event) abort
for line in filter(a:data, '!empty(v:val)')
call s:NOTI.notification(line, 'Normal')
call s:NOTI.notify(line, 'Normal')
endfor
endfunction
function! s:on_stderr(id, data, event) abort
for line in filter(a:data, '!empty(v:val)')
call s:NOTI.notification(line, 'WarningMsg')
call s:NOTI.notify(line, 'WarningMsg')
endfor
endfunction

View File

@ -6,9 +6,9 @@
"=============================================================================
let s:JOB = SpaceVim#api#import('job')
let s:NOTI =SpaceVim#api#import('notification')
let s:NOTI = SpaceVim#api#import('notify')
function! git#push#run(...)
function! git#push#run(...) abort
let cmd = ['git', 'push']
if len(a:1) > 0
@ -36,13 +36,13 @@ endfunction
function! s:on_stdout(id, data, event) abort
for line in filter(a:data, '!empty(v:val)')
call s:NOTI.notification(line, 'Normal')
call s:NOTI.notify(line, 'Normal')
endfor
endfunction
function! s:on_stderr(id, data, event) abort
for line in filter(a:data, '!empty(v:val)')
call s:NOTI.notification(line, 'WarningMsg')
call s:NOTI.notify(line, 'WarningMsg')
endfor
endfunction
@ -52,7 +52,7 @@ function! s:options() abort
\ ]
endfunction
function! git#push#complete(ArgLead, CmdLine, CursorPos)
function! git#push#complete(ArgLead, CmdLine, CursorPos) abort
let str = a:CmdLine[:a:CursorPos-1]
if str =~# '^Git\s\+push\s\+-$'
return join(s:options(), "\n")

View File

@ -7,13 +7,13 @@
" <
let s:JOB = SpaceVim#api#import('job')
let s:NOTI =SpaceVim#api#import('notification')
let s:NOTI = SpaceVim#api#import('notify')
let s:BUFFER = SpaceVim#api#import('vim#buffer')
let s:stash_show_bufnr = -1
function! git#stash#run(args)
function! git#stash#run(args) abort
let cmd = ['git', 'stash'] + a:args
let subcmd = get(a:args, 0, '')
@ -37,7 +37,7 @@ endfunction
function! s:on_stdout(id, data, event) abort
for line in filter(a:data, '!empty(v:val)')
call git#logger#info('git-stash stdout:' . line)
call s:NOTI.notification(line, 'Normal')
call s:NOTI.notify(line, 'Normal')
endfor
endfunction
@ -59,14 +59,14 @@ endfunction
function! s:on_drop_stdout(id, data, event) abort
for line in filter(a:data, '!empty(v:val)')
call git#logger#info('git-stash stdout:' . line)
call s:NOTI.notification(line, 'Normal')
call s:NOTI.notify(line, 'Normal')
endfor
endfunction
function! s:on_drop_stderr(id, data, event) abort
for line in filter(a:data, '!empty(v:val)')
call git#logger#info('git-stash stdout:' . line)
call s:NOTI.notification(line, 'WarningMsg')
call s:NOTI.notify(line, 'WarningMsg')
endfor
endfunction
@ -89,7 +89,7 @@ endfunction
function! s:on_show_stderr(id, data, event) abort
for line in filter(a:data, '!empty(v:val)')
call git#logger#info('git-stash stdout:' . line)
call s:NOTI.notification(line, 'WarningMsg')
call s:NOTI.notify(line, 'WarningMsg')
endfor
endfunction
@ -130,7 +130,7 @@ function! s:sub_commands() abort
\ "\n")
endfunction
function! git#stash#complete(ArgLead, CmdLine, CursorPos)
function! git#stash#complete(ArgLead, CmdLine, CursorPos) abort
let str = a:CmdLine[:a:CursorPos-1]
if str =~# '^Git\s\+stash\s\+[a-z]\=$'

29
docs/api/notify.md Normal file
View File

@ -0,0 +1,29 @@
---
title: "notify API"
description: "notify API provides some basic functions for generating notifications"
---
# [Available APIs](../) >> notify
<!-- vim-markdown-toc GFM -->
- [Intro](#intro)
- [Functions](#functions)
<!-- vim-markdown-toc -->
## Intro
This api provides some basic Functions for generating notifications.
```vim
let s:NOTIFY = SpaceVim#api#import('notify')
call s:NOTIFY.notify('This is a simple notification!')
```
## Functions
| function name | description |
| --------------------------- | ------------------------------------------------- |
| `notify(string)` | generate notification with default color |
| `notify(string, highlight)` | generate notification with custom highlight group |