about summary refs log tree commit diff stats
path: root/browse-slack/environment.mu
blob: d3e2a804784b8341672beb5a9294552ecb12b19e (plain) (blame)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
type environment {
  tabs: (handle array tab)
  current-tab-index: int  # index into tabs
}

type tab {
  type: int
  # type 0: everything
  # type 1: items in a channel
  root-index: int  # into channels; only for type 1
  item-index: int  # what item in the corresponding list we start rendering
                   # the current page at
}

# static buffer sizes in this file:
#   main-panel-hor            # in characters
#   item-padding-hor          # in pixels
#   item-padding-ver          # in characters
#   avatar-side               # in pixels
#   avatar-space-hor          # in characters
#   avatar-space-ver          # in characters
#   search-position-x         # in characters
#   search-space-ver          # in characters
#   author-name-padding-ver   # in characters
#   post-right-coord          # in characters
#   channel-offset-x          # in characters
#   menu-space-ver            # in characters

fn initialize-environment _self: (addr environment), _items: (addr item-list) {
  var self/esi: (addr environment) <- copy _self
  var items/eax: (addr item-list) <- copy _items
  var items-data-first-free-a/eax: (addr int) <- get items, data-first-free
  var final-item/edx: int <- copy *items-data-first-free-a
  final-item <- decrement
  var tabs-ah/ecx: (addr handle array tab) <- get self, tabs
  populate tabs-ah, 0x10
  # current-tab-index implicitly set to 0
  var tabs/eax: (addr array tab) <- lookup *tabs-ah
  var first-tab/eax: (addr tab) <- index tabs, 0/current-tab-index
  var dest/edi: (addr int) <- get first-tab, item-index
  copy-to *dest, final-item
}

### Render

fn render-environment screen: (addr screen), env: (addr environment), users: (addr array user), channels: (addr array channel), items: (addr item-list) {
  clear-screen screen
  render-search-input screen, env
  render-channels screen, env, channels
  render-item-list screen, env, items, channels, users
  render-menu screen
}

fn render-channels screen: (addr screen), env: (addr environment), _channels: (addr array channel) {
  var channels/esi: (addr array channel) <- copy _channels
  var y/ebx: int <- copy 2/search-space-ver
  y <- add 1/item-padding-ver
  var i/ecx: int <- copy 0
  var max/edx: int <- length channels
  {
    compare i, max
    break-if->=
    var offset/eax: (offset channel) <- compute-offset channels, i
    var curr/eax: (addr channel) <- index channels, offset
    var name-ah/eax: (addr handle array byte) <- get curr, name
    var name/eax: (addr array byte) <- lookup *name-ah
    compare name, 0
    break-if-=
    set-cursor-position screen, 2/x y
    draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, "#", 7/grey 0/black
    draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, name, 7/grey 0/black
    y <- add 2/channel-padding
    i <- increment
    loop
  }
}

fn render-item-list screen: (addr screen), _env: (addr environment), items: (addr item-list), channels: (addr array channel), users: (addr array user) {
  var env/esi: (addr environment) <- copy _env
  var tmp-width/eax: int <- copy 0
  var tmp-height/ecx: int <- copy 0
  tmp-width, tmp-height <- screen-size screen
  var screen-width: int
  copy-to screen-width, tmp-width
  var screen-height: int
  copy-to screen-height, tmp-height
  #
  var tabs-ah/eax: (addr handle array tab) <- get env, tabs
  var _tabs/eax: (addr array tab) <- lookup *tabs-ah
  var tabs/edx: (addr array tab) <- copy _tabs
  var current-tab-index-a/eax: (addr int) <- get env, current-tab-index
  var current-tab-index/eax: int <- copy *current-tab-index-a
  var current-tab-offset/eax: (offset tab) <- compute-offset tabs, current-tab-index
  var current-tab/edx: (addr tab) <- index tabs, current-tab-offset
  render-tab screen, current-tab, items, channels, users, screen-height
  var top/eax: int <- copy screen-height
  top <- subtract 2/menu-space-ver
  clear-rect screen, 0 top, screen-width screen-height, 0/bg
}

