From 057f4a2870188bfdbdc59b3f16f3b3b4fb3a4304 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 5 Mar 2022 09:47:47 -0800 Subject: [PATCH] anagrams.tlv: slightly more responsive Now we cancel screen-painting if any key is pressed. However it looks like just computing the list of anagrams can take a long time. --- anagrams.tlv | 12 +++++++++++- src/lcurses/window.c | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/anagrams.tlv b/anagrams.tlv index 9a20f26..06e760e 100644 --- a/anagrams.tlv +++ b/anagrams.tlv @@ -228,6 +228,7 @@ - __teliva_timestamp: original main: >function main() + > Window:nodelay(true) > while true do > render(Window) > update(Window) @@ -236,7 +237,11 @@ - __teliva_timestamp: original update: >function update(window) - > local key = window:getch() + > local key + > while true do + > key = window:getch() + > if key then break end + > end > if key == curses.KEY_LEFT then > if cursor > 1 then > cursor = cursor-1 @@ -277,6 +282,11 @@ > for i, w in ipairs(results) do > window:addstr(w) > window:addstr(' ') + > local tmp = window:getch() + > if tmp then + > window:ungetch(tmp) + > break + > end > end > end > diff --git a/src/lcurses/window.c b/src/lcurses/window.c index 02fccb2..e3fcc70 100644 --- a/src/lcurses/window.c +++ b/src/lcurses/window.c @@ -1360,6 +1360,25 @@ Wgetch(lua_State *L) } +/*** +Put back a character obtained from @{getch} +@function ungetch +@int ch +@treturn OK or ERR +@see mvwgetch(3x) +@see getch +*/ +static int +Wungetch(lua_State *L) +{ + int ch = checkint(L, 2); + int result = ungetch(ch); + if (result == ERR) + return 0; + return pushintresult(result); +} + + /*** Call @{move} then @{getch} @function mvgetch @@ -1893,6 +1912,7 @@ static const luaL_Reg curses_window_fns[] = LCURSES_FUNC( Wtimeout ), LCURSES_FUNC( Wtouch ), LCURSES_FUNC( Wtouchline ), + LCURSES_FUNC( Wungetch ), LCURSES_FUNC( Wvline ), LCURSES_FUNC( Wwbkgd ), LCURSES_FUNC( Wwbkgdset ),