-
Notifications
You must be signed in to change notification settings - Fork 0
/
lch-prompt.nk
371 lines (271 loc) · 9.2 KB
/
lch-prompt.nk
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
needsCapability: console
"Some constants for you to tweak:"
33 33 33 rgb $: BG "console background"
245 245 245 rgb $: FG "console foreground"
0 $: HUE_MIN_DEG "minimum hue in animation"
360 $: HUE_MAX_DEG "maximum hue in animation"
30 $: LIGHTNESS_MIN_PERCENTS "minimum Lightness in animation"
88 $: LIGHTNESS_MAX_PERCENTS "maximum Lightness in animation"
60 $: FPS_CAP
2000 $: WAIT_AFTER_ACTIVITY_MS
1000 $: ANIMATION_DURATION_MS "duration of on + off"
console:on
console:truecolor
0 console:setTimeout
[ "Holds and controls the buffer content (a quote)."
'' $: content
"When someone wants to use `buffer` in place of a quote,
give them up-to-date `content`."
[ content ] @: __quote__
[ "( I -- ): deletes grapheme at Index in this buffer."
content empty? => [ drop ^ ]
"'hello world'"
"'hel|lo world'"
"'hel' 'lo world'"
"'he|l' 'lo world'"
"'he' 'l' 'lo world'"
"'he' 'lo world'"
"'helo world'"
content swap sliceQuoteAt dip: [ dup count 1 - sliceQuoteAt drop ] ~ =: content
] @: deleteAt
[ "( Q I -- ): inserts Quote at Index in this buffer."
content empty? => [ drop =: content ^ ]
content swap sliceQuoteAt rot swap ~ ~ =: content
] @: insertAt
[ "( -- L ): leaves Length of this buffer."
content count
] @: length
] obj $: buffer
[ "Holds an operates on the cursor position (a decimal)."
0 $: pos
"When someone wants to use `cursor` in place of a decimal,
give them up-to-date `pos`."
[ pos ] @: __decimal__
"-*-*-*-*- Movement etc. -*-*-*-*-"
[ "( Cp -- ): set Cursor position and trigger cursor
activity event on the cursor view."
=: pos cursorView.onCursorActivity
] @: setPosActively
[ "( -- Cp ): get Cursor position and trigger cursor
activity event on the cursor view."
cursorView.onCursorActivity pos
] @: getPosActively
[ "( -- ): move one character to the left."
pos 1 - 0 2max setPosActively
] @: goLeftOnce
[ "( -- ): move one character to the right."
pos 1 + buffer.length 2min setPosActively
] @: goRightOnce
[ "( -- ): moves cursor to the start of the buffer."
0 setPosActively
] @: goToStart
[ "( -- ): moves cursor to the end of the buffer."
buffer.length setPosActively
] @: goToEnd
[ "( -- B ): leaves Boolean for whether the cursor is at
the start of the buffer."
getPosActively zero?
] @: atStart?
[ "( -- B ): leaves Boolean for whether the cursor is at
the end of the buffer."
getPosActively buffer.length =
] @: atEnd?
] obj $: cursor
[ "Handles the presentation of the cursor on screen."
"-*-*-*-*- Animation control -*-*-*-*-"
"Animation progress in percents (0-1)."
0 $: animationProgressPercent
"Animation progress in milliseconds."
0 $: animationProgressMs
"Duration of the whole animation in milliseconds."
ANIMATION_DURATION_MS $: animationDurationMs
"How much to wait before the animation starts again.
Frame durations are subtracted from this entry's value
form until it becomes 0 <=, hence the use of 'sink'.
NOTE: it is not reset in any way automatically."
0 $: sinkBeforeAnimationMs
"Increases animation progress."
this -> + $: advanceProgressForth
"Decreases animation progress."
this -> - $: advanceProgressBack
"Opens the current progress advancer."
advanceProgressForth @: progressAdvancer
"Whether we're currently advancing progress forth."
true $: advancingProgressForth?
[ "Swaps progress advancers: if currently advancing
progress forth, will do that back, and vice versa."
advancingProgressForth? br:
becomeDarkened
becomeIlluminated
] @: swapProgressAdvancer
[ "Resets the animation to the start state."
0 =: animationProgressPercent
0 =: animationProgressMs
advanceProgressForth =: progressAdvancer
true =: advancingProgressForth?
] @: becomeIlluminated
[ "Resets the animation to the end state."
1 =: animationProgressPercent
animationDurationMs =: animationProgressMs
advanceProgressBack =: progressAdvancer
false =: advancingProgressForth?
] @: becomeDarkened
"-*-*-*-*- Colors -*-*-*-*-"
"Minimum/maximum lightness:"
LIGHTNESS_MIN_PERCENTS $: minLPercent
LIGHTNESS_MAX_PERCENTS $: maxLPercent
"Minimum/maximum hue:"
HUE_MIN_DEG $: minHDegrees
HUE_MAX_DEG $: maxHDegrees
[ "( P -- L ): leaves Lightness for lightness Percents (0-1)."
mapToRange: minLPercent maxLPercent trunc
] @: mapToLightnessRange
[ "( P -- H ): leaves Hue for hue Percents (0-1)."
mapToRange: minHDegrees maxHDegrees trunc
] @: mapToHueRange
[ "( Pb -- Cfb ): leaves a Color fetch block for Percentage
block, which should compute the current percentage for
the color of interest."
@: percentageComputer
[ "Fetches the current color using the percentage
computer block."
percentageComputer $: percents
percents mapToLightnessRange 39 percents mapToHueRange lch
]
] @: createColorComputer
"Background color percentage is simply the animation percentage."
[ animationProgressPercent ] createColorComputer @: bg
"Foreground color has percentage inverse of that of background."
[ 1 animationProgressPercent - ] createColorComputer @: fg
"-*-*-*-*- Events -*-*-*-*-"
[ "( Dt -- ): gets triggered from the main loop with Delta
time, on each animation cycle. Advances the animation."
drop "Uhmmm... perhaps use delta time ???"
sinkBeforeAnimationMs 0 > => [
sinkBeforeAnimationMs frameDurationMs - =: sinkBeforeAnimationMs
^
]
animationProgressMs frameDurationMs progressAdvancer =: animationProgressMs
"The animation cycle for blinking:"
animationProgressMs animationDurationMs / easeInOutCubic =: animationProgressPercent
"If before/after the boundary, swap advancers:"
animationProgressPercent 1 >=
animationProgressPercent 0 <=
or => swapProgressAdvancer
] @: onAnimationCycle
[ "Triggered on activity: cursor movement, position
inquiry, etc. Resets the blinking cycle progress."
becomeIlluminated
WAIT_AFTER_ACTIVITY_MS =: sinkBeforeAnimationMs
] @: onCursorActivity
"-*-*-*-*- Presentation -*-*-*-*-"
[ "( X Y -- ): draws the cursor offset by X, Y."
bg withEchoBg
fg withEchoFg
dip: [ cursor + ] console:change
dropEchoBg
dropEchoFg
] @: draw
] obj $: cursorView
[ "Handles the presentation of the buffer on screen."
[ drop ] @: onAnimationCycle
[ "( X Y -- ): draws the buffer offset by X, Y."
buffer -rot console:appendEcho
] @: draw
] obj $: bufferView
[ "( -- ): processes keyboard input."
console:hadKeyPressed? or: ^
"Ctrl-C => quit"
console:hadCtrlPressed?
console:getCharPressed 'c' = and => [
false =: running? ^
]
"Left/goRightOnce arrow => cursor, move goLeftOnce/goRightOnce"
console:hadLeftPressed? => [ cursor.goLeftOnce ^ ]
console:hadRightPressed? => [ cursor.goRightOnce ^ ]
"Backspace => buffer, rchop; cursor, move goLeftOnce once"
console:hadBackspacePressed? => [
cursor.atStart? => ^
cursor buffer.deleteAt
cursor.goLeftOnce ^
]
console:hadDeletePressed? => [
cursor.atEnd? => ^
cursor 1 + buffer.deleteAt ^
]
"Home => cursor, go to start"
console:hadHomePressed? => [ cursor.goToStart ^ ]
"End => cursor, go to end"
console:hadEndPressed? => [ cursor.goToEnd ^ ]
"Otherwise:"
console:hadCharPressed? => [
console:getCharPressed cursor buffer.insertAt
cursor.goRightOnce ^
]
] @: processKey
[ "( X Y -- ): draws everything offset by X, Y."
$: y $: x
x y bufferView.draw
x y cursorView.draw
] @: draw
[ "( Dt -- ): sends an onAnimationCycle to everyone
interested, with Delta time."
$: delta
delta cursorView.onAnimationCycle
delta bufferView.onAnimationCycle
] @: onAnimationCycle
[ "Updates the logic."
"There's no logic to be updated here..."
] @: update
"Copy-pasted from https://stackoverflow.com/a/25627639,
without much understanding (of course): {"
FPS_CAP $: fps
1000 fps / $: frameDurationMs
monotonic $: startMs
0 $: lagMs
[ "( Mlb -- ): runs Main loop block with delta time (which
it calculates) on top of an otherwise empty stack."
monotonic $: currentMs
currentMs startMs - $: elapsedMs
currentMs =: startMs
lagMs elapsedMs + =: lagMs
while: [ lagMs frameDurationMs >= ] [
update "Update the logic"
lagMs frameDurationMs - =: lagMs
]
lagMs frameDurationMs / enclose swap hydrate
] $: mainLoopController
"}"
"Whether the application is running."
true $: running?
[ "( B -- ): runs Block as the main loop block while the
application is `running?`. Opens Block with delta time
on top of an otherwise clean stack each loop iteration."
[ running? ] mainLoopController createDetachedLoop open
] @: asMainLoop
[ "Returns the console to normal gracefully."
dropEchoBg
dropEchoFg
console:off
] @: finalize
[
"Exit proprely upon death so if something is broken,
the console/error message doesn't get crazy and all
over the place."
finalize
die
] @: __died__
BG withEchoBg
FG withEchoFg
[ $: delta
console:readKey
processKey
console:clear
delta onAnimationCycle
2 1 draw
console:present
"Idk whether this multiplies (or even divides!) by zero
everything that happens above haha"
13 nap
] asMainLoop
finalize