aboutsummaryrefslogtreecommitdiffstats
path: root/lua/pi/animations.lua
blob: c0b7a6c28d34de064acaea8d4e17229a81750acf (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
42
43
44
45
46
47
48
49
50
local redraw_buffer = function()
  local Animation = require "animation"
  local fps = 45 -- frames per second
  local easing = require "animation.easing"

  local buffer = 0
  local len = 0
  local place_holder = {}

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

  for _, line in ipairs(lines) do
    len = len + string.len(line)
    table.insert(place_holder, "")
  end

  vim.api.nvim_buf_set_lines(buffer, 0, #lines, true, place_holder)

  local duration = math.ceil(len / fps * 1000)

  local i = 1
  local line_nr = 1
  local insertLine = ""

  local function callback()
    if lines[line_nr] == nil then
      return true
    end
    local char = string.sub(lines[line_nr], i, i)
    if char == nil then
      return true
    end
    insertLine = insertLine .. char
    i = i + 1
    vim.api.nvim_buf_set_lines(buffer, line_nr - 1, line_nr, true, { insertLine })

    if insertLine == lines[line_nr] then
      i = 1
      line_nr = line_nr + 1
      insertLine = ""
    end
  end

  local animation = Animation(duration, fps, easing.line, callback)
  animation:run()
end

return {
  redraw_buffer = redraw_buffer,
}