File tree Expand file tree Collapse file tree 3 files changed +36
-20
lines changed Expand file tree Collapse file tree 3 files changed +36
-20
lines changed Original file line number Diff line number Diff line change 10
10
(.getCharacter k)
11
11
kind))))
12
12
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))))))
Original file line number Diff line number Diff line change 1
1
(ns lanterna.screen
2
2
(:import com.googlecode.lanterna.screen.Screen
3
3
com.googlecode.lanterna.terminal.Terminal)
4
- (:use [lanterna.common :only [parse-key]])
4
+ (:use [lanterna.common :only [parse-key block-on ]])
5
5
(:require [lanterna.constants :as c]
6
6
[lanterna.terminal :as t]))
7
7
275
275
If the user has *not* pressed a key, this function will block, checking every
276
276
50ms. If you want to return nil immediately, use get-key instead.
277
277
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.
280
282
281
283
"
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))
289
286
290
287
291
288
(comment
Original file line number Diff line number Diff line change 6
6
com.googlecode.lanterna.terminal.swing.TerminalPalette
7
7
java.awt.GraphicsEnvironment
8
8
java.awt.Font)
9
- (:use [lanterna.common :only [parse-key]])
9
+ (:use [lanterna.common :only [parse-key block-on ]])
10
10
(:require [lanterna.constants :as c]))
11
11
12
12
240
240
If the user has *not* pressed a key, this function will block, checking every
241
241
50ms. If you want to return nil immediately, use get-key instead.
242
242
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.
245
247
246
248
"
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))
254
251
255
252
256
253
(comment
263
260
(start t)
264
261
(set-fg-color t :yellow )
265
262
(put-string t " Hello, world!" )
263
+ (get-key-blocking t :timeout 1000 )
264
+ (get-key-blocking t :interval 2000 )
266
265
(stop t)
267
266
268
267
)
You can’t perform that action at this time.
0 commit comments