|
| 1 | +local api = vim.api |
| 2 | +local win_id = nil |
| 3 | +local buf_id = nil |
| 4 | + |
| 5 | +local config = { |
| 6 | + row = 2, |
| 7 | + col_offset = 4, |
| 8 | +} |
| 9 | + |
| 10 | +local function close_banner() |
| 11 | + if win_id and api.nvim_win_is_valid(win_id) then |
| 12 | + api.nvim_win_close(win_id, true) |
| 13 | + end |
| 14 | + |
| 15 | + if buf_id and api.nvim_buf_is_valid(buf_id) then |
| 16 | + api.nvim_buf_delete(buf_id, { force = true }) |
| 17 | + end |
| 18 | + |
| 19 | + win_id = nil |
| 20 | + buf_id = nil |
| 21 | +end |
| 22 | + |
| 23 | +local function open_banner() |
| 24 | + local reg = vim.fn.reg_recording() |
| 25 | + |
| 26 | + if reg == '' then |
| 27 | + return |
| 28 | + end |
| 29 | + |
| 30 | + close_banner() |
| 31 | + |
| 32 | + local text = string.format(' ● REC @%s ', reg) |
| 33 | + |
| 34 | + buf_id = api.nvim_create_buf(false, true) |
| 35 | + api.nvim_buf_set_lines(buf_id, 0, -1, false, { text }) |
| 36 | + |
| 37 | + local col = vim.o.columns - #text - config.col_offset |
| 38 | + |
| 39 | + win_id = api.nvim_open_win(buf_id, false, { |
| 40 | + relative = 'editor', |
| 41 | + width = #text, |
| 42 | + height = 1, |
| 43 | + row = config.row, |
| 44 | + col = col, |
| 45 | + style = 'minimal', |
| 46 | + border = 'none', |
| 47 | + focusable = false, |
| 48 | + zindex = 150, |
| 49 | + }) |
| 50 | + |
| 51 | + api.nvim_set_option_value( |
| 52 | + 'winhighlight', |
| 53 | + 'Normal:DiagnosticError', |
| 54 | + { win = win_id } |
| 55 | + ) |
| 56 | +end |
| 57 | + |
| 58 | +local group = api.nvim_create_augroup('MacroRecordingBanner', { clear = true }) |
| 59 | + |
| 60 | +api.nvim_create_autocmd( |
| 61 | + 'RecordingEnter', |
| 62 | + { group = group, callback = open_banner } |
| 63 | +) |
| 64 | +api.nvim_create_autocmd('RecordingLeave', { |
| 65 | + group = group, |
| 66 | + callback = function() |
| 67 | + vim.defer_fn(close_banner, 50) |
| 68 | + end, |
| 69 | +}) |
| 70 | +api.nvim_create_autocmd('VimResized', { |
| 71 | + group = group, |
| 72 | + callback = function() |
| 73 | + if win_id and api.nvim_win_is_valid(win_id) then |
| 74 | + open_banner() |
| 75 | + end |
| 76 | + end, |
| 77 | +}) |
0 commit comments