<!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 - 081print.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; }
.CommentedCode { color: #6c6c6c; }
.muRecipe { color: #ff8700; }
.muScenario { color: #00af00; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># Wrappers around print primitives that take a 'screen' object and are thus</span>
<span class="Comment"># easier to test.</span>
<span class="muData">container</span> screen [
num-rows:num
num-columns:num
cursor-row:num
cursor-column:num
data:&:@:screen-cell
]
<span class="muData">container</span> screen-cell [
contents:char
color:num
]
<span class="muRecipe">def</span> new-fake-screen w:num, h:num<span class="muRecipe"> -> </span>result:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
result <span class="Special"><-</span> new <span class="Constant">screen:type</span>
bufsize:num <span class="Special"><-</span> multiply w, h
data:&:@:screen-cell <span class="Special"><-</span> new <span class="Constant">screen-cell:type</span>, bufsize
*result <span class="Special"><-</span> merge h/num-rows, w/num-columns, <span class="Constant">0/cursor-row</span>, <span class="Constant">0/cursor-column</span>, data
result <span class="Special"><-</span> clear-screen result
]
<span class="muRecipe">def</span> clear-screen screen:&:screen<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
<span class="muControl">break-if</span> screen
<span class="Comment"># real screen</span>
clear-display
<span class="muControl">return</span>
<span class="Delimiter">}</span>
<span class="Comment"># fake screen</span>
buf:&:@:screen-cell <span class="Special"><-</span> get *screen, <span class="Constant">data:offset</span>
max:num <span class="Special"><-</span> length *buf
i:num <span class="Special"><-</span> copy<span class="Constant"> 0</span>
<span class="Delimiter">{</span>
done?:bool <span class="Special"><-</span> greater-or-equal i, max
<span class="muControl">break-if</span> done?
curr:screen-cell <span class="Special"><-</span> merge <span class="Constant">0/empty</span>, <span class="Constant">7/white</span>
*buf <span class="Special"><-</span> put-index *buf, i, curr
i <span class="Special"><-</span> add i,<span class="Constant"> 1</span>
<span class="muControl">loop</span>
<span class="Delimiter">}</span>
<span class="Comment"># reset cursor</span>
*screen <span class="Special"><-</span> put *screen, <span class="Constant">cursor-row:offset</span>,<span class="Constant"> 0</span>
*screen <span class="Special"><-</span> put *screen, <span class="Constant">cursor-column:offset</span>,<span class="Constant"> 0</span>
]
<span class="muRecipe">def</span> sync-screen screen:&:screen<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="muControl">return-if</span> screen <span class="Comment"># do nothing for fake screens</span>
sync-display
]
<span class="muRecipe">def</span> fake-screen-is-empty? screen:&:screen<span class="muRecipe"> -> </span>result:bool [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="muControl">return-unless</span> screen, <span class="Constant">1/true</span> <span class="Comment"># do nothing for real screens</span>
buf:&:@:screen-cell <span class="Special"><-</span> get *screen, <span class="Constant">data:offset</span>
i:num <span class="Special"><-</span> copy<span class="Constant"> 0</span>
len:num <span class="Special"><-</span> length *buf
<span class="Delimiter">{</span>
done?:bool <span class="Special"><-</span> greater-or-equal i, len
<span class="muControl">break-if</span> done?
curr:screen-cell <span class="Special"><-</span> index *buf, i
curr-contents:char <span class="Special"><-</span> get curr, <span class="Constant">contents:offset</span>
i <span class="Special"><-</span> add i,<span class="Constant"> 1</span>
<span class="muControl">loop-unless</span> curr-contents
<span class="Comment"># not 0</span>
<span class="muControl">return</span> <span class="Constant">0/false</span>
<span class="Delimiter">}</span>
<span class="muControl">return</span> <span class="Constant">1/true</span>
]
<span class="muRecipe">def</span> print screen:&:screen, c:char<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
color:num, color-found?:bool <span class="Special"><-</span> <span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default color to white</span>
<span class="muControl">break-if</span> color-found?
color <span class="Special"><-</span> copy <span class="Constant">7/white</span>
<span class="Delimiter">}</span>
bg-color:num, bg-color-found?:bool <span class="Special"><-</span> <span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default bg-color to black</span>
<span class="muControl">break-if</span> bg-color-found?
bg-color <span class="Special"><-</span> copy <span class="Constant">0/black</span>
<span class="Delimiter">}</span>
c2:num <span class="Special"><-</span> character-to-code c
trace<span class="Constant"> 90</span>, <span class="Constant">[print-character]</span>, c2
<span class="Delimiter">{</span>
<span class="Comment"># real screen</span>
<span class="muControl">break-if</span> screen
print-character-to-display c, color, bg-color
<span class="muControl">return</span>
<span class="Delimiter">}</span>
<span class="Comment"># fake screen</span>
<span class="Comment"># (handle special cases exactly like in the real screen)</span>
width:num <span class="Special"><-</span> get *screen, <span class="Constant">num-columns:offset</span>
height:num <span class="Special"><-</span> get *screen, <span class="Constant">num-rows:offset</span>
<span class="Comment"># if cursor is out of bounds, silently exit</span>
row:num <span class="Special"><-</span> get *screen, <span class="Constant">cursor-row:offset</span>
legal?:bool <span class="Special"><-</span> greater-or-equal row,<span class="Constant"> 0</span>
<span class="muControl">return-unless</span> legal?
legal? <span class="Special"><-</span> lesser-than row, height
<span class="muControl">return-unless</span> legal?
column:num <span class="Special"><-</span> get *screen, <span class="Constant">cursor-column:offset</span>
legal? <span class="Special"><-</span> greater-or-equal column,<span class="Constant"> 0</span>
<span class="muControl">return-unless</span> legal?
legal? <span class="Special"><-</span> lesser-than column, width
<span class="muControl">return-unless</span> legal?
<span class="CommentedCode">#? $print [print-character (], row, [, ], column, [): ], c, 10/newline</span>
<span class="Comment"># special-case: newline</span>
<span class="Delimiter">{</span>
newline?:bool <span class="Special"><-</span> equal c, <span class="Constant">10/newline</span>
<span class="muControl">break-unless</span> newline?
<span class="Delimiter">{</span>
<span class="Comment"># unless cursor is already at bottom</span>
bottom:num <span class="Special"><-</span> subtract height,<span class="Constant"> 1</span>
at-bottom?:bool <span class="Special"><-</span> greater-or-equal row, bottom
<span class="muControl">break-if</span> at-bottom?
<span class="Comment"># move it to the next row</span>
column <span class="Special"><-</span> copy<span class="Constant"> 0</span>
*screen <span class="Special"><-</span> put *screen, <span class="Constant">cursor-column:offset</span>, column
row <span class="Special"><-</span> add row,<span class="Constant"> 1</span>
*screen <span class="Special"><-</span> put *screen, <span class="Constant">cursor-row:offset</span>, row
<span class="Delimiter">}</span>
<span class="muControl">return</span>
<span class="Delimiter">}</span>
<span class="Comment"># save character in fake screen</span>
index:num <span class="Special"><-</span> multiply row, width
index <span class="Special"><-</span> add index, column
buf:&:@:screen-cell <span class="Special"><-</span> get *screen, <span class="Constant">data:offset</span>
len:num <span class="Special"><-</span> length *buf
<span class="Comment"># special-case: backspace</span>
<span class="Delimiter">{</span>
backspace?:bool <span class="Special"><-</span> equal c,<span class="Constant"> 8</span>
<span class="muControl">break-unless</span> backspace?
<span class="Delimiter">{</span>
<span class="Comment"># unless cursor is already at left margin</span>
at-left?:bool <span class="Special"><-</span> lesser-or-equal column,<span class="Constant"> 0</span>
<span class="muControl">break-if</span> at-left?
<span class="Comment"># clear previous location</span>
column <span class="Special"><-</span> subtract column,<span class="Constant"> 1</span>
*screen <span class="Special"><-</span> put *screen, <span class="Constant">cursor-column:offset</span>, column
index <span class="Special"><-</span> subtract index,<span class="Constant"> 1</span>
cursor:screen-cell <span class="Special"><-</span> merge <span class="Constant">32/space</span>, <span class="Constant">7/white</span>
*buf <span class="Special"><-</span> put-index *buf, index, cursor
<span class="Delimiter">}</span>
<span class="muControl">return</span>
<span class="Delimiter">}</span>
cursor:screen-cell <span class="Special"><-</span> merge c, color
*buf <span class="Special"><-</span> put-index *buf, index, cursor
<span class="Comment"># increment column unless it's already all the way to the right</span>
<span class="Delimiter">{</span>
right:num <span class="Special"><-</span> subtract width,<span class="Constant"> 1</span>
at-right?:bool <span class="Special"><-</span> greater-or-equal column, right
<span class="muControl">break-if</span> at-right?
column <span class="Special"><-</span> add column,<span class="Constant"> 1</span>
*screen <span class="Special"><-</span> put *screen, <span class="Constant">cursor-column:offset</span>, column
<span class="Delimiter">}</span>
]
<span class="muScenario">scenario</span> print-character-at-top-left [
<span class="Constant">local-scope</span>
fake-screen:&:screen <span class="Special"><-</span> new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
run [
a:char <span class="Special"><-</span> copy <span class="Constant">97/a</span>
fake-screen <span class="Special"><-</span> print fake-screen, a:char
cell:&:@:screen-cell <span class="Special"><-</span> get *fake-screen, <span class="Constant">data:offset</span>
1:@:screen-cell/<span class="Special">raw</span> <span class="Special"><-</span> copy *cell
]
memory-should-contain [
<span class="Constant"> 1</span> <span class="Special"><-</span><span class="Constant"> 6</span> <span class="Comment"># width*height</span>
<span class="Constant"> 2</span> <span class="Special"><-</span><span class="Constant"> 97</span> <span class="Comment"># 'a'</span>
<span class="Constant"> 3</span> <span class="Special"><-</span><span class="Constant"> 7</span> <span class="Comment"># white</span>
<span class="Comment"># rest of screen is empty</span>
<span class="Constant"> 4</span> <span class="Special"><-</span><span class="Constant"> 0</span>
]
]
<span class="muScenario">scenario</span> print-character-in-color [
<span class="Constant">local-scope</span>
fake-screen:&:screen <span class="Special"><-</span> new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
run [
a:char <span class="Special"><-</span> copy <span class="Constant">97/a</span>
fake-screen <span class="Special"><-</span> print fake-screen, a:char, <span class="Constant">1/red</span>
cell:&:@:screen-cell <span class="Special"><-</span> get *fake-screen, <span class="Constant">data:offset</span>
1:@:screen-cell/<span class="Special">raw</span> <span class="Special"><-</span> copy *cell
]
memory-should-contain [
<span class="Constant"> 1</span> <span class="Special"><-</span><span class="Constant"> 6</span> <span class="Comment"># width*height</span>
<span class="Constant"> 2</span> <span class="Special"><-</span><span class="Constant"> 97</span> <span class="Comment"># 'a'</span>
<span class="Constant"> 3</span> <span class="Special"><-</span><span class="Constant"> 1</span> <span class="Comment"># red</span>
<span class="Comment"># rest of screen is empty</span>
<span class="Constant"> 4</span> <span class="Special"><-</span><span class="Constant"> 0</span>
]
]
<span class="muScenario">scenario</span> print-backspace-character [
<span class="Constant">local-scope</span>
fake-screen:&:screen <span class="Special"><-</span> new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
a:char <span class="Special"><-</span> copy <span class="Constant">97/a</span>
fake-screen <span class="Special"><-</span> print fake-screen, a
run [
backspace:char <span class="Special"><-</span> copy <span class="Constant">8/backspace</span>
fake-screen <span class="Special"><-</span> print fake-screen, backspace
10:num/<span class="Special">raw</span> <span class="Special"><-</span> get *fake-screen, <span class="Constant">cursor-column:offset</span>
cell:&:@:screen-cell <span class="Special"><-</span> get *fake-screen, <span class="Constant">data:offset</span>
11:@:screen-cell/<span class="Special">raw</span> <span class="Special"><-</span> copy *cell
]
memory-should-contain [
<span class="Constant"> 10</span> <span class="Special"><-</span><span class="Constant"> 0</span> <span class="Comment"># cursor column</span>
<span class="Constant"> 11</span> <span class="Special"><-</span><span class="Constant"> 6</span> <span class="Comment"># width*height</span>
<span class="Constant"> 12</span> <span class="Special"><-</span><span class="Constant"> 32</span> <span class="Comment"># space, not 'a'</span>
<span class="Constant"> 13</span> <span class="Special"><-</span><span class="Constant"> 7</span> <span class="Comment"># white</span>
<span class="Comment"># rest of screen is empty</span>
<span class="Constant"> 14</span> <span class="Special"><-</span><span class="Constant"> 0</span>
]
]
<span class="muScenario">scenario</span> print-extra-backspace-character [
<span class="Constant">local-scope</span>
fake-screen:&:screen <span class="Special"><-</span> new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
a:char <span class="Special"><-</span> copy <span class="Constant">97/a</span>
fake-screen <span class="Special"><-</span> print fake-screen, a
run [
backspace:char <span class="Special"><-</span> copy <span class="Constant">8/backspace</span>
fake-screen <span class="Special"><-</span> print fake-screen, backspace
fake-screen <span class="Special"><-</span> print fake-screen, backspace <span class="Comment"># cursor already at left margin</span>
1:num/<span class="Special">raw</span> <span class="Special"><-</span> get *fake-screen, <span class="Constant">cursor-column:offset</span>
cell:&:@:screen-cell <span class="Special"><-</span> get *fake-screen, <span class="Constant">data:offset</span>
3:@:screen-cell/<span class="Special">raw</span> <span class="Special"><-</span> copy *cell
]
memory-should-contain [
<span class="Constant"> 1</span> <span class="Special"><-</span><span class="Constant"> 0</span> <span class="Comment"># cursor column</span>
<span class="Constant"> 3</span> <span class="Special"><-</span><span class="Constant"> 6</span> <span class="Comment"># width*height</span>
<span class="Constant"> 4</span> <span class="Special"><-</span><span class="Constant"> 32</span> <span class="Comment"># space, not 'a'</span>
<span class="Constant"> 5</span> <span class="Special"><-</span><span class="Constant"> 7</span> <span class="Comment"># white</span>
<span class="Comment"># rest of screen is empty</span>
<span class="Constant"> 6</span> <span class="Special"><-</span><span class="Constant"> 0</span>
]
]
<span class="muScenario">scenario</span> print-character-at-right-margin [
<span class="Comment"># fill top row of screen with text</span>
<span class="Constant">local-scope</span>
fake-screen:&:screen <span class="Special"><-</span> new-fake-screen <span class="Constant">2/width</span>, <span class="Constant">2/height</span>
a:char <span class="Special"><-</span> copy <span class="Constant">97/a</span>
fake-screen <span class="Special"><-</span> print fake-screen, a
b:char <span class="Special"><-</span> copy <span class="Constant">98/b</span>
fake-screen <span class="Special"><-</span> print fake-screen, b
run [
<span class="Comment"># cursor now at right margin</span>
c:char <span class="Special"><-</span> copy <span class="Constant">99/c</span>
fake-screen <span class="Special"><-</span> print fake-screen, c
10:num/<span class="Special">raw</span> <span class="Special"><-</span> get *fake-screen, <span class="Constant">cursor-column:offset</span>
cell:&:@:screen-cell <span class="Special"><-</span> get *fake-screen, <span class="Constant">data:offset</span>
11:@:screen-cell/<span class="Special">raw</span> <span class="Special"><-</span> copy *cell
]
memory-should-contain [
<span class="Constant"> 10</span> <span class="Special"><-</span><span class="Constant"> 1</span> <span class="Comment"># cursor column</span>
<span class="Constant"> 11</span> <span class="Special"><-</span><span class="Constant"> 4</span> <span class="Comment"># width*height</span>
<span class="Constant"> 12</span> <span class="Special"><-</span><span class="Constant"> 97</span> <span class="Comment"># 'a'</span>
<span class="Constant"> 13</span> <span class="Special"><-</span><span class="Constant"> 7</span> <span class="Comment"># white</span>
<span class="Constant"> 14</span> <span class="Special"><-</span><span class="Constant"> 99</span> <span class="Comment"># 'c' over 'b'</span>
<span class="Constant"> 15</span> <span class="Special"><-</span><span class="Constant"> 7</span> <span class="Comment"># white</span>
<span class="Comment"># rest of screen is empty</span>
<span class="Constant"> 16</span> <span class="Special"><-</span><span class="Constant"> 0</span>
]
]
<span class="muScenario">scenario</span> print-newline-character [
<span class="Constant">local-scope</span>
fake-screen:&:screen <span class="Special"><-</span> new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
a:char <span class="Special"><-</span> copy <span class="Constant">97/a</span>
fake-screen <span class="Special"><-</span> print fake-screen, a
run [
newline:char <span class="Special"><-</span> copy <span class="Constant">10/newline</span>
fake-screen <span class="Special"><-</span> print fake-screen, newline
10:num/<span class="Special">raw</span> <span class="Special"><-</span> get *fake-screen, <span class="Constant">cursor-row:offset</span>
11:num/<span class="Special">raw</span> <span class="Special"><-</span> get *fake-screen, <span class="Constant">cursor-column:offset</span>
cell:&:@:screen-cell <span class="Special"><-</span> get *fake-screen, <span class="Constant">data:offset</span>
12:@:screen-cell/<span class="Special">raw</span> <span class="Special"><-</span> copy *cell
]
memory-should-contain [
<span class="Constant"> 10</span> <span class="Special"><-</span><span class="Constant"> 1</span> <span class="Comment"># cursor row</span>
<span class="Constant"> 11</span> <span class="Special"><-</span><span class="Constant"> 0</span> <span class="Comment"># cursor column</span>
<span class="Constant"> 12</span> <span class="Special"><-</span><span class="Constant"> 6</span> <span class="Comment"># width*height</span>
<span class="Constant"> 13</span> <span class="Special"><-</span><span class="Constant"> 97</span> <span class="Comment"># 'a'</span>
<span class="Constant"> 14</span> <span class="Special"><-</span><span class="Constant"> 7</span> <span class="Comment"># white</span>
<span class="Comment"># rest of screen is empty</span>
<span class="Constant"> 15</span> <span class="Special"><-</span><span class="Constant"> 0</span>
]
]
<span class="muScenario">scenario</span> print-newline-at-bottom-line [
<span class="Constant">local-scope</span>
fake-screen:&:screen <span class="Special"><-</span> new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
newline:char <span class="Special"><-</span> copy <span class="Constant">10/newline</span>
fake-screen <span class="Special"><-</span> print fake-screen, newline
fake-screen <span class="Special"><-</span> print fake-screen, newline
run [
<span class="Comment"># cursor now at bottom of screen</span>
fake-screen <span class="Special"><-</span> print fake-screen, newline
10:num/<span class="Special">raw</span> <span class="Special"><-</span> get *fake-screen, <span class="Constant">cursor-row:offset</span>
11:num/<span class="Special">raw</span> <span class="Special"><-</span> get *fake-screen, <span class="Constant">cursor-column:offset</span>
]
<span class="Comment"># doesn't move further down</span>
memory-should-contain [
<span class="Constant"> 10</span> <span class="Special"><-</span><span class="Constant"> 1</span> <span class="Comment"># cursor row</span>
<span class="Constant"> 11</span> <span class="Special"><-</span><span class="Constant"> 0</span> <span class="Comment"># cursor column</span>
]
]
<span class="muScenario">scenario</span> print-character-at-bottom-right [
<span class="Constant">local-scope</span>
fake-screen:&:screen <span class="Special"><-</span> new-fake-screen <span class="Constant">2/width</span>, <span class="Constant">2/height</span>
newline:char <span class="Special"><-</span> copy <span class="Constant">10/newline</span>
fake-screen <span class="Special"><-</span> print fake-screen, newline
a:char <span class="Special"><-</span> copy <span class="Constant">97/a</span>
fake-screen <span class="Special"><-</span> print fake-screen, a
b:char <span class="Special"><-</span> copy <span class="Constant">98/b</span>
fake-screen <span class="Special"><-</span> print fake-screen, b
c:char <span class="Special"><-</span> copy <span class="Constant">99/c</span>
fake-screen <span class="Special"><-</span> print fake-screen, c
fake-screen <span class="Special"><-</span> print fake-screen, newline
run [
<span class="Comment"># cursor now at bottom right</span>
d:char <span class="Special"><-</span> copy <span class="Constant">100/d</span>
fake-screen <span class="Special"><-</span> print fake-screen, d
10:num/<span class="Special">raw</span> <span class="Special"><-</span> get *fake-screen, <span class="Constant">cursor-row:offset</span>
11:num/<span class="Special">raw</span> <span class="Special"><-</span> get *fake-screen, <span class="Constant">cursor-column:offset</span>
cell:&:@:screen-cell <span class="Special"><-</span> get *fake-screen, <span class="Constant">data:offset</span>
20:@:screen-cell/<span class="Special">raw</span> <span class="Special"><-</span> copy *cell
]
memory-should-contain [
<span class="Constant"> 10</span> <span class="Special"><-</span><span class="Constant"> 1</span> <span class="Comment"># cursor row</span>
<span class="Constant"> 11</span> <span class="Special"><-</span><span class="Constant"> 1</span> <span class="Comment"># cursor column</span>
<span class="Constant"> 20</span> <span class="Special"><-</span><span class="Constant"> 4</span> <span class="Comment"># width*height</span>
<span class="Constant"> 21</span> <span class="Special"><-</span><span class="Constant"> 0</span> <span class="Comment"># unused</span>
<span class="Constant"> 22</span> <span class="Special"><-</span><span class="Constant"> 7</span> <span class="Comment"># white</span>
<span class="Constant"> 23</span> <span class="Special"><-</span><span class="Constant"> 0</span> <span class="Comment"># unused</span>
<span class="Constant"> 24</span> <span class="Special"><-</span><span class="Constant"> 7</span> <span class="Comment"># white</span>
<span class="Constant"> 25</span> <span class="Special"><-</span><span class="Constant"> 97</span> <span class="Comment"># 'a'</span>
<span class="Constant"> 26</span> <span class="Special"><-</span><span class="Constant"> 7</span> <span class="Comment"># white</span>
<span class="Constant"> 27</span> <span class="Special"><-</span><span class="Constant"> 100</span> <span class="Comment"># 'd' over 'b' and 'c' and newline</span>
<span class="Constant"> 28</span> <span class="Special"><-</span><span class="Constant"> 7</span> <span class="Comment"># white</span>
<span class="Comment"># rest of screen is empty</span>
<span class="Constant"> 29</span> <span class="Special"><-</span><span class="Constant"> 0</span>
]
]
<span class="muRecipe">def</span> clear-line screen:&:screen<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
space:char <span class="Special"><-</span> copy <span class="Constant">0/nul</span>
<span class="Delimiter">{</span>
<span class="muControl">break-if</span> screen
<span class="Comment"># real screen</span>
clear-line-on-display
<span class="muControl">return</span>
<span class="Delimiter">}</span>
<span class="Comment"># fake screen</span>
width:num <span class="Special"><-</span> get *screen, <span class="Constant">num-columns:offset</span>
column:num <span class="Special"><-</span> get *screen, <span class="Constant">cursor-column:offset</span>
original-column:num <span class="Special"><-</span> copy column
<span class="Comment"># space over the entire line</span>
<span class="Delimiter">{</span>
right:num <span class="Special"><-</span> subtract width,<span class="Constant"> 1</span>
done?:bool <span class="Special"><-</span> greater-or-equal column, right
<span class="muControl">break-if</span> done?
print screen, space
column <span class="Special"><-</span> add column,<span class="Constant"> 1</span>
<span class="muControl">loop</span>
<span class="Delimiter">}</span>
<span class="Comment"># now back to where the cursor was</span>
*screen <span class="Special"><-</span> put *screen, <span class="Constant">cursor-column:offset</span>, original-column
]
<span class="muRecipe">def</span> clear-line-until screen:&:screen, right:num/inclusive<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
_, column:num <span class="Special"><-</span> cursor-position screen
space:char <span class="Special"><-</span> copy <span class="Constant">32/space</span>
bg-color:num, bg-color-found?:bool <span class="Special"><-</span> <span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default bg-color to black</span>
<span class="muControl">break-if</span> bg-color-found?
bg-color <span class="Special"><-</span> copy <span class="Constant">0/black</span>
<span class="Delimiter">}</span>
<span class="Delimiter">{</span>
done?:bool <span class="Special"><-</span> greater-than column, right
<span class="muControl">break-if</span> done?
screen <span class="Special"><-</span> print screen, space, <span class="Constant">7/white</span>, bg-color <span class="Comment"># foreground color is mostly unused except if the cursor shows up at this cell</span>
column <span class="Special"><-</span> add column,<span class="Constant"> 1</span>
<span class="muControl">loop</span>
<span class="Delimiter">}</span>
]
<span class="muRecipe">def</span> cursor-position screen:&:screen<span class="muRecipe"> -> </span>row:num, column:num [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
<span class="muControl">break-if</span> screen
<span class="Comment"># real screen</span>
row, column <span class="Special"><-</span> cursor-position-on-display
<span class="muControl">return</span>
<span class="Delimiter">}</span>
<span class="Comment"># fake screen</span>
row:num <span class="Special"><-</span> get *screen, <span class="Constant">cursor-row:offset</span>
column:num <span class="Special"><-</span> get *screen, <span class="Constant">cursor-column:offset</span>
]
<span class="muRecipe">def</span> move-cursor screen:&:screen, new-row:num, new-column:num<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
<span class="muControl">break-if</span> screen
<span class="Comment"># real screen</span>
move-cursor-on-display new-row, new-column
<span class="muControl">return</span>
<span class="Delimiter">}</span>
<span class="Comment"># fake screen</span>
*screen <span class="Special"><-</span> put *screen, <span class="Constant">cursor-row:offset</span>, new-row
*screen <span class="Special"><-</span> put *screen, <span class="Constant">cursor-column:offset</span>, new-column
]
<span class="muScenario">scenario</span> clear-line-erases-printed-characters [
<span class="Constant">local-scope</span>
fake-screen:&:screen <span class="Special"><-</span> new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
<span class="Comment"># print a character</span>
a:char <span class="Special"><-</span> copy <span class="Constant">97/a</span>
fake-screen <span class="Special"><-</span> print fake-screen, a
<span class="Comment"># move cursor to start of line</span>
fake-screen <span class="Special"><-</span> move-cursor fake-screen, <span class="Constant">0/row</span>, <span class="Constant">0/column</span>
run [
fake-screen <span class="Special"><-</span> clear-line fake-screen
cell:&:@:screen-cell <span class="Special"><-</span> get *fake-screen, <span class="Constant">data:offset</span>
10:@:screen-cell/<span class="Special">raw</span> <span class="Special"><-</span> copy *cell
]
<span class="Comment"># screen should be blank</span>
memory-should-contain [
<span class="Constant"> 10</span> <span class="Special"><-</span><span class="Constant"> 6</span> <span class="Comment"># width*height</span>
<span class="Constant"> 11</span> <span class="Special"><-</span><span class="Constant"> 0</span>
<span class="Constant"> 12</span> <span class="Special"><-</span><span class="Constant"> 7</span>
<span class="Constant"> 13</span> <span class="Special"><-</span><span class="Constant"> 0</span>
<span class="Constant"> 14</span> <span class="Special"><-</span><span class="Constant"> 7</span>
<span class="Constant"> 15</span> <span class="Special"><-</span><span class="Constant"> 0</span>
<span class="Constant"> 16</span> <span class="Special"><-</span><span class="Constant"> 7</span>
<span class="Constant"> 17</span> <span class="Special"><-</span><span class="Constant"> 0</span>
<span class="Constant"> 18</span> <span class="Special"><-</span><span class="Constant"> 7</span>
<span class="Constant"> 19</span> <span class="Special"><-</span><span class="Constant"> 0</span>
<span class="Constant"> 20</span> <span class="Special"><-</span><span class="Constant"> 7</span>
<span class="Constant"> 21</span> <span class="Special"><-</span><span class="Constant"> 0</span>
<span class="Constant"> 22</span> <span class="Special"><-</span><span class="Constant"> 7</span>
]
]
<span class="muRecipe">def</span> cursor-down screen:&:screen<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
<span class="muControl">break-if</span> screen
<span class="Comment"># real screen</span>
move-cursor-down-on-display
<span class="muControl">return</span>
<span class="Delimiter">}</span>
<span class="Comment"># fake screen</span>
height:num <span class="Special"><-</span> get *screen, <span class="Constant">num-rows:offset</span>
row:num <span class="Special"><-</span> get *screen, <span class="Constant">cursor-row:offset</span>
max:num <span class="Special"><-</span> subtract height,<span class="Constant"> 1</span>
at-bottom?:bool <span class="Special"><-</span> greater-or-equal row, max
<span class="muControl">return-if</span> at-bottom?
row <span class="Special"><-</span> add row,<span class="Constant"> 1</span>
*screen <span class="Special"><-</span> put *screen, <span class="Constant">cursor-row:offset</span>, row
]
<span class="muRecipe">def</span> cursor-up screen:&:screen<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
<span class="muControl">break-if</span> screen
<span class="Comment"># real screen</span>
move-cursor-up-on-display
<span class="muControl">return</span>
<span class="Delimiter">}</span>
<span class="Comment"># fake screen</span>
row:num <span class="Special"><-</span> get *screen, <span class="Constant">cursor-row:offset</span>
at-top?:bool <span class="Special"><-</span> lesser-or-equal row,<span class="Constant"> 0</span>
<span class="muControl">return-if</span> at-top?
row <span class="Special"><-</span> subtract row,<span class="Constant"> 1</span>
*screen <span class="Special"><-</span> put *screen, <span class="Constant">cursor-row:offset</span>, row
]
<span class="muRecipe">def</span> cursor-right screen:&:screen<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
<span class="muControl">break-if</span> screen
<span class="Comment"># real screen</span>
move-cursor-right-on-display
<span class="muControl">return</span>
<span class="Delimiter">}</span>
<span class="Comment"># fake screen</span>
width:num <span class="Special"><-</span> get *screen, <span class="Constant">num-columns:offset</span>
column:num <span class="Special"><-</span> get *screen, <span class="Constant">cursor-column:offset</span>
max:num <span class="Special"><-</span> subtract width,<span class="Constant"> 1</span>
at-bottom?:bool <span class="Special"><-</span> greater-or-equal column, max
<span class="muControl">return-if</span> at-bottom?
column <span class="Special"><-</span> add column,<span class="Constant"> 1</span>
*screen <span class="Special"><-</span> put *screen, <span class="Constant">cursor-column:offset</span>, column
]
<span class="muRecipe">def</span> cursor-left screen:&:screen<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
<span class="muControl">break-if</span> screen
<span class="Comment"># real screen</span>
move-cursor-left-on-display
<span class="muControl">return</span>
<span class="Delimiter">}</span>
<span class="Comment"># fake screen</span>
column:num <span class="Special"><-</span> get *screen, <span class="Constant">cursor-column:offset</span>
at-top?:bool <span class="Special"><-</span> lesser-or-equal column,<span class="Constant"> 0</span>
<span class="muControl">return-if</span> at-top?
column <span class="Special"><-</span> subtract column,<span class="Constant"> 1</span>
*screen <span class="Special"><-</span> put *screen, <span class="Constant">cursor-column:offset</span>, column
]
<span class="muRecipe">def</span> cursor-to-start-of-line screen:&:screen<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
row:num <span class="Special"><-</span> cursor-position screen
column:num <span class="Special"><-</span> copy<span class="Constant"> 0</span>
screen <span class="Special"><-</span> move-cursor screen, row, column
]
<span class="muRecipe">def</span> cursor-to-next-line screen:&:screen<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
screen <span class="Special"><-</span> cursor-down screen
screen <span class="Special"><-</span> cursor-to-start-of-line screen
]
<span class="muRecipe">def</span> move-cursor-to-column screen:&:screen, column:num<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
row:num, _ <span class="Special"><-</span> cursor-position screen
move-cursor screen, row, column
]
<span class="muRecipe">def</span> screen-width screen:&:screen<span class="muRecipe"> -> </span>width:num [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> screen
<span class="Comment"># fake screen</span>
width <span class="Special"><-</span> get *screen, <span class="Constant">num-columns:offset</span>
<span class="muControl">return</span>
<span class="Delimiter">}</span>
<span class="Comment"># real screen</span>
width <span class="Special"><-</span> display-width
]
<span class="muRecipe">def</span> screen-height screen:&:screen<span class="muRecipe"> -> </span>height:num [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> screen
<span class="Comment"># fake screen</span>
height <span class="Special"><-</span> get *screen, <span class="Constant">num-rows:offset</span>
<span class="muControl">return</span>
<span class="Delimiter">}</span>
<span class="Comment"># real screen</span>
height <span class="Special"><-</span> display-height
]
<span class="muRecipe">def</span> hide-cursor screen:&:screen<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="muControl">reply-if</span> screen <span class="Comment"># fake screen; do nothing</span>
<span class="Comment"># real screen</span>
hide-cursor-on-display
]
<span class="muRecipe">def</span> show-cursor screen:&:screen<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="muControl">reply-if</span> screen <span class="Comment"># fake screen; do nothing</span>
<span class="Comment"># real screen</span>
show-cursor-on-display
]
<span class="muRecipe">def</span> hide-screen screen:&:screen<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="muControl">reply-if</span> screen <span class="Comment"># fake screen; do nothing</span>
<span class="Comment"># real screen</span>
hide-display
]
<span class="muRecipe">def</span> show-screen screen:&:screen<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="muControl">reply-if</span> screen <span class="Comment"># fake screen; do nothing</span>
<span class="Comment"># real screen</span>
show-display
]
<span class="muRecipe">def</span> print screen:&:screen, s:text<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
color:num, color-found?:bool <span class="Special"><-</span> <span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default color to white</span>
<span class="muControl">break-if</span> color-found?
color <span class="Special"><-</span> copy <span class="Constant">7/white</span>
<span class="Delimiter">}</span>
bg-color:num, bg-color-found?:bool <span class="Special"><-</span> <span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default bg-color to black</span>
<span class="muControl">break-if</span> bg-color-found?
bg-color <span class="Special"><-</span> copy <span class="Constant">0/black</span>
<span class="Delimiter">}</span>
len:num <span class="Special"><-</span> length *s
i:num <span class="Special"><-</span> copy<span class="Constant"> 0</span>
<span class="Delimiter">{</span>
done?:bool <span class="Special"><-</span> greater-or-equal i, len
<span class="muControl">break-if</span> done?
c:char <span class="Special"><-</span> index *s, i
print screen, c, color, bg-color
i <span class="Special"><-</span> add i,<span class="Constant"> 1</span>
<span class="muControl">loop</span>
<span class="Delimiter">}</span>
]
<span class="muScenario">scenario</span> print-text-stops-at-right-margin [
<span class="Constant">local-scope</span>
fake-screen:&:screen <span class="Special"><-</span> new-fake-screen <span class="Constant">3/width</span>, <span class="Constant">2/height</span>
run [
fake-screen <span class="Special"><-</span> print fake-screen, <span class="Constant">[abcd]</span>
cell:&:@:screen-cell <span class="Special"><-</span> get *fake-screen, <span class="Constant">data:offset</span>
10:@:screen-cell/<span class="Special">raw</span> <span class="Special"><-</span> copy *cell
]
memory-should-contain [
<span class="Constant"> 10</span> <span class="Special"><-</span><span class="Constant"> 6</span> <span class="Comment"># width*height</span>
<span class="Constant"> 11</span> <span class="Special"><-</span><span class="Constant"> 97</span> <span class="Comment"># 'a'</span>
<span class="Constant"> 12</span> <span class="Special"><-</span><span class="Constant"> 7</span> <span class="Comment"># white</span>
<span class="Constant"> 13</span> <span class="Special"><-</span><span class="Constant"> 98</span> <span class="Comment"># 'b'</span>
<span class="Constant"> 14</span> <span class="Special"><-</span><span class="Constant"> 7</span> <span class="Comment"># white</span>
<span class="Constant"> 15</span> <span class="Special"><-</span><span class="Constant"> 100</span> <span class="Comment"># 'd' overwrites 'c'</span>
<span class="Constant"> 16</span> <span class="Special"><-</span><span class="Constant"> 7</span> <span class="Comment"># white</span>
<span class="Comment"># rest of screen is empty</span>
<span class="Constant"> 17</span> <span class="Special"><-</span><span class="Constant"> 0</span>
]
]
<span class="muRecipe">def</span> print-integer screen:&:screen, n:num<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
color:num, color-found?:bool <span class="Special"><-</span> <span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default color to white</span>
<span class="muControl">break-if</span> color-found?
color <span class="Special"><-</span> copy <span class="Constant">7/white</span>
<span class="Delimiter">}</span>
bg-color:num, bg-color-found?:bool <span class="Special"><-</span> <span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default bg-color to black</span>
<span class="muControl">break-if</span> bg-color-found?
bg-color <span class="Special"><-</span> copy <span class="Constant">0/black</span>
<span class="Delimiter">}</span>
<span class="Comment"># todo: other bases besides decimal</span>
s:text <span class="Special"><-</span> to-text n
screen <span class="Special"><-</span> print screen, s, color, bg-color
]
<span class="Comment"># for now, we can only print integers</span>
<span class="muRecipe">def</span> print screen:&:screen, n:num<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
color:num, color-found?:bool <span class="Special"><-</span> <span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default color to white</span>
<span class="muControl">break-if</span> color-found?
color <span class="Special"><-</span> copy <span class="Constant">7/white</span>
<span class="Delimiter">}</span>
bg-color:num, bg-color-found?:bool <span class="Special"><-</span> <span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default bg-color to black</span>
<span class="muControl">break-if</span> bg-color-found?
bg-color <span class="Special"><-</span> copy <span class="Constant">0/black</span>
<span class="Delimiter">}</span>
screen <span class="Special"><-</span> print-integer screen, n, color, bg-color
]
<span class="Comment"># addresses</span>
<span class="muRecipe">def</span> print screen:&:screen, n:&:_elem<span class="muRecipe"> -> </span>screen:&:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
color:num, color-found?:bool <span class="Special"><-</span> <span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default color to white</span>
<span class="muControl">break-if</span> color-found?
color <span class="Special"><-</span> copy <span class="Constant">7/white</span>
<span class="Delimiter">}</span>
bg-color:num, bg-color-found?:bool <span class="Special"><-</span> <span class="Constant">next-ingredient</span>
<span class="Delimiter">{</span>
<span class="Comment"># default bg-color to black</span>
<span class="muControl">break-if</span> bg-color-found?
bg-color <span class="Special"><-</span> copy <span class="Constant">0/black</span>
<span class="Delimiter">}</span>
n2:num <span class="Special"><-</span> copy n
screen <span class="Special"><-</span> print-integer screen, n2, color, bg-color
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->