about summary refs log tree commit diff stats
path: root/html/084console.mu.html
blob: 3af59b06575d3e1b31253f7f2f172e7673396a9a (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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 084console.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.muData { color: #ffff00; }
.muControl { color: #c0a020; }
.Delimiter { color: #800080; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #c00000; }
.muRecipe { color: #ff8700; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># Wrappers around interaction primitives that take a potentially fake object</span>
<span class="Comment"># and are thus easier to test.</span>

<span class="muData">exclusive-container</span> event [
  text:char
  keycode:num  <span class="Comment"># keys on keyboard without a unicode representation</span>
  touch:touch-event  <span class="Comment"># mouse, track ball, etc.</span>
  resize:resize-event
  <span class="Comment"># update the assume-console handler if you add more variants</span>
]

<span class="muData">container</span> touch-event [
  type:num
  row:num
  column:num
]

<span class="muData">container</span> resize-event [
  width:num
  height:num
]

<span class="muData">container</span> console [
  current-event-index:num
  events:&amp;:@:event
]

<span class="muRecipe">def</span> new-fake-console events:&amp;:@:event<span class="muRecipe"> -&gt; </span>result:&amp;:console [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  result:&amp;:console<span class="Special"> &lt;- </span>new <span class="Constant">console:type</span>
  *result<span class="Special"> &lt;- </span>put *result, <span class="Constant">events:offset</span>, events
]

<span class="muRecipe">def</span> read-event console:&amp;:console<span class="muRecipe"> -&gt; </span>result:event, console:&amp;:console, found?:bool, quit?:bool [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> console
    current-event-index:num<span class="Special"> &lt;- </span>get *console, <span class="Constant">current-event-index:offset</span>
    buf:&amp;:@:event<span class="Special"> &lt;- </span>get *console, <span class="Constant">events:offset</span>
    <span class="Delimiter">{</span>
      max:num<span class="Special"> &lt;- </span>length *buf
      done?:bool<span class="Special"> &lt;- </span>greater-or-equal current-event-index, max
      <span class="muControl">break-unless</span> done?
      dummy:&amp;:event<span class="Special"> &lt;- </span>new <span class="Constant">event:type</span>
      <span class="muControl">return</span> *dummy, console/same-as-ingredient:<span class="Constant">0</span>, <span class="Constant">1/found</span>, <span class="Constant">1/quit</span>
    <span class="Delimiter">}</span>
    result<span class="Special"> &lt;- </span>index *buf, current-event-index
    current-event-index<span class="Special"> &lt;- </span>add current-event-index, <span class="Constant">1</span>
    *console<span class="Special"> &lt;- </span>put *console, <span class="Constant">current-event-index:offset</span>, current-event-index
    <span class="muControl">return</span> result, console/same-as-ingredient:<span class="Constant">0</span>, <span class="Constant">1/found</span>, <span class="Constant">0/quit</span>
  <span class="Delimiter">}</span>
  switch  <span class="Comment"># real event source is infrequent; avoid polling it too much</span>
  result:event, found?:bool<span class="Special"> &lt;- </span>check-for-interaction
  <span class="muControl">return</span> result, console/same-as-ingredient:<span class="Constant">0</span>, found?, <span class="Constant">0/quit</span>
]

<span class="Comment"># variant of read-event for just keyboard events. Discards everything that</span>
<span class="Comment"># isn't unicode, so no arrow keys, page-up/page-down, etc. But you still get</span>
<span class="Comment"># newlines, tabs, ctrl-d..</span>
<span class="muRecipe">def</span> read-key console:&amp;:console<span class="muRecipe"> -&gt; </span>result:char, console:&amp;:console, found?:bool, quit?:bool [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  x:event, console, found?:bool, quit?:bool<span class="Special"> &lt;- </span>read-event console
  <span class="muControl">return-if</span> quit?, <span class="Constant">0</span>, console/same-as-ingredient:<span class="Constant">0</span>, found?, quit?
  <span class="muControl">return-unless</span> found?, <span class="Constant">0</span>, console/same-as-ingredient:<span class="Constant">0</span>, found?, quit?
  c:char, converted?:bool<span class="Special"> &lt;- </span>maybe-convert x, <span class="Constant">text:variant</span>
  <span class="muControl">return-unless</span> converted?, <span class="Constant">0</span>, console/same-as-ingredient:<span class="Constant">0</span>, <span class="Constant">0/found</span>, <span class="Constant">0/quit</span>
  <span class="muControl">return</span> c, console/same-as-ingredient:<span class="Constant">0</span>, <span class="Constant">1/found</span>, <span class="Constant">0/quit</span>
]

<span class="muRecipe">def</span> send-keys-to-channel console:&amp;:console, chan:&amp;:sink:char, screen:&amp;:screen<span class="muRecipe"> -&gt; </span>console:&amp;:console, chan:&amp;:sink:char, screen:&amp;:screen [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Delimiter">{</span>
    c:char, console, found?:bool, quit?:bool<span class="Special"> &lt;- </span>read-key console
    <span class="muControl">loop-unless</span> found?
    <span class="muControl">break-if</span> quit?
    assert c, <span class="Constant">[invalid event, expected text]</span>
    screen<span class="Special"> &lt;- </span>print screen, c
    chan<span class="Special"> &lt;- </span>write chan, c
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  chan<span class="Special"> &lt;- </span>close chan
]

<span class="muRecipe">def</span> wait-for-event console:&amp;:console<span class="muRecipe"> -&gt; </span>console:&amp;:console [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Delimiter">{</span>
    _, console, found?:bool<span class="Special"> &lt;- </span>read-event console
    <span class="muControl">loop-unless</span> found?
  <span class="Delimiter">}</span>
]

<span class="Comment"># use this helper to skip rendering if there's lots of other events queued up</span>
<span class="muRecipe">def</span> has-more-events? console:&amp;:console<span class="muRecipe"> -&gt; </span>result:bool [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> console
    <span class="Comment"># fake consoles should be plenty fast; never skip</span>
    <span class="muControl">return</span> <span class="Constant">0/false</span>
  <span class="Delimiter">}</span>
  result<span class="Special"> &lt;- </span>interactions-left?
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->