fn render-tab screen: (addr screen), _current-tab: (addr tab), items: (addr item-list), channels: (addr array channel), users: (addr array user), screen-height: int {
  var current-tab/esi: (addr tab) <- copy _current-tab
  var current-tab-type/eax: (addr int) <- get current-tab, type
  compare *current-tab-type, 0/all-items
  {
    break-if-!=
    render-all-items screen, current-tab, items, users, screen-height
    return
  }
  compare *current-tab-type, 1/channel
  {
    break-if-!=
    render-channel-tab screen, current-tab, items, channels, users, screen-height
    return
  }
}

fn render-all-items screen: (addr screen), _current-tab: (addr tab), _items: (addr item-list), users: (addr array user), screen-height: int {
  var current-tab/esi: (addr tab) <- copy _current-tab
  var items/edi: (addr item-list) <- copy _items
  var newest-item/eax: (addr int) <- get current-tab, item-index
  var i/ebx: int <- copy *newest-item
  var items-data-first-free-addr/eax: (addr int) <- get items, data-first-free
  render-progress screen, i, *items-data-first-free-addr
  var items-data-ah/eax: (addr handle array item) <- get items, data
  var _items-data/eax: (addr array item) <- lookup *items-data-ah
  var items-data/edi: (addr array item) <- copy _items-data
  var y/ecx: int <- copy 2/search-space-ver
  y <- add 1/item-padding-ver
  {
    compare i, 0
    break-if-<
    compare y, screen-height
    break-if->=
    var offset/eax: (offset item) <- compute-offset items-data, i
    var curr-item/eax: (addr item) <- index items-data, offset
    y <- render-item screen, curr-item, users, y, screen-height
    i <- decrement
    loop
  }
}

fn render-channel-tab screen: (addr screen), _current-tab: (addr tab), _items: (addr item-list), _channels: (addr array channel), users: (addr array user), screen-height: int {
  var current-tab/esi: (addr tab) <- copy _current-tab
  var items/edi: (addr item-list) <- copy _items
  var channels/ebx: (addr array channel) <- copy _channels
  var channel-index-addr/eax: (addr int) <- get current-tab, root-index
  var channel-index/eax: int <- copy *channel-index-addr
  var channel-offset/eax: (offset channel) <- compute-offset channels, channel-index
  var current-channel/ecx: (addr channel) <- index channels, channel-offset
  var current-channel-posts-ah/eax: (addr handle array int) <- get current-channel, posts
  var _current-channel-posts/eax: (addr array int) <- lookup *current-channel-posts-ah
  var current-channel-posts/edx: (addr array int) <- copy _current-channel-posts
  var current-channel-first-channel-item-addr/eax: (addr int) <- get current-tab, item-index
  var i/ebx: int <- copy *current-channel-first-channel-item-addr
  var current-channel-posts-first-free-addr/eax: (addr int) <- get current-channel, posts-first-free
  set-cursor-position 0/screen, 0x68/x 0/y
  draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, "channel", 7/fg 0/bg
  render-progress screen, i, *current-channel-posts-first-free-addr
  var items-data-ah/eax: (addr handle array item) <- get items, data
  var _items-data/eax: (addr array item) <- lookup *items-data-ah
  var items-data/edi: (addr array item) <- copy _items-data
  var y/ecx: int <- copy 2/search-space-ver
  y <- add 1/item-padding-ver
  {
    compare i, 0
    break-if-<
    compare y, screen-height
    break-if->=
    var item-index-addr/eax: (addr int) <- index current-channel-posts, i
    var item-index/eax: int <- copy *item-index-addr
    var item-offset/eax: (offset item) <- compute-offset items-data, item-index
    var curr-item/eax: (addr item) <- index items-data, item-offset
    y <- render-item screen, curr-item, users, y, screen-height
    i <- decrement
    loop
  }
}

# side-effect: mutates cursor position
fn render-progress screen: (addr screen), curr: int, max: int {
  set-cursor-position 0/screen, 0x70/x 0/y
  var top-index/eax: int <- copy max
  top-index <- subtract curr  # happy accident: 1-based
  draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen screen, top-index, 7/fg 0/bg
  draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, "/", 7/fg 0/bg
  draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen screen, max, 7/fg 0/bg
}

