summaryrefslogtreecommitdiff
path: root/script/brave/work.lua
blob: 907f78cf166259b3acf01539faef78ebe457caf4 (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
63
64
65
66
local brave   = require 'brave.brave'
local parser  = require 'parser'
local fs      = require 'bee.filesystem'
local furi    = require 'file-uri'
local util    = require 'utility'
local thread  = require 'bee.thread'

brave.on('loadProto', function ()
    local jsonrpc = require 'jsonrpc'
    while true do
        local proto, err = jsonrpc.decode(io.read, log.error)
        --log.debug('loaded proto', proto.method)
        if not proto then
            brave.push('protoerror', err)
            return
        end
        brave.push('proto', proto)
    end
end)

brave.on('timer', function (time)
    while true do
        thread.sleep(time)
        brave.push('wakeup')
    end
end)

brave.on('compile', function (text)
    local state, err = parser:compile(text, 'lua', 'Lua 5.4')
    if not state then
        log.error(err)
        return
    end
    local lines = parser:lines(text)
    return {
        root  = state.root,
        value = state.value,
        errs  = state.errs,
        lines = lines,
    }
end)

brave.on('listDirectory', function (uri)
    local path = fs.path(furi.decode(uri))
    local uris = {}
    for child in path:list_directory() do
        local childUri = furi.encode(child:string())
        uris[#uris+1] = childUri
    end
    return uris
end)

brave.on('isDirectory', function (uri)
    local path = fs.path(furi.decode(uri))
    return fs.is_directory(path)
end)

brave.on('loadFile', function (uri)
    local filename = furi.decode(uri)
    return util.loadFile(filename)
end)

brave.on('saveFile', function (params)
    local filename = furi.decode(params.uri)
    return util.saveFile(filename, params.text)
end)