Skip to content

Commit 37bd48e

Browse files
committed
get-key-blocking accepts optional interval/timeout
This is implemented by a shared function block-on in lanterna.common.
1 parent 09bf896 commit 37bd48e

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

src/lanterna/common.clj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,23 @@
1010
(.getCharacter k)
1111
kind))))
1212

13+
(defn block-on
14+
"Repeatedly apply func to args until a non-nil value is returned.
15+
16+
opts is a map optionally containing an :interval and a :timeout in msecs.
17+
:interval sets the interval between function applications (default 50).
18+
:timeout sets the maximum amount of time blocking will occur before
19+
returning nil.
20+
21+
"
22+
[func args {:keys [interval timeout] :as opts}]
23+
(let [{:keys [interval timeout]} (merge {:interval 50
24+
:timeout Double/POSITIVE_INFINITY}
25+
opts)]
26+
(loop [timeout timeout]
27+
(when (> timeout 0)
28+
(let [val (apply func args)]
29+
(if (nil? val)
30+
(do (Thread/sleep interval)
31+
(recur (- timeout interval)))
32+
val))))))

src/lanterna/screen.clj

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(ns lanterna.screen
22
(:import com.googlecode.lanterna.screen.Screen
33
com.googlecode.lanterna.terminal.Terminal)
4-
(:use [lanterna.common :only [parse-key]])
4+
(:use [lanterna.common :only [parse-key block-on]])
55
(:require [lanterna.constants :as c]
66
[lanterna.terminal :as t]))
77

@@ -275,17 +275,14 @@
275275
If the user has *not* pressed a key, this function will block, checking every
276276
50ms. If you want to return nil immediately, use get-key instead.
277277
278-
TODO: Make the interval configurable.
279-
TODO: Add a timeout option.
278+
Optionally accepts :interval and :timeout &rest keyword arguments.
279+
:interval sets the interval between checks.
280+
:timeout sets the maximum amount of time blocking will occur before
281+
returning nil.
280282
281283
"
282-
[^Screen screen]
283-
(let [k (get-key screen)]
284-
(if (nil? k)
285-
(do
286-
(Thread/sleep 50)
287-
(recur screen))
288-
k)))
284+
[^Screen screen & {:keys [interval timeout] :as opts}]
285+
(block-on get-key [screen] opts))
289286

290287

291288
(comment

src/lanterna/terminal.clj

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
com.googlecode.lanterna.terminal.swing.TerminalPalette
77
java.awt.GraphicsEnvironment
88
java.awt.Font)
9-
(:use [lanterna.common :only [parse-key]])
9+
(:use [lanterna.common :only [parse-key block-on]])
1010
(:require [lanterna.constants :as c]))
1111

1212

@@ -240,17 +240,14 @@
240240
If the user has *not* pressed a key, this function will block, checking every
241241
50ms. If you want to return nil immediately, use get-key instead.
242242
243-
TODO: Make the interval configurable.
244-
TODO: Add a timeout option.
243+
Optionally accepts :interval and :timeout &rest keyword arguments.
244+
:interval sets the interval between checks.
245+
:timeout sets the maximum amount of time blocking will occur before
246+
returning nil.
245247
246248
"
247-
[^Terminal terminal]
248-
(let [k (get-key terminal)]
249-
(if (nil? k)
250-
(do
251-
(Thread/sleep 50)
252-
(recur terminal))
253-
k)))
249+
[^Terminal terminal & {:keys [interval timeout] :as opts}]
250+
(block-on get-key [terminal] opts))
254251

255252

256253
(comment
@@ -263,6 +260,8 @@
263260
(start t)
264261
(set-fg-color t :yellow)
265262
(put-string t "Hello, world!")
263+
(get-key-blocking t :timeout 1000)
264+
(get-key-blocking t :interval 2000)
266265
(stop t)
267266

268267
)

0 commit comments

Comments
 (0)