1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 13:50:05 +08:00

feat(flygrep): add statusline for flygrep.lua

This commit is contained in:
Wang Shidong 2022-10-08 23:46:57 +08:00 committed by GitHub
parent dbdb33846a
commit cd193b852d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 5 deletions

View File

@ -0,0 +1,43 @@
local M = {}
function M.support_float()
return vim.fn.exists('*nvim_open_win') == 1
end
function M.opened()
return vim.fn.win_id2tabwin(M.__winid)[1] == vim.fn.tabpagenr()
end
function M.open_float(sl)
if M.__bufnr == nil or vim.fn.bufexists(M.__bufnr) == 0 then
M.__bufnr = vim.api.nvim_create_buf(false, true)
end
if M.__winid == nil or not M.opened() then
M.__winid = vim.api.nvim_open_win(M.__bufnr, false, {
relative = 'editor',
width = vim.o.columns,
height = 1,
-- highlight = 'SpaceVim_statusline_a_bold',
row = vim.o.lines - 2,
col = 0
})
end
vim.fn.setwinvar(M.__winid, '&winhighlight', 'Normal:SpaceVim_statusline_a_bold')
vim.fn.setbufvar(M.__bufnr, '&relativenumber', 0)
vim.fn.setbufvar(M.__bufnr, '&number', 0)
vim.fn.setbufvar(M.__bufnr, '&bufhidden', 'wipe')
vim.fn.setbufvar(M.__bufnr, '&cursorline', 0)
vim.fn.setbufvar(M.__bufnr, '&modifiable', 1)
vim.fn.setwinvar(vim.fn.win_id2win(M.__winid), '&cursorline', 0)
vim.api.nvim_buf_set_virtual_text( M.__bufnr, -1, 0, sl, {})
vim.fn.setbufvar(M.__bufnr, '&modifiable', 0)
return M.__winid
end
function M.close_float()
if M.__winid ~= nil then
vim.api.nvim_win_close(M.__winid, true)
end
end
return M

View File

@ -6,6 +6,7 @@ local hi = require('spacevim.api').import('vim.highlight')
local regex = require('spacevim.api').import('vim.regex')
local Key = require('spacevim.api').import('vim.keys')
local buffer = require('spacevim.api').import('vim.buffer')
local sl = require('spacevim.api').import('vim.statusline')
-- compatibility functions
local jobstart = vim.fn.jobstart
@ -146,7 +147,23 @@ local function grep_stderr(id, data, event)
end
local function update_statusline()
if sl.support_float() and vim.fn.win_id2tabwin(flygrep_win_id)[1] == vim.fn.tabpagenr() then
sl.open_float({
{'FlyGrep ', 'SpaceVim_statusline_a_bold'},
{'', 'SpaceVim_statusline_a_SpaceVim_statusline_b'},
{M.mode() .. ' ', 'SpaceVim_statusline_b'},
{'', 'SpaceVim_statusline_b_SpaceVim_statusline_c'},
{vim.fn.getcwd() .. ' ', 'SpaceVim_statusline_c'},
{'', 'SpaceVim_statusline_c_SpaceVim_statusline_b'},
{M.lineNr() .. ' ', 'SpaceVim_statusline_b'},
{'', 'SpaceVim_statusline_b_SpaceVim_statusline_z'},
{vim.fn['repeat'](' ', vim.o.columns - 11), 'SpaceVim_statusline_z'},
})
end
end
local function close_statusline()
sl.close_float()
end
local function grep_exit(id, data, event)
@ -258,6 +275,7 @@ local function next_item()
cursor[1] = cursor[1] + 1
end
vim.api.nvim_win_set_cursor(flygrep_win_id, cursor)
update_statusline()
vim.cmd('redraw')
mpt._build_prompt()
end
@ -270,6 +288,7 @@ local function previous_item()
cursor[1] = cursor[1] - 1
end
vim.api.nvim_win_set_cursor(flygrep_win_id, cursor)
update_statusline()
vim.cmd('redraw')
mpt._build_prompt()
end
@ -372,6 +391,24 @@ mpt._function_key = {
[Key.t('x80\xfc`\x80\xfdL')] = next_item,
}
function M.mode()
if mode == '' then
return grep_mode
else
return grep_mode .. '(' .. mode .. ')'
end
end
function M.lineNr()
if vim.fn.getbufline(buffer_id, 1)[1] == '' then
return 'no result'
else
local current = vim.api.nvim_win_get_cursor(flygrep_win_id)[1]
local total = vim.api.nvim_buf_line_count(buffer_id)
return current .. '/' .. total
end
end
function M.open(argv)
previous_winid = vim.fn.win_getid()
@ -447,6 +484,9 @@ function M.open(argv)
logger.info(' smart_case : ' .. vim.fn.string(grep_smart_case))
logger.info(' expr opt : ' .. vim.fn.string(grep_expr_opt))
mpt.open()
if sl.support_float() then
close_statusline()
end
logger.info('FlyGrep ending =====================')
vim.o.t_ve = save_tve
hi.hi(cursor_hi)