Skip to content

Commit

Permalink
add module rote.cursesConsts
Browse files Browse the repository at this point in the history
This modules was previously part of alnbox:

https://github.com/starius/alnbox
  • Loading branch information
starius committed Mar 30, 2015
1 parent f908554 commit 61a48f8
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ rt:keyPress(keycode)
```

You can get values of keycodes from [posix.curses][3].
Unfortunately it should be initialized, otherwise
constants are not available. Initialization of curses
may be undesirable in an application (testing tool),
which runs another application, which runs curses.
There is a workaround: module `"rote.cursesConsts"`.
It uses rote to run child Lua process, which initializes
curses and prints values of constants.
The module `"rote.cursesConsts"` returns them
as a table.

### Snapshots

Expand Down
1 change: 1 addition & 0 deletions lua-rote-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ build = {
libdirs = {"$(ROTE_LIBDIR)"},
libraries = {"rote"},
},
['rote.cursesConsts'] = 'src/cursesConsts.lua',
},
install = {
bin = { "demo/boxshell.lua" }
Expand Down
14 changes: 14 additions & 0 deletions spec/cursesConsts_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- This file is part of lua-rote, Lua binding to ROTE
-- Terminal Emulation library
-- Copyright (C) 2015 Boris Nagaev
-- See the LICENSE file for terms of use.

describe("rote.cursesConsts", function()
it("gets values of curses constants", function()
local cursesConsts = require 'rote.cursesConsts'
assert.truthy(cursesConsts.KEY_UP)
assert.truthy(cursesConsts.KEY_DOWN)
assert.truthy(cursesConsts.KEY_LEFT)
assert.truthy(cursesConsts.KEY_RIGHT)
end)
end)
57 changes: 57 additions & 0 deletions src/cursesConsts.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
-- lua-rote, Lua binding to ROTE, Terminal Emulation library
-- Copyright (C) 2015 Boris Nagaev
-- See the LICENSE file for terms of use.

-- ROTE is a simple C library for VT102 terminal emulation.
-- See http://rote.sourceforge.net/

-- returns table of curses numeric consts
-- module posix.curses requires starting curses,
-- otherwise these consts are not initialized.
-- Starting curses if often not desirable.
-- This module spawns child process, which
-- prints values of consts.

local rote = require 'rote'
local wait = assert(require "posix.sys.wait")

local function printConsts()
local curses = require 'posix.curses'
local stdscr = curses.initscr()
curses.endwin()
local out_fname = assert(arg[1])
local out = io.open(out_fname, 'w')
out:write('return {')
for name, value in pairs(curses) do
if type(value) == "number" then
local t = "[%q] = %i,"
out:write(t:format(name, value))
end
end
out:write('}')
out:close()
end

local fname = os.tmpname()
local f = io.open(fname, 'w')
f:write(string.dump(printConsts))
f:close()

local fname2 = os.tmpname()

local lluacov = os.getenv('LOAD_LUACOV') or ''

local cmd = 'lua %s %s %s'
cmd = cmd:format(lluacov, fname, fname2)

local rt = rote.RoteTerm(24, 80)
local pid = rt:forkPty(cmd)
wait.wait(pid)
rt:forsakeChild()

local consts = dofile(fname2)

os.remove(fname)
os.remove(fname2)

return consts

0 comments on commit 61a48f8

Please sign in to comment.