fn render-search-input screen: (addr screen), env: (addr environment) {
  set-cursor-position 0/screen, 0x22/x=search-position-x 1/y
  draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, "search ", 7/fg 0/bg
  draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, "________________________________", 0xf/fg 0/bg
}

fn render-menu screen: (addr screen) {
  var width/eax: int <- copy 0
  var y/ecx: int <- copy 0
  width, y <- screen-size screen
  y <- decrement
  set-cursor-position screen, 2/x, y
  draw-text-rightward-from-cursor screen, " / ", width, 0/fg 0xf/bg
  draw-text-rightward-from-cursor screen, " search  ", width, 0xf/fg, 0/bg
  draw-text-rightward-from-cursor screen, " ^b ", width, 0/fg 0xf/bg
  draw-text-rightward-from-cursor screen, " << previous page  ", width, 0xf/fg, 0/bg
  draw-text-rightward-from-cursor screen, " ^p ", width, 0/fg 0xf/bg
  draw-text-rightward-from-cursor screen, " < previous item  ", width, 0xf/fg, 0/bg
  draw-text-rightward-from-cursor screen, " ^n ", width, 0/fg 0xf/bg
  draw-text-rightward-from-cursor screen, " next item >  ", width, 0xf/fg, 0/bg
  draw-text-rightward-from-cursor screen, " ^f ", width, 0/fg 0xf/bg
  draw-text-rightward-from-cursor screen, " next page >>  ", width, 0xf/fg, 0/bg
}

fn render-item screen: (addr screen), _item: (addr item), _users: (addr array user), y: int, screen-height: int -> _/ecx: int {
  var item/esi: (addr item) <- copy _item
  var users/edi: (addr array user) <- copy _users
  var author-index-addr/ecx: (addr int) <- get item, by
  var author-index/ecx: int <- copy *author-index-addr
  var author-offset/ecx: (offset user) <- compute-offset users, author-index
  var author/ecx: (addr user) <- index users, author-offset
  # author avatar
  var author-avatar-ah/eax: (addr handle image) <- get author, avatar
  var _author-avatar/eax: (addr image) <- lookup *author-avatar-ah
  var author-avatar/ebx: (addr image) <- copy _author-avatar
  {
    compare author-avatar, 0
    break-if-=
    var y/edx: int <- copy y
    y <- shift-left 4/log2font-height
    var x/eax: int <- copy 0x20/main-panel-hor
    x <- shift-left 3/log2font-width
    x <- add 0x18/item-padding-hor
    render-image screen, author-avatar, x, y, 0x50/avatar-side, 0x50/avatar-side
  }
  # channel
  var channel-name-ah/eax: (addr handle array byte) <- get item, channel
  var channel-name/eax: (addr array byte) <- lookup *channel-name-ah
  {
    var x/eax: int <- copy 0x20/main-panel-hor
    x <- add 0x40/channel-offset-x
    set-cursor-position screen, x y
  }
  draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, "#", 7/grey 0/black
  draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, channel-name, 7/grey 0/black
  # author name
  var author-real-name-ah/eax: (addr handle array byte) <- get author, real-name
  var author-real-name/eax: (addr array byte) <- lookup *author-real-name-ah
  {
    var x/ecx: int <- copy 0x20/main-panel-hor
    x <- add 0x10/avatar-space-hor
    set-cursor-position screen, x y
    draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, author-real-name, 0xf/white 0/black
  }
  increment y
  # text
  var text-ah/eax: (addr handle array byte) <- get item, text
  var _text/eax: (addr array byte) <- lookup *text-ah
  var text/edx: (addr array byte) <- copy _text
  var text-y/eax: int <- render-slack-message screen, text, y, screen-height
  # flush
  add-to y, 6/avatar-space-ver
  compare y, text-y
  {
    break-if-<
    return y
  }
  return text-y
}

