forked from xyproto/algernon
/
luaextras.go
71 lines (64 loc) · 1.56 KB
/
luaextras.go
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
63
64
65
66
67
68
69
70
71
package main
import (
log "github.com/sirupsen/logrus"
"github.com/yuin/gopher-lua"
)
// Extra Lua functions
const luacode = `
-- Given the name of a python script in the same directory,
-- return the outputted lines as a table
function py(filename)
if filename == nil then
return {}
end
local cmd = "python " .. scriptdir() .. "/" .. filename
local f = assert(io.popen(cmd, 'r'))
local a = {}
for line in f:lines() do
table.insert(a, line)
end
f:close()
return a
end
-- Given the name of an executable (or executable script) in the same directory,
-- return the outputted lines as a table
function run(given_command)
if given_command == nil then
return {}
end
local cmd = "cd " .. scriptdir() .. "; " .. given_command
local f = assert(io.popen(cmd, 'r'))
local a = {}
for line in f:lines() do
table.insert(a, line)
end
f:close()
return a
end
-- List a table
function dir(t)
if t == nil then
t = _G
end
local output = {}
for k, v in pairs(t) do
table.insert(output, string.format("%-16s\t->\t%s", tostring(k), tostring(v)))
end
return table.concat(output, "\n")
end
`
// Lua function for converting a table to JSON (string or int)
func loadExtras(L *lua.LState) int {
if err := L.DoString(luacode); err != nil {
log.Error("Could not load Lua extras!")
log.Error(err)
}
//L.Push(lua.LString("Loaded extra functions"))
//return 1 // number of results
return 0 // number of results
}
func exportExtras(L *lua.LState) {
// Load extra Lua functions
//L.SetGlobal("extras", L.NewFunction(loadExtras))
loadExtras(L)
}