-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample03.lua
108 lines (100 loc) · 4.86 KB
/
example03.lua
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
local nocurses = require("nocurses")
local llthreads = require("llthreads2.ex") -- https://luarocks.org/modules/moteus/lua-llthreads2
local mtmsg = require("mtmsg") -- https://luarocks.org/modules/osch/mtmsg
local function printf(...)
io.write(string.format(...))
io.flush()
end
local threadIn = mtmsg.newbuffer()
local threadOut = mtmsg.newbuffer()
threadOut:notifier(nocurses)
local thread = llthreads.new(function(inId, outId)
local mtmsg = require("mtmsg")
local threadIn = mtmsg.buffer(inId)
local threadOut = mtmsg.buffer(outId)
local started = mtmsg.time()
local lastReport = mtmsg.time()
local paused = false
local DURATION = 10 -- work has finished after 10 seconds
local function progress(text)
return string.format("%-8s %3d%%", text, math.floor((mtmsg.time() - started)/DURATION*100))
end
threadOut:addmsg(progress("working"))
threadIn:nonblock(true)
while true do
local msg = threadIn:nextmsg()
if msg == "start" then
if not started or paused then
if paused then
started = mtmsg.time() - paused
else
started = mtmsg.time()
end
paused = false
threadOut:addmsg(progress("working"))
lastReport = mtmsg.time()
threadIn:nonblock(true)
end
elseif msg == "pause" then
if started and not paused then
paused = mtmsg.time() - started
threadOut:addmsg(progress("paused"))
threadIn:nonblock(false)
end
elseif msg == "terminate" then
threadOut:addmsg("terminated")
return
end
if started and not paused then
-- do some "work" here ...
if mtmsg.time() - lastReport >= 1 then
threadOut:addmsg(progress("working"))
lastReport = mtmsg.time()
end
if mtmsg.time() - started > DURATION then
started = false
threadOut:addmsg("finished")
threadIn:nonblock(false)
end
end
end
end,
threadIn:id(), threadOut:id())
thread:start()
nocurses.setfontbold(true)
printf("Press <Q> to Quit, <Space> for thread start/stop...\n")
nocurses.setfontbold(false)
local started = true
while true do
local c = nocurses.getch()
c = c and string.char(c)
local status = threadOut:nextmsg(0)
if status then
if status:match("^paused") then
started = false
nocurses.setfontbold(true)
nocurses.setfontcolor("RED")
elseif status:match("^working") then
started = true
elseif status:match("^finished") then
started = false
nocurses.setfontbold(true)
nocurses.setfontcolor("GREEN")
end
printf("Thread status: %s\n", status)
nocurses.resetcolors()
end
if c == ' ' then
if started then
threadIn:addmsg("pause")
else
threadIn:addmsg("start")
end
elseif c == 'q' or c == 'Q' then
threadIn:addmsg("terminate")
break
end
end
thread:join()
nocurses.setfontbold(true)
printf("Quit.\n")