summaryrefslogtreecommitdiff
path: root/.config/vis/vis-compile-win/init.lua
blob: 02fc3dd66d1bbdd5895a2b3f20cd41b98379fe24 (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
51
52
53
54
55
56
57
58
59
60
61
62
require('vis')
local process_name = "vis-compile-win"

local buf

local M = {}

local function append_to_win(win, buffer)
  local file = win.file
  local pos = win.selection.pos
  local result = file:insert(file.size, buffer)
  file.modified = false
  win.selection.pos = pos
end

function M:compile(cmd, callback)
  vis:command"new"
  self.callback = callback
  self.win = vis.win
  self.buf = [[Compilation started at ]]..os.date("%a %b %d %X")..[[


]]..cmd..[[

]]
  append_to_win(self.win, self.buf)
	self.fd = vis:communicate(process_name, cmd)
	return true
end

function M:recv_data(buffer)
  local data = buffer or ""
  self.buf = self.buf..data
  append_to_win(self.win, data)
  return true
end

function M:finish_compilation(event, code, buffer)
  local footer = [[

Compilation finished at ]]..os.date("%a %b %d %X")
  self.buf = self.buf..footer
  append_to_win(self.win, footer)
  return self:callback()
end

vis:command_register("Compile", function(argv, force, win, selection, range)
	return M:compile(table.concat(argv, " "))
end)

vis.events.subscribe(vis.events.PROCESS_RESPONSE, function(name, event, code, buffer)
	if name ~= process_name then end

  if event == 'EXIT' or event == 'SIGNAL' then
    M:finish_compilation(event, code, buffer)
    return
  end

	M:recv_data(buffer)
end)

return M