fn render-slack-message screen: (addr screen), text: (addr array byte), ymin: int, ymax: int -> _/eax: int {
  var x/eax: int <- copy 0x20/main-panel-hor
  x <- add 0x10/avatar-space-hor
  var y/ecx: int <- copy ymin
  y <- add 1/author-name-padding-ver
  x, y <- draw-json-text-wrapping-right-then-down screen, text, x y, 0x70/xmax=post-right-coord ymax, x y, 7/fg 0/bg
  y <- add 2/item-padding-ver
  return y
}

# draw text in the rectangle from (xmin, ymin) to (xmax, ymax), starting from (x, y), wrapping as necessary
# return the next (x, y) coordinate in raster order where drawing stopped
# that way the caller can draw more if given the same min and max bounding-box.
# if there isn't enough space, truncate
fn draw-json-text-wrapping-right-then-down screen: (addr screen), _text: (addr array byte), xmin: int, ymin: int, xmax: int, ymax: int, _x: int, _y: int, color: int, background-color: int -> _/eax: int, _/ecx: int {
  var stream-storage: (stream byte 0x4000/print-buffer-size)
  var stream/edi: (addr stream byte) <- address stream-storage
  var text/esi: (addr array byte) <- copy _text
  var len/eax: int <- length text
  compare len, 0x4000/print-buffer-size
  {
    break-if-<
    write stream, "ERROR: stream too small in draw-text-wrapping-right-then-down"
  }
  compare len, 0x4000/print-buffer-size
  {
    break-if->=
    write stream, text
  }
  var x/eax: int <- copy _x
  var y/ecx: int <- copy _y
  x, y <- draw-json-stream-wrapping-right-then-down screen, stream, xmin, ymin, xmax, ymax, x, y, color, background-color
  return x, y
}

# draw a stream in the rectangle from (xmin, ymin) to (xmax, ymax), starting from (x, y), wrapping as necessary
# return the next (x, y) coordinate in raster order where drawing stopped
# that way the caller can draw more if given the same min and max bounding-box.
# if there isn't enough space, truncate
fn draw-json-stream-wrapping-right-then-down screen: (addr screen), stream: (addr stream byte), xmin: int, ymin: int, xmax: int, ymax: int, x: int, y: int, color: int, background-color: int -> _/eax: int, _/ecx: int {
  var xcurr/eax: int <- copy x
  var ycurr/ecx: int <- copy y
  {
    var g/ebx: grapheme <- read-json-grapheme stream
    compare g, 0xffffffff/end-of-file
    break-if-=
    $draw-json-stream-wrapping-right-then-down:render-grapheme: {
      compare g, 0x5c/backslash
      {
        break-if-!=
        xcurr, ycurr <- render-json-escaped-grapheme screen, stream, xmin, ymin, xmax, ymax, xcurr, ycurr, color, background-color
        break $draw-json-stream-wrapping-right-then-down:render-grapheme
      }
      xcurr, ycurr <- render-grapheme screen, g, xmin, ymin, xmax, ymax, xcurr, ycurr, color, background-color
    }
    loop
  }
  set-cursor-position screen, xcurr, ycurr
  return xcurr, ycurr
}

# just return a different register
fn read-json-grapheme stream: (addr stream byte) -> _/ebx: grapheme {
  var result/eax: grapheme <- read-grapheme stream
  return result
}

# '\' encountered
# https://www.json.org/json-en.html
fn render-json-escaped-grapheme screen: (addr screen), stream: (addr stream byte), xmin: int, ymin: int, xmax: int, ymax: int, xcurr: int, ycurr: int, color: int, background-color: int -> _/eax: int, _/ecx: int {
  var g/ebx: grapheme <- read-json-grapheme stream
  compare g, 0xffffffff/end-of-file
  {
    break-if-!=
    return xcurr, ycurr
  }
  # \n = newline
  compare g, 0x6e/n
  var x/eax: int <- copy xcurr
  {
    break-if-!=
    increment ycurr
    return xmin, ycurr
  }
  # ignore \t \r \f \b
  {
    compare g, 0x74/t
    break-if-!=
    return xcurr, ycurr
  }
  {
    compare g, 0x72/r
    break-if-!=
    return xcurr, ycurr
  }
  {
    compare g, 0x66/f
    break-if-!=
    return xcurr, ycurr
  }
  {
    compare g, 0x62/b
    break-if-!=
    return xcurr, ycurr
  }
  var y/ecx: int <- copy 0
  # \u = Unicode
  {
    compare g, 0x75/u
    break-if-!=
    x, y <- render-json-escaped-unicode-grapheme screen, stream, xmin, ymin, xmax, ymax, xcurr, ycurr, color, background-color
    return x, y
  }
  # most characters escape to themselves
  x, y <- render-grapheme screen, g, xmin, ymin, xmax, ymax, xcurr, ycurr, color, background-color
  return x, y
}

