, 2014-2015
--- Released under the terms of the MIT license. See file LICENSE for details.
---[[ screenshot.lua
- a simple screenshot facility for termfx programs, outputs html in
- a string. Cell colors and attributes are preserved. Note, this only
- creates what is needed for a dump of the terminal/buffer's contents,
- nothing else. Just "...data...
"
-
- use:
-
- screenshot = require "screenshot"
-
- -- ... draw stuff ...
-
- html = screenshot()
-
- -- or to dump the contents of a buffer instead of the terminal:
-
- html = screenshot(buf)
-
- -- then write it to a file, surrounded by a html template as necessary.
---]] local tfx = require "termfx"
-
-local function to_html(scr)
- local fg, bg
- local res = {""}
- for y = 1, scr.h do
- for x = 1, scr.w do
- local cel = scr[y][x]
- if fg ~= cel.fg or bg ~= cel.bg then
- if fg then
- res[#res + 1] = ""
- end
- local fgcol, fgattr = tfx.colorinfo(cel.fg % 256), math.floor(cel.fg / 256)
- local bgcol = tfx.colorinfo(cel.bg % 256)
- local style, weight = "", ""
- if fgattr % 2 == 1 then
- weight = "; font-weight: bold"
- end
- fgattr = fgattr / 2
- if fgattr % 2 == 1 then
- style = "; text-decoration: underline"
- end
- fgattr = fgattr / 2
- if fgattr % 2 == 1 then
- fgcol, bgcol = bgcol, fgcol
- end
-
- res[#res + 1] = string.format(''
- fg = cel.fg
- bg = cel.bg
- end
- res[#res + 1] = string.format('%c', cel.ch)
- end
- res[#res + 1] = "
"
- end
- res[#res + 1] = ""
- return table.concat(res)
-end
-
-local function screenshot(buf)
- local w, h, getcell
- if buf then
- w, h = buf:size()
- getcell = function(x, y)
- return buf:getcell(x, y)
- end
- else
- w, h = tfx.size()
- getcell = tfx.getcell
- end
-
- local res = {
- w = w,
- h = h,
- }
- for y = 1, h do
- res[y] = {}
- for x = 1, w do
- res[y][x] = getcell(x, y)
- end
- end
-
- return to_html(res)
-end
-
-return screenshot
diff --git a/framework/lualib/3rd/termfx/termfxtest.lua b/framework/lualib/3rd/termfx/termfxtest.lua
deleted file mode 100755
index 50fe4ba..0000000
--- a/framework/lualib/3rd/termfx/termfxtest.lua
+++ /dev/null
@@ -1,254 +0,0 @@
--- sample for termfx
--- Gunnar Zötl , 2014-2015
--- Released under the terms of the MIT license. See file LICENSE for details.
-
-package.path = "samples/?.lua;"..package.path
-
-tfx = require "termfx"
-ui = require "simpleui"
-screenshot = require "screenshot"
-
-
-tfx.init()
-tfx.inputmode(tfx.input.ALT + tfx.input.MOUSE)
-tfx.outputmode(tfx.output.COL256)
-
-rev_keys = {}
-for k, v in pairs(tfx.key) do
- if rev_keys[v] then
- rev_keys[v] = rev_keys[v] .. ','..k
- else
- rev_keys[v] = k
- end
-end
-
-function find_name(tbl, val)
- for k, v in pairs(tbl) do
- if val == v then return k end
- end
- return nil
-end
-
-function tbl_keys(tbl)
- local res = {}
- for k, v in pairs(tbl) do
- res[#res+1] = k
- end
- table.sort(res, function(i, k) return tbl[i] < tbl[k] end)
- return res
-end
-
-function pr_event(x, y, evt)
- evt = evt or {}
-
- tfx.attributes(tfx.color.BLUE, tfx.color.WHITE)
- tfx.printat(x, y, "Event:")
- tfx.printat(x+9, y, evt.type)
-
- tfx.attributes(tfx.color.WHITE, tfx.color.BLACK)
-
- if evt and evt.type then
- tfx.printat(x, y+1, "elapsed")
- tfx.printat(x+8, y+1, evt.elapsed)
- end
-
- if evt.type == "key" then
- tfx.printat(x, y+2, "mod")
- tfx.printat(x+8, y+2, evt.mod)
- tfx.printat(x, y+3, "key")
- tfx.printat(x+8, y+3, rev_keys[evt.key] or evt.key)
- tfx.printat(x, y+4, "ch")
- tfx.printat(x+8, y+4, evt.ch)
- tfx.printat(x, y+5, "char")
- tfx.printat(x+8, y+5, evt.char)
- elseif evt.type == "resize" then
- tfx.printat(x, y+2, "w")
- tfx.printat(x+8, y+2, evt.w)
- tfx.printat(x, y+3, "h")
- tfx.printat(x+8, y+3, evt.h)
- elseif evt.type == "mouse" then
- tfx.printat(x, y+2, "x")
- tfx.printat(x+8, y+2, evt.x)
- tfx.printat(x, y+3, "y")
- tfx.printat(x+8, y+3, evt.y)
- tfx.printat(x, y+4, "key")
- tfx.printat(x+8, y+4, rev_keys[evt.key] or evt.key)
- end
-end
-
-function pr_colors(x, y, w)
- tfx.attributes(tfx.color.WHITE, tfx.color.BLACK)
- tfx.printat(x, y, "BLACK", w)
- tfx.attributes(tfx.color.WHITE, tfx.color.RED)
- tfx.printat(x, y+1, "RED", w)
- tfx.attributes(tfx.color.WHITE, tfx.color.GREEN)
- tfx.printat(x, y+2, "GREEN", w)
- tfx.attributes(tfx.color.BLACK, tfx.color.YELLOW)
- tfx.printat(x, y+3, "YELLOW", w)
- tfx.attributes(tfx.color.WHITE, tfx.color.BLUE)
- tfx.printat(x, y+4, "BLUE", w)
- tfx.attributes(tfx.color.BLACK, tfx.color.MAGENTA)
- tfx.printat(x, y+5, "MAGENTA", w)
- tfx.attributes(tfx.color.BLACK, tfx.color.CYAN)
- tfx.printat(x, y+6, "CYAN", w)
- tfx.attributes(tfx.color.BLACK, tfx.color.WHITE)
- tfx.printat(x, y+7, "WHITE", w)
-end
-
-function pr_stats(x, y)
- local tw, th = tfx.size()
- local im = tfx.inputmode()
- local om = tfx.outputmode()
-
- tfx.attributes(tfx.color.WHITE, tfx.color.BLACK)
- tfx.printat(x, y, "Size:")
- tfx.printat(x+8, y, tw .. " x " .. th)
- tfx.printat(x, y+1, "Input: ")
- tfx.printat(x+8, y+1, find_name(tfx.input, im))
- tfx.printat(x, y+2, "Output: ")
- tfx.printat(x+8, y+2, find_name(tfx.output, om))
-end
-
-function pr_coltbl(x, y)
- local i = 0
- local om = tfx.outputmode()
-
- if om == tfx.output.NORMAL or om == tfx.output.COL256 then
- for j=i, i+7 do
- tfx.attributes(tfx.color.WHITE, j)
- tfx.printat(x, y+j, string.format("%02X", j), 2)
- tfx.attributes(tfx.color.WHITE, j+8)
- tfx.printat(x+3, y+j, string.format("%02X", j+8), 2)
- end
- i = 16
- x = x+6
- end
-
- if om == tfx.output.COL216 or om == tfx.output.COL256 then
- for j=0, 11 do
- for k=0, 15 do
- local col = k*12+j+i
- tfx.attributes(tfx.color.WHITE, col)
- tfx.printat(x+k*3, y+j, string.format("%02X", col), 2)
- end
- end
- x = x+48
- i=i+216
- end
-
- if om == tfx.output.GRAYSCALE or om == tfx.output.COL256 then
- for j=0, 11 do
- for k=0, 1 do
- local col = k*12+j+i
- tfx.attributes(tfx.color.WHITE, col)
- tfx.printat(x+k*3, y+j, string.format("%02X", col), 2)
- end
- end
- end
-end
-
-function pr_formats(x, y)
- local fg, bg = tfx.attributes()
-
- tfx.printat(x, y, "Normal")
- x = x + 7
- tfx.attributes(fg + tfx.format.BOLD, bg)
- tfx.printat(x, y, "Bold")
- x = x + 5
- tfx.attributes(fg + tfx.format.UNDERLINE, bg)
- tfx.printat(x, y, "Under")
- x = x + 6
- tfx.attributes(fg + tfx.format.REVERSE, bg)
- tfx.printat(x, y, "Reverse")
-
- tfx.attributes(fg, bg)
-end
-
-function blit_a_bit(x, y, w, h)
- tfx.attributes(tfx.color.WHITE, tfx.color.BLACK)
- ui.box(x, y, w, h)
-
- local buf = tfx.newbuffer(8, 6)
- buf:clear(tfx.color.WHITE, tfx.color.BLACK)
-
- local cell = tfx.newcell('#', tfx.color.YELLOW, tfx.color.GREEN)
- local eye = tfx.newcell('O', tfx.color.BLACK, tfx.color.GREEN)
- local mouth = tfx.newcell('X', tfx.color.BLACK, tfx.color.GREEN)
-
- for i=3, 6 do
- buf:setcell(i, 1, cell)
- buf:setcell(i, 6, cell)
- end
- buf:rect(1, 2, 8, 4, cell)
-
- buf:setcell(3, 3, eye)
- buf:setcell(6, 3, eye)
- buf:setcell(2, 4, mouth)
- buf:setcell(7, 4, mouth)
- buf:printat(3, 5, { mouth, mouth, mouth, mouth})
-
- tfx.blit(x, y, buf)
- tfx.blit(x+w-8, y+6, buf)
- tfx.blit(x+w-8, y+h-6, buf)
- tfx.blit(x, y+h-12, buf)
-end
-
-function select_inputmode()
- local which = ui.select("select input mode", tbl_keys(tfx.input))
- if which then tfx.inputmode(tfx.input[which]) end
-end
-
-function select_outputmode()
- local which = ui.select("select output mode", tbl_keys(tfx.output))
- if which then tfx.outputmode(tfx.output[which]) end
-end
-
-ok, err = pcall(function()
-
- local quit = false
- local evt
- repeat
-
- tfx.clear(tfx.color.WHITE, tfx.color.BLACK)
- tfx.printat(1, tfx.height(), "press I for input mode, O for output mode, S for screenshot, Q to quit")
-
- tfx.printat(1, 1, _VERSION)
- pr_event(1, 3, evt)
- pr_stats(25, 1)
- pr_formats(1, 10)
- pr_colors(50, 1, 10)
- pr_coltbl(1, 12)
- blit_a_bit(62, 2, 18, 21)
-
- tfx.present()
- evt = tfx.pollevent()
-
- tfx.attributes(tfx.color.WHITE, tfx.color.BLUE)
- if evt.char == "q" or evt.char == "Q" then
- quit = ui.ask("Really quit?")
- evt = {}
- elseif evt.char == "i" or evt.char == "I" then
- select_inputmode()
- evt = {}
- elseif evt.char == "o" or evt.char == "O" then
- select_outputmode()
- evt = {}
- elseif evt.char == "s" or evt.char == "S" then
- local f = io.open("screenshot.html", "w")
- if f then
- f:write("")
- f:write(screenshot())
- f:write("")
- f:close()
- ui.message("Screenshot saved to screenshot.html")
- else
- ui.message("Could not save screenshot.")
- end
- evt = {}
- end
-
- until quit
-
-end)
-tfx.shutdown()
-if not ok then print("Error: "..err) end
diff --git a/framework/lualib/3rd/termfx/simpleui.lua b/framework/simulation/simpleui.lua
similarity index 100%
rename from framework/lualib/3rd/termfx/simpleui.lua
rename to framework/simulation/simpleui.lua
diff --git a/framework/lualib-src/luasocket/src/socket.lua b/framework/simulation/socket.lua
similarity index 69%
rename from framework/lualib-src/luasocket/src/socket.lua
rename to framework/simulation/socket.lua
index d1c0b16..b45bb01 100644
--- a/framework/lualib-src/luasocket/src/socket.lua
+++ b/framework/simulation/socket.lua
@@ -2,7 +2,6 @@
-- LuaSocket helper module
-- Author: Diego Nehab
-----------------------------------------------------------------------------
-
-----------------------------------------------------------------------------
-- Declare module and import dependencies
-----------------------------------------------------------------------------
@@ -25,9 +24,13 @@ function _M.connect6(address, port, laddress, lport)
end
function _M.bind(host, port, backlog)
- if host == "*" then host = "0.0.0.0" end
+ if host == "*" then
+ host = "0.0.0.0"
+ end
local addrinfo, err = socket.dns.getaddrinfo(host);
- if not addrinfo then return nil, err end
+ if not addrinfo then
+ return nil, err
+ end
local sock, res
err = "no info on address"
for i, alt in base.ipairs(addrinfo) do
@@ -36,7 +39,9 @@ function _M.bind(host, port, backlog)
else
sock, err = socket.tcp6()
end
- if not sock then return nil, err end
+ if not sock then
+ return nil, err
+ end
sock:setoption("reuseaddr", true)
res, err = sock:bind(alt.addr, port)
if not res then
@@ -61,8 +66,11 @@ function _M.choose(table)
name, opt1, opt2 = "default", name, opt1
end
local f = table[name or "nil"]
- if not f then base.error("unknown key (".. base.tostring(name) ..")", 3)
- else return f(opt1, opt2) end
+ if not f then
+ base.error("unknown key (" .. base.tostring(name) .. ")", 3)
+ else
+ return f(opt1, opt2)
+ end
end
end
@@ -78,27 +86,40 @@ _M.BLOCKSIZE = 2048
sinkt["close-when-done"] = function(sock)
return base.setmetatable({
- getfd = function() return sock:getfd() end,
- dirty = function() return sock:dirty() end
+ getfd = function()
+ return sock:getfd()
+ end,
+ dirty = function()
+ return sock:dirty()
+ end,
}, {
__call = function(self, chunk, err)
if not chunk then
sock:close()
return 1
- else return sock:send(chunk) end
- end
+ else
+ return sock:send(chunk)
+ end
+ end,
})
end
sinkt["keep-open"] = function(sock)
return base.setmetatable({
- getfd = function() return sock:getfd() end,
- dirty = function() return sock:dirty() end
+ getfd = function()
+ return sock:getfd()
+ end,
+ dirty = function()
+ return sock:dirty()
+ end,
}, {
__call = function(self, chunk, err)
- if chunk then return sock:send(chunk)
- else return 1 end
- end
+ if chunk then
+ return sock:send(chunk)
+ else
+ return 1
+ end
+ end,
})
end
@@ -108,40 +129,56 @@ _M.sink = _M.choose(sinkt)
sourcet["by-length"] = function(sock, length)
return base.setmetatable({
- getfd = function() return sock:getfd() end,
- dirty = function() return sock:dirty() end
+ getfd = function()
+ return sock:getfd()
+ end,
+ dirty = function()
+ return sock:dirty()
+ end,
}, {
__call = function()
- if length <= 0 then return nil end
+ if length <= 0 then
+ return nil
+ end
local size = math.min(socket.BLOCKSIZE, length)
local chunk, err = sock:receive(size)
- if err then return nil, err end
+ if err then
+ return nil, err
+ end
length = length - string.len(chunk)
return chunk
- end
+ end,
})
end
sourcet["until-closed"] = function(sock)
local done
return base.setmetatable({
- getfd = function() return sock:getfd() end,
- dirty = function() return sock:dirty() end
+ getfd = function()
+ return sock:getfd()
+ end,
+ dirty = function()
+ return sock:dirty()
+ end,
}, {
__call = function()
- if done then return nil end
+ if done then
+ return nil
+ end
local chunk, err, partial = sock:receive(socket.BLOCKSIZE)
- if not err then return chunk
+ if not err then
+ return chunk
elseif err == "closed" then
sock:close()
done = 1
return partial
- else return nil, err end
- end
+ else
+ return nil, err
+ end
+ end,
})
end
-
sourcet["default"] = sourcet["until-closed"]
_M.source = _M.choose(sourcet)