aboutsummaryrefslogtreecommitdiffstats
path: root/lua/pi/animations.lua
blob: e4bc85977699be55e4cde4920e51caa58a84505e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-- Animación de "retipeo" del buffer, con vim.uv (antes dependía de animation.nvim).
local fps = 15 -- un caracter por frame, como la versión original

local redraw_buffer = function()
  local buffer = vim.api.nvim_get_current_buf()
  local lines = vim.api.nvim_buf_get_lines(buffer, 0, -1, true)

  local place_holder = {}
  for _ = 1, #lines do
    table.insert(place_holder, "")
  end
  vim.api.nvim_buf_set_lines(buffer, 0, -1, true, place_holder)

  local line_nr, col = 1, 0
  local timer = assert(vim.uv.new_timer())

  timer:start(
    0,
    math.floor(1000 / fps),
    vim.schedule_wrap(function()
      if not vim.api.nvim_buf_is_valid(buffer) or line_nr > #lines then
        timer:stop()
        timer:close()
        return
      end

      col = col + 1
      local line = lines[line_nr]
      vim.api.nvim_buf_set_lines(buffer, line_nr - 1, line_nr, true, { line:sub(1, col) })

      if col >= #line then
        line_nr = line_nr + 1
        col = 0
      end
    end)
  )
end

return {
  redraw_buffer = redraw_buffer,
}