# '\u' encountered
fn render-json-escaped-unicode-grapheme screen: (addr screen), stream: (addr stream byte), xmin: int, ymin: int, xmax: int, ymax: int, xcurr: int, ycurr: int, color: int, background-color: int -> _/eax: int, _/ecx: int {
  var ustream-storage: (stream byte 4)
  var ustream/esi: (addr stream byte) <- address ustream-storage
  # slurp 4 bytes exactly
  var b/eax: byte <- read-byte stream
  var b-int/eax: int <- copy b
  append-byte ustream, b-int
  var b/eax: byte <- read-byte stream
  var b-int/eax: int <- copy b
  append-byte ustream, b-int
  var b/eax: byte <- read-byte stream
  var b-int/eax: int <- copy b
  append-byte ustream, b-int
  var b/eax: byte <- read-byte stream
  var b-int/eax: int <- copy b
  append-byte ustream, b-int
  # \u2013 = -
  {
    var endash?/eax: boolean <- stream-data-equal? ustream, "2013"
    compare endash?, 0/false
    break-if-=
    var x/eax: int <- copy 0
    var y/ecx: int <- copy 0
    x, y <- render-grapheme screen, 0x2d/dash, xmin, ymin, xmax, ymax, xcurr, ycurr, color, background-color
    return x, y
  }
  # \u2014 = -
  {
    var emdash?/eax: boolean <- stream-data-equal? ustream, "2014"
    compare emdash?, 0/false
    break-if-=
    var x/eax: int <- copy 0
    var y/ecx: int <- copy 0
    x, y <- render-grapheme screen, 0x2d/dash, xmin, ymin, xmax, ymax, xcurr, ycurr, color, background-color
    return x, y
  }
  # \u2018 = '
  {
    var left-quote?/eax: boolean <- stream-data-equal? ustream, "2018"
    compare left-quote?, 0/false
    break-if-=
    var x/eax: int <- copy 0
    var y/ecx: int <- copy 0
    x, y <- render-grapheme screen, 0x27/quote, xmin, ymin, xmax, ymax, xcurr, ycurr, color, background-color
    return x, y
  }
  # \u2019 = '
  {
    var right-quote?/eax: boolean <- stream-data-equal? ustream, "2019"
    compare right-quote?, 0/false
    break-if-=
    var x/eax: int <- copy 0
    var y/ecx: int <- copy 0
    x, y <- render-grapheme screen, 0x27/quote, xmin, ymin, xmax, ymax, xcurr, ycurr, color, background-color
    return x, y
  }
  # \u201c = "
  {
    var left-dquote?/eax: boolean <- stream-data-equal? ustream, "201c"
    compare left-dquote?, 0/false
    break-if-=
    var x/eax: int <- copy 0
    var y/ecx: int <- copy 0
    x, y <- render-grapheme screen, 0x22/dquote, xmin, ymin, xmax, ymax, xcurr, ycurr, color, background-color
    return x, y
  }
  # \u201d = "
  {
    var right-dquote?/eax: boolean <- stream-data-equal? ustream, "201d"
    compare right-dquote?, 0/false
    break-if-=
    var x/eax: int <- copy 0
    var y/ecx: int <- copy 0
    x, y <- render-grapheme screen, 0x22/dquote, xmin, ymin, xmax, ymax, xcurr, ycurr, color, background-color
    return x, y
  }
  # \u2022 = *
  {
    var bullet?/eax: boolean <- stream-data-equal? ustream, "2022"
    compare bullet?, 0/false
    break-if-=
    var x/eax: int <- copy 0
    var y/ecx: int <- copy 0
    x, y <- render-grapheme screen, 0x2a/asterisk, xmin, ymin, xmax, ymax, xcurr, ycurr, color, background-color
    return x, y
  }
  # \u2026 = ...
  {
    var ellipses?/eax: boolean <- stream-data-equal? ustream, "2026"
    compare ellipses?, 0/false
    break-if-=
    var x/eax: int <- copy 0
    var y/ecx: int <- copy 0
    x, y <- draw-text-wrapping-right-then-down screen, "...", xmin, ymin, xmax, ymax, xcurr, ycurr, color, background-color
    return x, y
  }
  # TODO: rest of Unicode
  var x/eax: int <- copy 0
  var y/ecx: int <- copy 0
  x, y <- draw-stream-wrapping-right-then-down screen, ustream, xmin, ymin, xmax, ymax, xcurr, ycurr, color, background-color
  return x, y
}

