Debian Codegraph @

lua/lualine/utils/notices.lua

raw
1 -- Copyright (c) 2020-2021 shadmansaleh
2 -- MIT license, see LICENSE for more details.
3
4 -- To provide notices for user
5 local M = {}
6 local notices = {}
7 local persistent_notices = {}
8
9 ---append new notice
10 ---@param notice string|table table is a list of strings
11 function M.add_notice(notice)
12 if type(notice) == 'string' then
13 notice = vim.split(notice, '\n')
14 end
15 if notice[#notice] ~= '' then
16 notice[#notice + 1] = ''
17 end
18 table.insert(notices, notice)
19 end
20
21 ---appends persistent notice. These don't get cleared on setup
22 ---@param notice string|table table is a list of strings
23 function M.add_persistent_notice(notice)
24 if type(notice) == 'string' then
25 notice = vim.split(notice, '\n')
26 end
27 if not vim.tbl_contains(persistent_notices, notice) then
28 table.insert(persistent_notices, notice)
29 end
30 end
31
32 ---show setup :LuaLineNotices and show notification about error when there
33 ---are notices available
34 local notify_done = false
35 function M.notice_message_startup()
36 notify_done = false
37 vim.defer_fn(function()
38 if notify_done then
39 return
40 end
41 if #notices > 0 or #persistent_notices > 0 then
42 vim.cmd('command! -nargs=0 LualineNotices lua require"lualine.utils.notices".show_notices()')
43 vim.notify(
44 'lualine: There are some issues with your config. Run :LualineNotices for details',
45 vim.log.levels.WARN,
46 {}
47 )
48 end
49 notify_done = true
50 end, 2000)
51 end
52
53 ---create notice view
54 function M.show_notices()
55 vim.cmd('silent! keepalt split')
56
57 local winid = vim.api.nvim_get_current_win()
58 local bufnr = vim.api.nvim_create_buf(false, true)
59 vim.api.nvim_win_set_buf(winid, bufnr)
60
61 vim.wo[winid].winfixheight = true
62 vim.wo[winid].winfixwidth = true
63 vim.wo[winid].number = false
64 vim.wo[winid].foldcolumn = '0'
65 vim.wo[winid].relativenumber = false
66 vim.wo[winid].signcolumn = 'no'
67 vim.bo[bufnr].filetype = 'markdown'
68
69 vim.api.nvim_buf_set_keymap(bufnr, 'n', 'q', '<Cmd>bd<CR>', { noremap = true, silent = true })
70
71 local ok, _ = pcall(vim.api.nvim_buf_set_name, 0, 'Lualine Notices')
72 if not ok then
73 vim.notify('Lualine Notices is already open in another buffer', vim.log.levels.ERROR, {})
74 vim.cmd('normal q')
75 return
76 end
77
78 local flatten
79 if vim.version.ge(vim.version(), { 0, 10, 0 }) then
80 flatten = function(t)
81 return vim.iter(t):flatten():totable()
82 end
83 else
84 flatten = vim.tbl_flatten
85 end
86
87 local notice = flatten(persistent_notices)
88 notice = vim.list_extend(notice, flatten(notices))
89 vim.fn.appendbufline(bufnr, 0, notice)
90
91 vim.fn.deletebufline(bufnr, #notice, vim.fn.line('$'))
92 vim.api.nvim_win_set_cursor(winid, { 1, 0 })
93 vim.bo[bufnr].modified = false
94 vim.bo[bufnr].modifiable = false
95 end
96
97 function M.clear_notices()
98 notices = {}
99 end
100
101 return M