Skip to content

Commit 7acbff6

Browse files
committed
new functions: peekch, skipch, getkey
1 parent 0921d4e commit 7acbff6

File tree

6 files changed

+596
-31
lines changed

6 files changed

+596
-31
lines changed

README.md

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ Supported platforms: Linux, MacOS and Cygwin.
4444
Demonstrates how to read keystrokes with multibyte sequences
4545
(arrow keys).
4646

47+
* [`example05.lua`](./examples/example05.lua)
48+
49+
Demonstrates how to read keystrokes with multibyte sequences
50+
(arrow keys) using the convenience method [nocurses.getkey()](#nocurses_getkey).
51+
4752
<!-- ---------------------------------------------------------------------------------------- -->
4853

4954
## Documentation
@@ -55,6 +60,9 @@ See also original documentation at https://github.com/LionyxML/nocurses.
5560
* [nocurses.clrline()](#nocurses_clrline)
5661
* [nocurses.clrscr()](#nocurses_clrscr)
5762
* [nocurses.getch()](#nocurses_getch)
63+
* [nocurses.peekch()](#nocurses_peekch)
64+
* [nocurses.skipch()](#nocurses_skipch)
65+
* [nocurses.getkey()](#nocurses_getkey)
5866
* [nocurses.gettermsize()](#nocurses_gettermsize)
5967
* [nocurses.gotoxy()](#nocurses_gotoxy)
6068
* [nocurses.resetcolors()](#nocurses_resetcolors)
@@ -74,32 +82,32 @@ See also original documentation at https://github.com/LionyxML/nocurses.
7482
## Module Functions
7583
<!-- ---------------------------------------------------------------------------------------- -->
7684

77-
* <a id="nocurses_awake">**` nocurses.awake()
85+
* <a id="nocurses_awake">**`nocurses.awake()
7886
`**</a>
7987

8088
May be called from any thread to interrupt [nocurses.getch()](#nocurses_getch) on the main
8189
thread. The main thread is the first thread that loads the *nocurses* module.
8290

8391
<!-- ---------------------------------------------------------------------------------------- -->
8492

85-
* <a id="nocurses_clrline">**` nocurses.clrline()
93+
* <a id="nocurses_clrline">**`nocurses.clrline()
8694
`**</a>
8795

8896
Clears the row contents.
8997

9098
<!-- ---------------------------------------------------------------------------------------- -->
9199

92-
* <a id="nocurses_clrscr">**` nocurses.clrscr()
100+
* <a id="nocurses_clrscr">**`nocurses.clrscr()
93101
`**</a>
94102

95103
Clears the screen.
96104

97105
<!-- ---------------------------------------------------------------------------------------- -->
98106

99-
* <a id="nocurses_getch">**` nocurses.getch(timeout)
107+
* <a id="nocurses_getch">**`nocurses.getch([timeout])
100108
`**</a>
101109

102-
Gets a character without waiting for enter.
110+
Gets a character byte without waiting for enter.
103111

104112
* *timeout* - optional float, timeout in seconds.
105113

@@ -119,87 +127,143 @@ See also original documentation at https://github.com/LionyxML/nocurses.
119127
see: [src/notify_capi.h](./src/notify_capi.h).
120128

121129
* the terminal size changes.
130+
131+
Otherwise this function returns the obtained character byte as integer value.
132+
133+
<!-- ---------------------------------------------------------------------------------------- -->
134+
* <a id="nocurses_peekch">**`nocurses.peekch([cnt])
135+
`**</a>
136+
137+
Obtains next character byte from the input queue without consuming the character, i.e.
138+
a subsequent call to [nocurses.getch()](#nocurses_getch) will deliver this byte again.
139+
140+
* *cnt* - optional integer, number of the byte in the input queue to obtain, first byte
141+
has number 1. If not given, the default value is 1, i.e. the first byte in the
142+
input queue is obtained.
143+
144+
This function does not wait and will return *nil* if the specified input byte is not
145+
available.
146+
147+
The input character byte is returned as integer value.
148+
149+
<!-- ---------------------------------------------------------------------------------------- -->
150+
* <a id="nocurses_skipch">**`nocurses.skipch([cnt])
151+
`**</a>
152+
153+
Discards bytes in the input queue.
154+
155+
* *cnt* - optional integer, number of bytes in the input queue to discard. If not given,
156+
the default value is 1, i.e. the first byte in the input queue is skipped,
157+
if available..
158+
159+
This function does not wait and will skip only input bytes in the queue that are immediately
160+
available.
161+
162+
This funtion returns the number of bytes skipped in the input queue.
163+
164+
<!-- ---------------------------------------------------------------------------------------- -->
165+
* <a id="nocurses_getkey">**`nocurses.getkey([timeout])
166+
`**</a>
167+
168+
Returns the name of the pressed key as string.
169+
170+
This function is implemented in Lua (see [`getkey.lua`](./src/nocurses/getkey.lua))
171+
using the low level functions [getch()](#nocurses_getch),
172+
[peekch()](#nocurses_peekch) and [skipch()](#nocurses_skipch) and
173+
heuristically determines the pressed key for the current input byte sequence.
174+
175+
* *timeout* - optional float, timeout in seconds.
176+
177+
The timout handling is the same as in the function [nocurses.getch()](#nocurses_getch).
178+
179+
If a pressed key could be determined, this functions returns the key name as string
180+
and the consumed raw input bytes as string.
181+
182+
If the pressed key could not be determined from the input byte sequence, this functions
183+
returns the boolean value *false* and the consumed raw input bytes as string.
184+
185+
See also: [`example05.lua`](./examples/example05.lua)
122186

123187
<!-- ---------------------------------------------------------------------------------------- -->
124188

125-
* <a id="nocurses_gettermsize">**` nocurses.gettermsize()
189+
* <a id="nocurses_gettermsize">**`nocurses.gettermsize()
126190
`**</a>
127191

128192
Gets the columns and rows of the terminal.
129193

130194
<!-- ---------------------------------------------------------------------------------------- -->
131195

132-
* <a id="nocurses_gotoxy">**` nocurses.gotoxy(x, y)
196+
* <a id="nocurses_gotoxy">**`nocurses.gotoxy(x, y)
133197
`**</a>
134198

135199
Sets the cursor do the position x, y. Where x is the row number and y the line number.
136200

137201
<!-- ---------------------------------------------------------------------------------------- -->
138202

139-
* <a id="nocurses_resetcolors">**` nocurses.resetcolors()
203+
* <a id="nocurses_resetcolors">**`nocurses.resetcolors()
140204
`**</a>
141205

142206
Reset terminal to default colors.
143207

144208
<!-- ---------------------------------------------------------------------------------------- -->
145209

146-
* <a id="nocurses_setbgrcolor">**` nocurses.setbgrcolor(colorName)
210+
* <a id="nocurses_setbgrcolor">**`nocurses.setbgrcolor(colorName)
147211
`**</a>
148212

149213
Sets the background color to one of the colors described on the color table below.
150214

151215
<!-- ---------------------------------------------------------------------------------------- -->
152216

153-
* <a id="nocurses_setblink">**` nocurses.setblink(status)
217+
* <a id="nocurses_setblink">**`nocurses.setblink(status)
154218
`**</a>
155219

156220
Sets the blink attribute on or off. *status* can be *true* or *false*.
157221

158222
<!-- ---------------------------------------------------------------------------------------- -->
159223

160-
* <a id="nocurses_setcurshape">**` nocurses.setcurshape(shapeName)
224+
* <a id="nocurses_setcurshape">**`nocurses.setcurshape(shapeName)
161225
`**</a>
162226

163227
Sets the shape of the cursor in the terminal in the shape table below.
164228

165229
<!-- ---------------------------------------------------------------------------------------- -->
166230

167-
* <a id="nocurses_setfontbold">**` nocurses.setfontbold(status)
231+
* <a id="nocurses_setfontbold">**`nocurses.setfontbold(status)
168232
`**</a>
169233

170234
Sets the bold attribute on or off. *status* can be *true* or *false*.
171235

172236
<!-- ---------------------------------------------------------------------------------------- -->
173237

174-
* <a id="nocurses_setfontcolor">**` nocurses.setfontcolor(colorName)
238+
* <a id="nocurses_setfontcolor">**`nocurses.setfontcolor(colorName)
175239
`**</a>
176240

177241
Sets the text color to one of the colors described on the color table below.
178242

179243
<!-- ---------------------------------------------------------------------------------------- -->
180244

181-
* <a id="nocurses_setinvert">**` nocurses.setinvert(status)
245+
* <a id="nocurses_setinvert">**`nocurses.setinvert(status)
182246
`**</a>
183247

184248
Sets the invert attribute on or off. *status* can be *true* or *false*.
185249

186250
<!-- ---------------------------------------------------------------------------------------- -->
187251

188-
* <a id="nocurses_settitle">**` nocurses.settitle(title)
252+
* <a id="nocurses_settitle">**`nocurses.settitle(title)
189253
`**</a>
190254

191255
Sets the title of the terminal.
192256

193257
<!-- ---------------------------------------------------------------------------------------- -->
194258

195-
* <a id="nocurses_setunderline">**` nocurses.setunderline(status)
259+
* <a id="nocurses_setunderline">**`nocurses.setunderline(status)
196260
`**</a>
197261

198262
Sets the underline attribute on or off. *status* can be *true* or *false*.
199263

200264
<!-- ---------------------------------------------------------------------------------------- -->
201265

202-
* <a id="nocurses_wait">**` nocurses.wait()
266+
* <a id="nocurses_wait">**`nocurses.wait()
203267
`**</a>
204268

205269
Waits for the user to hit [ENTER].

examples/example03.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local nocurses = require("nocurses")
2-
local llthreads = require("llthreads2.ex")
3-
local mtmsg = require("mtmsg")
2+
local llthreads = require("llthreads2.ex") -- https://luarocks.org/modules/moteus/lua-llthreads2
3+
local mtmsg = require("mtmsg") -- https://luarocks.org/modules/osch/mtmsg
44

55

66
local function printf(...)

examples/example05.lua

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
local nocurses = require("nocurses")
2+
3+
local function printf(...)
4+
io.write(string.format(...))
5+
io.flush()
6+
end
7+
8+
local screenWidth, screenHeight = nocurses.gettermsize()
9+
local redisplay = true
10+
local value = 0
11+
12+
local function printCentered(y, ...)
13+
local message = string.format(...)
14+
local x = math.floor((screenWidth - #message) / 2)
15+
if x < 1 then x = 1 end
16+
nocurses.gotoxy(x, y)
17+
printf(message)
18+
end
19+
20+
while true do
21+
if redisplay then
22+
nocurses.clrscr();
23+
24+
local y = math.floor(screenHeight / 2)
25+
printCentered(y, "Value = %d", value)
26+
printCentered(y+1, "(use arrow keys to increase/decrease value, press q to Quit)")
27+
28+
nocurses.gotoxy(1, screenHeight - 1)
29+
redisplay = false
30+
end
31+
32+
local key = nocurses.getkey() -- might be nil if terminal size changes
33+
34+
local w, h = nocurses.gettermsize()
35+
if w ~= screenWidth or h ~= screenHeight then
36+
screenWidth, screenHeight = w, h
37+
redisplay = true
38+
end
39+
if key then
40+
if key == "Q" or key == "q" then
41+
break
42+
43+
elseif key == "Up" then
44+
value = value + 10
45+
46+
elseif key == "Down" then
47+
value = value - 10
48+
49+
elseif key == "Right" then
50+
value = value + 1
51+
52+
elseif key == "Left" then
53+
value = value - 1
54+
else
55+
print(inp)
56+
end
57+
redisplay = true
58+
end
59+
end
60+
61+
nocurses.gotoxy(1, screenHeight - 2)
62+
nocurses.clrline()
63+
printf("Finished.\n")

rockspecs/nocurses-scm-0.rockspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ dependencies = {
2121
build = {
2222
type = "builtin",
2323
modules = {
24-
nocurses = {
24+
["nocurses"] = {
2525
sources = {
2626
"src/main.c",
2727
"src/nocurses_compat.c",
2828
},
2929
defines = { "NOCURSES_VERSION="..version:gsub("^(.*)-.-$", "%1") },
3030
},
31+
["nocurses.getkey"] = "src/nocurses/getkey.lua",
3132
}
3233
}

0 commit comments

Comments
 (0)