-
Notifications
You must be signed in to change notification settings - Fork 50
/
screen.clj
317 lines (233 loc) · 9.23 KB
/
screen.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
(ns lanterna.screen
(:import com.googlecode.lanterna.screen.Screen
com.googlecode.lanterna.terminal.Terminal)
(:use [lanterna.common :only [parse-key block-on]])
(:require [lanterna.constants :as c]
[lanterna.terminal :as t]))
(defn enumerate [s]
(map vector (iterate inc 0) s))
(defn add-resize-listener
"Create a listener that will call the supplied fn when the screen is resized.
The function must take two arguments: the new number of columns and the new
number of rows.
The listener itself will be returned. You don't need to do anything with it,
but you can use it to remove it later with remove-resize-listener.
"
[^Screen screen listener-fn]
(t/add-resize-listener (.getTerminal screen)
listener-fn))
(defn remove-resize-listener
"Remove a resize listener from the given screen."
[^Screen screen listener]
(t/remove-resize-listener (.getTerminal screen) listener))
(defn get-screen
"Get a screen object.
kind can be one of the following:
:auto - Try to guess the right type of screen based on OS, whether
there's a windowing environment, etc
:swing - Force a Swing-based screen.
:text - Force a console (i.e.: non-Swing) screen. Try to guess the
appropriate kind of console (UNIX/Cygwin) by the OS.
:unix - Force a UNIX console screen.
:cygwin - Force a Cygwin console screen.
Options can contain one or more of the following keys:
:cols - Width of the desired screen in characters (default 80).
:rows - Height of the desired screen in characters (default 24).
:charset - Charset of the desired screen. Can be any of
(keys lanterna.constants/charsets) (default :utf-8).
:resize-listener - A function to call when the screen is resized. This
function should take two parameters: the new number of
columns, and the new number of rows.
:font - Font to use. String or sequence of strings.
Use (lanterna.terminal/get-available-fonts) to see your options.
Will fall back to a basic monospaced font if none of the given
names are available.
:font-size - An int of the size of the font to use.
NOTE: The options are really just a suggestion!
The console screen will ignore rows and columns and will be the size of the
user's window.
The Swing screen will start out at this size but can be resized later by the
user, and will ignore the charset entirely.
God only know what Cygwin will do.
"
([] (get-screen :auto {}))
([kind] (get-screen kind {}))
([kind {:as opts
:keys [cols rows charset resize-listener]
:or {cols 80
rows 24
charset :utf-8
resize-listener nil}}]
(new Screen (t/get-terminal kind opts))))
(defn start
"Start the screen. Consider using in-screen instead.
This must be called before you do anything else to the screen.
"
[^Screen screen]
(.startScreen screen))
(defn stop
"Stop the screen. Consider using in-screen instead.
This should be called when you're done with the screen. Don't try to do
anything else to it after stopping it.
TODO: Figure out if it's safe to start, stop, and then restart a screen.
"
[^Screen screen]
(.stopScreen screen))
(defmacro in-screen
"Start the given screen, perform the body, and stop the screen afterward."
[screen & body]
`(let [screen# ~screen]
(start screen#)
(try ~@body
(finally (stop screen#)))))
(defn get-size
"Return the current size of the screen as [cols rows]."
[^Screen screen]
(let [size (.getTerminalSize screen)
cols (.getColumns size)
rows (.getRows size)]
[cols rows]))
(defn redraw
"Draw the screen.
This flushes any changes you've made to the actual user-facing terminal. You
need to call this for any of your changes to actually show up.
"
[^Screen screen]
(.refresh screen))
(defn move-cursor
"Move the cursor to a specific location on the screen.
This won't affect where text is printed when you use put-string -- the
coordinates passed to put-string determine that.
This is only used to move the cursor, presumably right before a redraw so it
appears in a specific place.
"
([^Screen screen x y]
(.setCursorPosition screen x y))
([^Screen screen [x y]]
(.setCursorPosition screen x y)))
(defn get-cursor
"Return the cursor position as [col row]."
[^Screen screen]
(let [pos (.getCursorPosition screen)
col (.getColumn pos)
row (.getRow pos)]
[col row]))
(defn put-string
"Put a string on the screen buffer, ready to be drawn at the next redraw.
x and y are the column and row to start the string.
s is the actual string to draw.
Options can contain any of the following:
:fg - Foreground color.
Can be any one of (keys lanterna.constants/colors).
(default :default)
:bg - Background color.
Can be any one of (keys lanterna.constants/colors).
(default :default)
:styles - Styles to apply to the text.
Can be a set containing some/none/all of (keys lanterna.constants/styles).
(default #{})
"
([^Screen screen x y s] (put-string screen x y s {}))
([^Screen screen x y ^String s {:as opts
:keys [fg bg styles]
:or {fg :default
bg :default
styles #{}}}]
(let [styles ^clojure.lang.PersistentHashSet (set (map c/styles styles))
x (int x)
y (int y)
fg ^com.googlecode.lanterna.terminal.Terminal$Color (c/colors fg)
bg ^com.googlecode.lanterna.terminal.Terminal$Color (c/colors bg)]
(.putString screen x y s fg bg styles))))
(defn put-sheet
"EXPERIMENTAL! Turn back now!
Draw a sheet to the screen (buffered, of course).
A sheet is a two-dimentional sequence of things to print to the screen. It
will be printed with its upper-left corner at the given x and y coordinates.
Sheets can take several forms. The simplest sheet is a vector of strings:
(put-sheet scr 2 0 [\"foo\" \"bar\" \"hello!\"])
This would print something like
0123456789
0 foo
1 bar
2 hello!
As you can see, the rows of a sheet do not need to all be the same size.
Shorter rows will *not* be padded in any way.
Rows can also be sequences themselves, of characters or strings:
(put-sheet scr 5 0 [[\\s \\p \\a \\m] [\"e\" \"g\" \"g\" \"s\"]])
0123456789
0 spam
1 eggs
Finally, instead of single characters of strings, you can pass a vector of a
[char-or-string options-map], like so:
(put-sheet scr 1 0 [[[\\r {:fg :red}] [\\g {:fg :green}]]
[[\\b {:fg :blue}]]])
0123456789
0 rg
1 b
And the letters would be colored appropriately.
Finally, you can mix and match any and all of these within a single sheet or
row:
(put-sheet scr 2 0 [\"foo\"
[\"b\" \\a [\\r {:bg :yellow :fg :black}])
"
[screen x y sheet]
(letfn [(put-item [c r item]
(cond
(string? item) (put-string screen c r item)
(char? item) (put-string screen c r (str item))
(vector? item) (let [[i opts] item]
(if (char? i)
(put-string screen c r (str i) opts)
(put-string screen c r i opts)))
:else nil ; TODO: die loudly
))
(put-row [r row]
(doseq [[c item] (enumerate row)]
(put-item (+ x c) r item)))]
(doseq [[i row] (enumerate sheet)]
(if (string? row)
(put-string screen x (+ y i) row)
(put-row (+ y i) row)))))
(defn clear
"Clear the screen.
Note that this is buffered like everything else, so you'll need to redraw
the screen to see the effect.
"
[^Screen screen]
(.clear screen))
(defn get-key
"Get the next keypress from the user, or nil if none are buffered.
If the user has pressed a key, that key will be returned (and popped off the
buffer of input).
If the user has *not* pressed a key, nil will be returned immediately. If you
want to wait for user input, use get-key-blocking instead.
"
[^Screen screen]
(parse-key (.readInput screen)))
(defn get-key-blocking
"Get the next keypress from the user.
If the user has pressed a key, that key will be returned (and popped off the
buffer of input).
If the user has *not* pressed a key, this function will block, checking every
50ms. If you want to return nil immediately, use get-key instead.
Options can include any of the following keys:
:interval - sets the interval between checks
:timeout - sets the maximum amount of time blocking will occur before
returning nil
"
([^Screen screen] (get-key-blocking screen {}))
([^Screen screen {:keys [interval timeout] :as opts}]
(block-on get-key [screen] opts)))
(comment
(def s (get-screen))
(start s)
(put-sheet s 5 5 ["foo" "bar" "hello"])
(put-sheet s 5 9 [[\f \o \o] ["b" "a" "r"] "hello"])
(let [r [\r {:bg :red :fg :white}]
g [\g {:bg :green :fg :black}]
b [\b {:bg :blue :fg :white}]]
(put-sheet s 5 13 [[r r r] [g g g] [b b b]]))
(redraw s)
(stop s)
)