### Edit

fn update-environment env: (addr environment), key: byte, users: (addr array user), channels: (addr array channel), items: (addr item-list) {
  {
    compare key, 0xe/ctrl-n
    break-if-!=
    next-item env, users, channels, items
    return
  }
  {
    compare key, 0x10/ctrl-p
    break-if-!=
    previous-item env, users, channels, items
    return
  }
  {
    compare key, 6/ctrl-f
    break-if-!=
    page-down env, users, channels, items
    return
  }
  {
    compare key, 2/ctrl-b
    break-if-!=
    page-up env, users, channels, items
    return
  }
}

fn next-item _env: (addr environment), users: (addr array user), channels: (addr array channel), _items: (addr item-list) {
  var env/edi: (addr environment) <- copy _env
  var tabs-ah/eax: (addr handle array tab) <- get env, tabs
  var _tabs/eax: (addr array tab) <- lookup *tabs-ah
  var tabs/edx: (addr array tab) <- copy _tabs
  var current-tab-index-a/eax: (addr int) <- get env, current-tab-index
  var current-tab-index/eax: int <- copy *current-tab-index-a
  var current-tab-offset/eax: (offset tab) <- compute-offset tabs, current-tab-index
  var current-tab/edx: (addr tab) <- index tabs, current-tab-offset
  var dest/eax: (addr int) <- get current-tab, item-index
  compare *dest, 0
  break-if-<=
  decrement *dest
}

fn previous-item _env: (addr environment), users: (addr array user), channels: (addr array channel), _items: (addr item-list) {
  var env/edi: (addr environment) <- copy _env
  var items/esi: (addr item-list) <- copy _items
  var items-data-first-free-a/ecx: (addr int) <- get items, data-first-free
  var final-item-index/ecx: int <- copy *items-data-first-free-a
  final-item-index <- decrement
  var tabs-ah/eax: (addr handle array tab) <- get env, tabs
  var _tabs/eax: (addr array tab) <- lookup *tabs-ah
  var tabs/edx: (addr array tab) <- copy _tabs
  var current-tab-index-a/eax: (addr int) <- get env, current-tab-index
  var current-tab-index/eax: int <- copy *current-tab-index-a
  var current-tab-offset/eax: (offset tab) <- compute-offset tabs, current-tab-index
  var current-tab/edx: (addr tab) <- index tabs, current-tab-offset
  var dest/eax: (addr int) <- get current-tab, item-index
  compare *dest, final-item-index
  break-if->=
  increment *dest
}

