Skip to content

Commit

Permalink
feat: Add support for vim.notify. Closes #191
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed Sep 15, 2023
1 parent a0f6c21 commit 0df3e98
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion autoload/db_ui.vim
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ function! s:dbui.connect(db) abort

try
let query_time = reltime()
call db_ui#notifications#info('Connecting to db '.a:db.name.'...', { 'echo': 1 })
call db_ui#notifications#info('Connecting to db '.a:db.name.'...')
let a:db.conn = db#connect(a:db.url)
let a:db.conn_error = ''
call db_ui#notifications#info('Connected to db '.a:db.name.' after '.split(reltimestr(reltime(query_time)))[0].' sec.')
Expand Down
6 changes: 3 additions & 3 deletions autoload/db_ui/drawer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,15 @@ function! s:drawer.render(...) abort

if get(opts, 'dbs', 0)
let query_time = reltime()
call db_ui#notifications#info('Refreshing all databases...', { 'echo': 1 })
call db_ui#notifications#info('Refreshing all databases...')
call self.dbui.populate_dbs()
call db_ui#notifications#info('Refreshed all databases after '.split(reltimestr(reltime(query_time)))[0].' sec.')
endif

if !empty(get(opts, 'db_key_name', ''))
let query_time = reltime()
let db = self.dbui.dbs[opts.db_key_name]
call db_ui#notifications#info('Refreshing database '.db.name.'...', {'echo': 1 })
call db_ui#notifications#info('Refreshing database '.db.name.'...')
let query_time = reltime()
let self.dbui.dbs[opts.db_key_name] = self.populate(db)
call db_ui#notifications#info('Refreshed database '.db.name.' after '.split(reltimestr(reltime(query_time)))[0].' sec.')
endif
Expand Down
29 changes: 24 additions & 5 deletions autoload/db_ui/notifications.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ let s:width = g:db_ui_notification_width "Default notification width
let s:pos = 'bot'.g:db_ui_win_position "Default position for notification
let s:title = '[DBUI]' "Title of notification
let s:last_msg = ''
let s:use_nvim_notify = g:db_ui_use_nvim_notify

let s:colors_set = 0

if s:use_nvim_notify && !has('nvim')
echoerr "Option db_ui_use_nvim_notify is supported only in neovim"
endif

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Public API, adapt names to your needs "
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Expand Down Expand Up @@ -47,16 +52,20 @@ function! s:notification(msg, opts) abort
return
endif

if !s:colors_set
call s:setup_colors()
let s:colors_set = 1
endif

let use_echo = get(a:opts, 'echo', 0)
if !use_echo
let use_echo = g:db_ui_force_echo_notifications
endif

if s:use_nvim_notify && !use_echo
return s:notification_nvim_notify(a:msg, a:opts)
endif

if !s:colors_set
call s:setup_colors()
let s:colors_set = 1
endif

if s:neovim_float && !use_echo
return s:notification_nvim(a:msg, a:opts)
endif
Expand All @@ -79,6 +88,16 @@ function! s:nvim_close() abort
silent! call timer_stop(s:timer)
endfunction

function! s:notification_nvim_notify(msg, opts) abort
let type = get(a:opts, 'type', 'info')
let title = get(a:opts, 'title', s:title)
let opts = { 'title': title }
if get(a:opts, 'delay')
let opts.timeout = { 'timeout': a:opts.delay }
endif
return luaeval('vim.notify(_A[1], _A[2], _A[3])', [a:msg, type, opts])
endfunction

function! s:notification_nvim(msg, opts) abort
let width = get(a:opts, 'width', s:width)
let title = get(a:opts, 'title', s:title)
Expand Down
2 changes: 1 addition & 1 deletion autoload/db_ui/query.vim
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function! s:query.print_query_time() abort
return
endif
let self.last_query_time = split(reltimestr(reltime(self.last_query_start_time)))[0]
call db_ui#notifications#info('Executing query...Done after '.self.last_query_time.' sec.')
call db_ui#notifications#info('Done after '.self.last_query_time.' sec.')
endfunction

function! s:query.execute_lines(db, lines, is_visual_mode) abort
Expand Down
7 changes: 7 additions & 0 deletions doc/dadbod-ui.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ DBUILastQueryInfo
*DBUIHideNotifications*
DBUIHideNotifications
Hide all floating notifications that are currently shown.
Does not work with `vim.notify` and `echo` mode

`:DBUIHideNotifications`

Expand Down Expand Up @@ -703,6 +704,12 @@ g:db_ui_force_echo_notifications
Notifications are shown via popups when supported. To force
echoing messages to command line set this value to 1.

Default value: `0`

*g:db_ui_use_nvim_notify*
g:db_ui_use_nvim_notify
Use Neovim's `vim.notify` API for notifications.

Default value: `0`

*g:db_ui_hide_schemas*
Expand Down
1 change: 1 addition & 0 deletions plugin/db_ui.vim
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let g:db_ui_show_help = get(g:, 'db_ui_show_help', 1)
let g:db_ui_use_nerd_fonts = get(g:, 'db_ui_use_nerd_fonts', 0)
let g:db_ui_execute_on_save = get(g:, 'db_ui_execute_on_save', 1)
let g:db_ui_force_echo_notifications = get(g:, 'db_ui_force_echo_notifications', 0)
let g:db_ui_use_nvim_notify = get(g:, 'db_ui_use_nvim_notify', 0)
let g:Db_ui_buffer_name_generator = get(g:, 'Db_ui_buffer_name_generator', 0)
let g:db_ui_debug = get(g:, 'db_ui_debug', 0)
let g:db_ui_hide_schemas = get(g:, 'db_ui_hide_schemas', [])
Expand Down

0 comments on commit 0df3e98

Please sign in to comment.