fn page-down _env: (addr environment), users: (addr array user), channels: (addr array channel), _items: (addr item-list) {
  var env/edi: (addr environment) <- copy _env
  var items/esi: (addr item-list) <- copy _items
  var items-data-ah/eax: (addr handle array item) <- get items, data
  var _items-data/eax: (addr array item) <- lookup *items-data-ah
  var items-data/ebx: (addr array item) <- copy _items-data
  var tabs-ah/eax: (addr handle array tab) <- get env, tabs
  var _tabs/eax: (addr array tab) <- lookup *tabs-ah
  var tabs/ecx: (addr array tab) <- copy _tabs
  var current-tab-index-a/eax: (addr int) <- get env, current-tab-index
  var current-tab-index/eax: int <- copy *current-tab-index-a
  var current-tab-offset/eax: (offset tab) <- compute-offset tabs, current-tab-index
  var current-tab/eax: (addr tab) <- index tabs, current-tab-offset
  var current-tab-item-index-addr/edi: (addr int) <- get current-tab, item-index
  var new-item-index/ecx: int <- copy *current-tab-item-index-addr
  var y/edx: int <- copy 2
  {
    compare new-item-index, 0
    break-if-<
    compare y, 0x28/screen-height-minus-menu
    break-if->=
    var offset/eax: (offset item) <- compute-offset items-data, new-item-index
    var item/eax: (addr item) <- index items-data, offset
    var item-text-ah/eax: (addr handle array byte) <- get item, text
    var item-text/eax: (addr array byte) <- lookup *item-text-ah
    var h/eax: int <- estimate-height item-text
    set-cursor-position 0/screen, 0 0
    draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, h, 4/fg 0/bg
    y <- add h
    new-item-index <- decrement
    loop
  }
  new-item-index <- increment
  {
    # HACK: make sure we make forward progress even if a single post takes up
    # the whole screen.
    # We can't see the rest of that single post at the moment. But at least we
    # can go past it.
    compare new-item-index, *current-tab-item-index-addr
    break-if-!=
    # Don't make "forward progress" past post 0.
    compare new-item-index, 0
    break-if-=
    new-item-index <- decrement
  }
  copy-to *current-tab-item-index-addr, new-item-index
}

fn page-up _env: (addr environment), users: (addr array user), channels: (addr array channel), _items: (addr item-list) {
  var env/edi: (addr environment) <- copy _env
  var items/esi: (addr item-list) <- copy _items
  var items-data-ah/eax: (addr handle array item) <- get items, data
  var _items-data/eax: (addr array item) <- lookup *items-data-ah
  var items-data/ebx: (addr array item) <- copy _items-data
  var items-data-first-free-a/eax: (addr int) <- get items, data-first-free
  var final-item-index/esi: int <- copy *items-data-first-free-a
  final-item-index <- decrement
  var tabs-ah/eax: (addr handle array tab) <- get env, tabs
  var _tabs/eax: (addr array tab) <- lookup *tabs-ah
  var tabs/ecx: (addr array tab) <- copy _tabs
  var current-tab-index-a/eax: (addr int) <- get env, current-tab-index
  var current-tab-index/eax: int <- copy *current-tab-index-a
  var current-tab-offset/eax: (offset tab) <- compute-offset tabs, current-tab-index
  var current-tab/eax: (addr tab) <- index tabs, current-tab-offset
  var current-tab-item-index-addr/edi: (addr int) <- get current-tab, item-index
  var new-item-index/ecx: int <- copy *current-tab-item-index-addr
  var y/edx: int <- copy 2
  {
    compare new-item-index, final-item-index
    break-if->
    compare y, 0x28/screen-height-minus-menu
    break-if->=
    var offset/eax: (offset item) <- compute-offset items-data, new-item-index
    var item/eax: (addr item) <- index items-data, offset
    var item-text-ah/eax: (addr handle array byte) <- get item, text
    var item-text/eax: (addr array byte) <- lookup *item-text-ah
    var h/eax: int <- estimate-height item-text
    set-cursor-position 0/screen, 0 0
    draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, h, 4/fg 0/bg
    y <- add h
    new-item-index <- increment
    loop
  }
  new-item-index <- decrement
  copy-to *current-tab-item-index-addr, new-item-index
}

# keep sync'd with render-item
fn estimate-height _message-text: (addr array byte) -> _/eax: int {
  var message-text/esi: (addr array byte) <- copy _message-text
  var result/eax: int <- length message-text
  var remainder/edx: int <- copy 0
  result, remainder <- integer-divide result, 0x40/post-width
  compare remainder, 0
  {
    break-if-=
    result <- increment
  }
  result <- add 2/item-padding-ver
  compare result, 6/avatar-space-ver
  {
    break-if->
    return 6/avatar-space-ver
  }
  return result
}