use markdown syntax for images
[teliva.git] / break.tlv
blobbc3806a21cf215187813218acdbf485571bec904
1 # .tlv file generated by https://github.com/akkartik/teliva
2 # You may edit it if you are careful; however, you may see cryptic errors if you
3 # violate Teliva's assumptions.
5 # .tlv files are representations of Teliva programs. Teliva programs consist of
6 # sequences of definitions. Each definition is a table of key/value pairs. Keys
7 # and values are both strings.
9 # Lines in .tlv files always follow exactly one of the following forms:
10 # - comment lines at the top of the file starting with '#' at column 0
11 # - beginnings of definitions starting with '- ' at column 0, followed by a
12 #   key/value pair
13 # - key/value pairs consisting of '  ' at column 0, containing either a
14 #   spaceless value on the same line, or a multi-line value
15 # - multiline values indented by more than 2 spaces, starting with a '>'
17 # If these constraints are violated, Teliva may unceremoniously crash. Please
18 # report bugs at http://akkartik.name/contact
19 - __teliva_timestamp: original
20   str_helpers:
21     >-- some string helpers from http://lua-users.org/wiki/StringIndexing
22     >
23     >-- index characters using []
24     >getmetatable('').__index = function(str,i)
25     >  if type(i) == 'number' then
26     >    return str:sub(i,i)
27     >  else
28     >    return string[i]
29     >  end
30     >end
31     >
32     >-- ranges using (), selected bytes using {}
33     >getmetatable('').__call = function(str,i,j)
34     >  if type(i)~='table' then
35     >    return str:sub(i,j)
36     >  else
37     >    local t={}
38     >    for k,v in ipairs(i) do
39     >      t[k]=str:sub(v,v)
40     >    end
41     >    return table.concat(t)
42     >  end
43     >end
44     >
45     >-- iterate over an ordered sequence
46     >function q(x)
47     >  if type(x) == 'string' then
48     >    return x:gmatch('.')
49     >  else
50     >    return ipairs(x)
51     >  end
52     >end
53     >
54     >-- insert within string
55     >function string.insert(str1, str2, pos)
56     >  return str1:sub(1,pos)..str2..str1:sub(pos+1)
57     >end
58     >
59     >function string.remove(s, pos)
60     >  return s:sub(1,pos-1)..s:sub(pos+1)
61     >end
62     >
63     >function string.pos(s, sub)
64     >  return string.find(s, sub, 1, true)  -- plain=true to disable regular expressions
65     >end
66     >
67     >-- TODO: backport utf-8 support from Lua 5.3
68 - __teliva_timestamp: original
69   debugy:
70     >debugy = 5
71 - __teliva_timestamp: original
72   dbg:
73     >-- helper for debug by print; overlay debug information towards the right
74     >-- reset debugy every time you refresh screen
75     >function dbg(window, s)
76     >  local oldy = 0
77     >  local oldx = 0
78     >  oldy, oldx = window:getyx()
79     >  window:mvaddstr(debugy, 60, s)
80     >  debugy = debugy+1
81     >  window:mvaddstr(oldy, oldx, '')
82     >end
83 - __teliva_timestamp: original
84   check:
85     >function check(x, msg)
86     >  if x then
87     >    Window:addch('.')
88     >  else
89     >    print('F - '..msg)
90     >    print('  '..str(x)..' is false/nil')
91     >    teliva_num_test_failures = teliva_num_test_failures + 1
92     >    -- overlay first test failure on editors
93     >    if teliva_first_failure == nil then
94     >      teliva_first_failure = msg
95     >    end
96     >  end
97     >end
98 - __teliva_timestamp: original
99   check_eq:
100     >function check_eq(x, expected, msg)
101     >  if eq(x, expected) then
102     >    Window:addch('.')
103     >  else
104     >    print('F - '..msg)
105     >    print('  expected '..str(expected)..' but got '..str(x))
106     >    teliva_num_test_failures = teliva_num_test_failures + 1
107     >    -- overlay first test failure on editors
108     >    if teliva_first_failure == nil then
109     >      teliva_first_failure = msg
110     >    end
111     >  end
112     >end
113 - __teliva_timestamp: original
114   eq:
115     >function eq(a, b)
116     >  if type(a) ~= type(b) then return false end
117     >  if type(a) == 'table' then
118     >    if #a ~= #b then return false end
119     >    for k, v in pairs(a) do
120     >      if b[k] ~= v then
121     >        return false
122     >      end
123     >    end
124     >    for k, v in pairs(b) do
125     >      if a[k] ~= v then
126     >        return false
127     >      end
128     >    end
129     >    return true
130     >  end
131     >  return a == b
132     >end
133 - __teliva_timestamp: original
134   str:
135     >-- smarter tostring
136     >-- slow; used only for debugging
137     >function str(x)
138     >  if type(x) == 'table' then
139     >    local result = ''
140     >    result = result..#x..'{'
141     >    for k, v in pairs(x) do
142     >      result = result..str(k)..'='..str(v)..', '
143     >    end
144     >    result = result..'}'
145     >    return result
146     >  elseif type(x) == 'string' then
147     >    return '"'..x..'"'
148     >  end
149     >  return tostring(x)
150     >end
151 - __teliva_timestamp: original
152   find_index:
153     >function find_index(arr, x)
154     >  for n, y in ipairs(arr) do
155     >    if x == y then
156     >      return n
157     >    end
158     >  end
159     >end
160 - __teliva_timestamp: original
161   trim:
162     >function trim(s)
163     >  return s:gsub('^%s*', ''):gsub('%s*$', '')
164     >end
165 - __teliva_timestamp: original
166   split:
167     >function split(s, d)
168     >  result = {}
169     >  for match in (s..d):gmatch("(.-)"..d) do
170     >    table.insert(result, match);
171     >  end
172     >  return result
173     >end
174 - __teliva_timestamp: original
175   map:
176     >-- only for arrays
177     >function map(l, f)
178     >  result = {}
179     >  for _, x in ipairs(l) do
180     >    table.insert(result, f(x))
181     >  end
182     >  return result
183     >end
184 - __teliva_timestamp: original
185   reduce:
186     >-- only for arrays
187     >function reduce(l, f, init)
188     >  result = init
189     >  for _, x in ipairs(l) do
190     >    result = f(result, x)
191     >  end
192     >  return result
193     >end
194 - __teliva_timestamp: original
195   filter:
196     >function filter(h, f)
197     >  result = {}
198     >  for k, v in pairs(h) do
199     >    if f(k, v) then
200     >      result[k] = v
201     >    end
202     >  end
203     >  return result
204     >end
205 - __teliva_timestamp: original
206   ifilter:
207     >-- only for arrays
208     >function ifilter(l, f)
209     >  result = {}
210     >  for _, x in ipairs(l) do
211     >    if f(x) then
212     >      table.insert(result, x)
213     >    end
214     >  end
215     >  return result
216     >end
217 - __teliva_timestamp: original
218   sort_letters:
219     >function sort_letters(s)
220     >  tmp = {}
221     >  for i=1,#s do
222     >    table.insert(tmp, s[i])
223     >  end
224     >  table.sort(tmp)
225     >  local result = ''
226     >  for _, c in pairs(tmp) do
227     >    result = result..c
228     >  end
229     >  return result
230     >end
231     >
232     >function test_sort_letters(s)
233     >  check_eq(sort_letters(''), '', 'test_sort_letters: empty')
234     >  check_eq(sort_letters('ba'), 'ab', 'test_sort_letters: non-empty')
235     >  check_eq(sort_letters('abba'), 'aabb', 'test_sort_letters: duplicates')
236     >end
237 - __teliva_timestamp: original
238   count_letters:
239     >-- TODO: handle unicode
240     >function count_letters(s)
241     >  local result = {}
242     >  for i=1,s:len() do
243     >    local c = s[i]
244     >    if result[c] == nil then
245     >      result[c] = 1
246     >    else
247     >      result[c] = result[c] + 1
248     >    end
249     >  end
250     >  return result
251     >end
252 - __teliva_timestamp: original
253   count:
254     >-- turn an array of elements into a map from elements to their frequency
255     >-- analogous to count_letters for non-strings
256     >function count(a)
257     >  local result = {}
258     >  for i, v in ipairs(a) do
259     >    if result[v] == nil then
260     >      result[v] = 1
261     >    else
262     >      result[v] = result[v] + 1
263     >    end
264     >  end
265     >  return result
266     >end
267 - __teliva_timestamp: original
268   union:
269     >function union(a, b)
270     >  for k, v in pairs(b) do
271     >    a[k] = v
272     >  end
273     >  return a
274     >end
275 - __teliva_timestamp: original
276   subtract:
277     >-- set subtraction
278     >function subtract(a, b)
279     >  for k, v in pairs(b) do
280     >    a[k] = nil
281     >  end
282     >  return a
283     >end
284 - __teliva_timestamp: original
285   all:
286     >-- universal quantifier on sets
287     >function all(s, f)
288     >  for k, v in pairs(s) do
289     >    if not f(k, v) then
290     >      return false
291     >    end
292     >  end
293     >  return true
294     >end
295 - __teliva_timestamp: original
296   to_array:
297     >-- turn a set into an array
298     >-- drops values
299     >function to_array(h)
300     >  local result = {}
301     >  for k, _ in pairs(h) do
302     >    table.insert(result, k)
303     >  end
304     >  return result
305     >end
306 - __teliva_timestamp: original
307   append:
308     >-- concatenate list 'elems' into 'l', modifying 'l' in the process
309     >function append(l, elems)
310     >  for i=1,#elems do
311     >    table.insert(l, elems[i])
312     >  end
313     >end
314 - __teliva_timestamp: original
315   prepend:
316     >-- concatenate list 'elems' into the start of 'l', modifying 'l' in the process
317     >function prepend(l, elems)
318     >  for i=1,#elems do
319     >    table.insert(l, i, elems[i])
320     >  end
321     >end
322 - __teliva_timestamp: original
323   all_but:
324     >function all_but(x, idx)
325     >  if type(x) == 'table' then
326     >    local result = {}
327     >    for i, elem in ipairs(x) do
328     >      if i ~= idx then
329     >        table.insert(result,elem)
330     >      end
331     >    end
332     >    return result
333     >  elseif type(x) == 'string' then
334     >    if idx < 1 then return x:sub(1) end
335     >    return x:sub(1, idx-1) .. x:sub(idx+1)
336     >  else
337     >    error('all_but: unsupported type '..type(x))
338     >  end
339     >end
340     >
341     >function test_all_but()
342     >  check_eq(all_but('', 0), '', 'all_but: empty')
343     >  check_eq(all_but('abc', 0), 'abc', 'all_but: invalid low index')
344     >  check_eq(all_but('abc', 4), 'abc', 'all_but: invalid high index')
345     >  check_eq(all_but('abc', 1), 'bc', 'all_but: first index')
346     >  check_eq(all_but('abc', 3), 'ab', 'all_but: final index')
347     >  check_eq(all_but('abc', 2), 'ac', 'all_but: middle index')
348     >end
349 - __teliva_timestamp: original
350   set:
351     >function set(l)
352     >  local result = {}
353     >  for i, elem in ipairs(l) do
354     >    result[elem] = true
355     >  end
356     >  return result
357     >end
358 - __teliva_timestamp: original
359   set_eq:
360     >function set_eq(l1, l2)
361     >  return eq(set(l1), set(l2))
362     >end
363     >
364     >function test_set_eq()
365     >  check(set_eq({1}, {1}), 'set_eq: identical')
366     >  check(not set_eq({1, 2}, {1, 3}), 'set_eq: different')
367     >  check(set_eq({1, 2}, {2, 1}), 'set_eq: order')
368     >  check(set_eq({1, 2, 2}, {2, 1}), 'set_eq: duplicates')
369     >end
370 - __teliva_timestamp: original
371   clear:
372     >function clear(lines)
373     >  while #lines > 0 do
374     >    table.remove(lines)
375     >  end
376     >end
377 - __teliva_timestamp: original
378   zap:
379     >function zap(target, src)
380     >  clear(target)
381     >  append(target, src)
382     >end
383 - __teliva_timestamp: original
384   mfactorial:
385     >-- memoized version of factorial
386     >-- doesn't memoize recursive calls, but may be good enough
387     >mfactorial = memo1(factorial)
388 - __teliva_timestamp: original
389   factorial:
390     >function factorial(n)
391     >  local result = 1
392     >  for i=1,n do
393     >    result = result*i
394     >  end
395     >  return result
396     >end
397 - __teliva_timestamp: original
398   memo1:
399     >-- a higher-order function that takes a function of a single arg
400     >-- (that never returns nil)
401     >-- and returns a memoized version of it
402     >function memo1(f)
403     >  local memo = {}
404     >  return function(x)
405     >    if memo[x] == nil then
406     >      memo[x] = f(x)
407     >    end
408     >    return memo[x]
409     >  end
410     >end
411     >
412     >-- mfactorial doesn't seem noticeably faster
413     >function test_memo1()
414     >  for i=0,30 do
415     >    check_eq(mfactorial(i), factorial(i), 'memo1 over factorial: '..str(i))
416     >  end
417     >end
418 - __teliva_timestamp: original
419   num_permutations:
420     >-- number of permutations of n distinct objects, taken r at a time
421     >function num_permutations(n, r)
422     >  return factorial(n)/factorial(n-r)
423     >end
424     >
425     >-- mfactorial doesn't seem noticeably faster
426     >function test_memo1()
427     >  for i=0,30 do
428     >    for j=0,i do
429     >      check_eq(num_permutations(i, j), mfactorial(i)/mfactorial(i-j), 'num_permutations memoizes: '..str(i)..'P'..str(j))
430     >    end
431     >  end
432     >end
433 - __teliva_timestamp: original
434   menu:
435     >-- To show app-specific hotkeys in the menu bar, add hotkey/command
436     >-- arrays of strings to the menu array.
437     >menu = {}
438 - __teliva_timestamp: original
439   Window:
440     >Window = curses.stdscr()
441 - __teliva_timestamp: original
442   window:
443     >-- constructor for fake screen and window
444     >-- call it like this:
445     >--   local w = window{
446     >--     kbd=kbd('abc'),
447     >--     scr=scr{h=5, w=4},
448     >--   }
449     >-- eventually it'll do everything a real ncurses window can
450     >function window(h)
451     >  h.__index = h
452     >  setmetatable(h, h)
453     >  h.__index = function(table, key)
454     >    return rawget(h, key)
455     >  end
456     >  h.attrset = function(self, x)
457     >    self.scr.attrs = x
458     >  end
459     >  h.attron = function(self, x)
460     >    -- currently same as attrset since Lua 5.1 doesn't have bitwise operators
461     >    -- doesn't support multiple attrs at once
462     >--    local old = self.scr.attrs
463     >--    self.scr.attrs = old|x
464     >    self.scr.attrs = x
465     >  end
466     >  h.attroff = function(self, x)
467     >    -- currently borked since Lua 5.1 doesn't have bitwise operators
468     >    -- doesn't support multiple attrs at once
469     >--    local old = self.scr.attrs
470     >--    self.scr.attrs = old & (~x)
471     >    self.scr.attrs = curses.A_NORMAL
472     >  end
473     >  h.getch = function(self)
474     >    local c = table.remove(h.kbd, 1)
475     >    if c == nil then return c end
476     >    return string.byte(c)  -- for verisimilitude with ncurses
477     >  end
478     >  h.addch = function(self, c)
479     >    local scr = self.scr
480     >    if c == '\n' then
481     >      scr.cursy = scr.cursy+1
482     >      scr.cursx = 0
483     >      return
484     >    end
485     >    if scr.cursy <= scr.h then
486     >      scr[scr.cursy][scr.cursx] = {data=c, attrs=scr.attrs}
487     >      scr.cursx = scr.cursx+1
488     >      if scr.cursx > scr.w then
489     >        scr.cursy = scr.cursy+1
490     >        scr.cursx = 1
491     >      end
492     >    end
493     >  end
494     >  h.addstr = function(self, s)
495     >    for i=1,s:len() do
496     >      self:addch(s[i])
497     >    end
498     >  end
499     >  h.mvaddch = function(self, y, x, c)
500     >    self.scr.cursy = y
501     >    self.scr.cursx = x
502     >    self:addch(c)
503     >  end
504     >  h.mvaddstr = function(self, y, x, s)
505     >    self.scr.cursy = y
506     >    self.scr.cursx = x
507     >    self:addstr(s)
508     >  end
509     >  h.clear = function(self)
510     >    clear_scr(self.scr)
511     >  end
512     >  h.refresh = function(self)
513     >    -- nothing
514     >  end
515     >  return h
516     >end
517 - __teliva_timestamp: original
518   kbd:
519     >function kbd(keys)
520     >  local result = {}
521     >  for i=1,keys:len() do
522     >    table.insert(result, keys[i])
523     >  end
524     >  return result
525     >end
526 - __teliva_timestamp: original
527   scr:
528     >function scr(props)
529     >  props.cursx = 1
530     >  props.cursy = 1
531     >  clear_scr(props)
532     >  return props
533     >end
534 - __teliva_timestamp: original
535   clear_scr:
536     >function clear_scr(props)
537     >  props.cursy = 1
538     >  props.cursx = 1
539     >  for y=1,props.h do
540     >    props[y] = {}
541     >    for x=1,props.w do
542     >      props[y][x] = {data=' ', attrs=curses.A_NORMAL}
543     >    end
544     >  end
545     >  return props
546     >end
547 - __teliva_timestamp: original
548   check_screen:
549     >function check_screen(window, contents, message)
550     >  local x, y = 1, 1
551     >  for i=1,contents:len() do
552     >    check_eq(window.scr[y][x].data, contents[i], message..'/'..y..','..x)
553     >    x = x+1
554     >    if x > window.scr.w then
555     >      y = y+1
556     >      x = 1
557     >    end
558     >  end
559     >end
560     >
561     >-- putting it all together, an example test of both keyboard and screen
562     >function test_check_screen()
563     >  local lines = {
564     >    c='123',
565     >    d='234',
566     >    a='345',
567     >    b='456',
568     >  }
569     >  local w = window{
570     >    kbd=kbd('abc'),
571     >    scr=scr{h=3, w=5},
572     >  }
573     >  local y = 1
574     >  while true do
575     >    local b = w:getch()
576     >    if b == nil then break end
577     >    w:mvaddstr(y, 1, lines[string.char(b)])
578     >    y = y+1
579     >  end
580     >  check_screen(w, '345  '..
581     >                  '456  '..
582     >                  '123  ',
583     >              'test_check_screen')
584     >end
585 - __teliva_timestamp: original
586   check_reverse:
587     >function check_reverse(window, contents, message)
588     >  local x, y = 1, 1
589     >  for i=1,contents:len() do
590     >    if contents[i] ~= ' ' then
591     >      -- hacky version while we're without bitwise operators on Lua 5.1
592     >--      check(window.scr[y][x].attrs & curses.A_REVERSE, message..'/'..y..','..x)
593     >      check_eq(window.scr[y][x].attrs, curses.A_REVERSE, message..'/'..y..','..x)
594     >    else
595     >      -- hacky version while we're without bitwise operators on Lua 5.1
596     >--      check(window.scr[y][x].attrs & (~curses.A_REVERSE), message..'/'..y..','..x)
597     >      check(window.scr[y][x].attrs ~= curses.A_REVERSE, message..'/'..y..','..x)
598     >    end
599     >    x = x+1
600     >    if x > window.scr.w then
601     >      y = y+1
602     >      x = 1
603     >    end
604     >  end
605     >end
606 - __teliva_timestamp: original
607   check_bold:
608     >function check_bold(window, contents, message)
609     >  local x, y = 1, 1
610     >  for i=1,contents:len() do
611     >    if contents[i] ~= ' ' then
612     >      -- hacky version while we're without bitwise operators on Lua 5.1
613     >--      check(window.scr[y][x].attrs & curses.A_BOLD, message..'/'..y..','..x)
614     >      check_eq(window.scr[y][x].attrs, curses.A_BOLD, message..'/'..y..','..x)
615     >    else
616     >      -- hacky version while we're without bitwise operators on Lua 5.1
617     >--      check(window.scr[y][x].attrs & (~curses.A_BOLD), message..'/'..y..','..x)
618     >      check(window.scr[y][x].attrs ~= curses.A_BOLD, message..'/'..y..','..x)
619     >    end
620     >    x = x+1
621     >    if x > window.scr.w then
622     >      y = y+1
623     >      x = 1
624     >    end
625     >  end
626     >end
627 - __teliva_timestamp: original
628   check_color:
629     >-- check which parts of a screen have the given color_pair
630     >function check_color(window, cp, contents, message)
631     >  local x, y = 1, 1
632     >  for i=1,contents:len() do
633     >    if contents[i] ~= ' ' then
634     >      -- hacky version while we're without bitwise operators on Lua 5.1
635     >--      check(window.scr[y][x].attrs & curses.color_pair(cp), message..'/'..y..','..x)
636     >      check_eq(window.scr[y][x].attrs, curses.color_pair(cp), message..'/'..y..','..x)
637     >    else
638     >      -- hacky version while we're without bitwise operators on Lua 5.1
639     >--      check(window.scr[y][x].attrs & (~curses.A_BOLD), message..'/'..y..','..x)
640     >      check(window.scr[y][x].attrs ~= curses.color_pair(cp), message..'/'..y..','..x)
641     >    end
642     >    x = x+1
643     >    if x > window.scr.w then
644     >      y = y+1
645     >      x = 1
646     >    end
647     >  end
648     >end
649 - __teliva_timestamp: original
650   sep:
651     >-- horizontal separator
652     >function sep(window)
653     >  local y, _ = window:getyx()
654     >  window:mvaddstr(y+1, 0, '')
655     >  local _, cols = window:getmaxyx()
656     >  for col=1,cols do
657     >    window:addstr('_')
658     >  end
659     >end
660 - __teliva_timestamp: original
661   render:
662     >function render(window)
663     >  window:clear()
664     >  -- draw stuff to screen here
665     >  window:attron(curses.A_BOLD)
666     >  window:mvaddstr(1, 5, "example app")
667     >  window:attrset(curses.A_NORMAL)
668     >  for i=0,15 do
669     >    window:attrset(curses.color_pair(i))
670     >    window:mvaddstr(3+i, 5, "========================")
671     >  end
672     >  window:refresh()
673     >end
674 - __teliva_timestamp: original
675   update:
676     >function update(window)
677     >  local key = window:getch()
678     >  -- process key here
679     >end
680 - __teliva_timestamp: original
681   init_colors:
682     >function init_colors()
683     >  for i=0,7 do
684     >    curses.init_pair(i, i, -1)
685     >  end
686     >  curses.init_pair(8, 7, 0)
687     >  curses.init_pair(9, 7, 1)
688     >  curses.init_pair(10, 7, 2)
689     >  curses.init_pair(11, 7, 3)
690     >  curses.init_pair(12, 7, 4)
691     >  curses.init_pair(13, 7, 5)
692     >  curses.init_pair(14, 7, 6)
693     >  curses.init_pair(15, -1, 15)
694     >end
695 - __teliva_timestamp: original
696   main:
697     >function main()
698     >  init_colors()
699     >
700     >  while true do
701     >    render(Window)
702     >    update(Window)
703     >  end
704     >end
705 - __teliva_timestamp:
706     >Thu Mar 17 21:21:29 2022
707   program:
708     >program = {lines = nil, cursor = nil}
709     >-- lines: [line]
710     >-- cursor: int, index into lines
711     >-- line: {[word], cursor}
712     >-- word: {string, cursor}
713 - __teliva_timestamp:
714     >Thu Mar 17 21:27:38 2022
715   render:
716     >function render(window)
717     >  local key = window:getch()
718     >  for row, line in ipair(program.lines) do
719     >    for col, word in ipair(line.words) do
720     >      window:addstr(word)
721     >      window:addstr(' ')
722     >    end
723     >    window:addstr('\n')
724     >  end
725     >end
726 - __teliva_timestamp:
727     >Thu Mar 17 21:28:39 2022
728   render:
729     >function render(window)
730     >  local key = window:getch()
731     >  for row, line in ipairs(program.lines) do
732     >    for col, word in pairs(line.words) do
733     >      window:addstr(word)
734     >      window:addstr(' ')
735     >    end
736     >    window:addstr('\n')
737     >  end
738     >end
739 - __teliva_timestamp:
740     >Thu Mar 17 21:31:33 2022
741   Program:
742     >Program = {
743     >  cursor_y = 1,
744     >  lines = {
745     >    {
746     >      cursor_x = 1,
747     >      words = {
748     >        {
749     >          cursor = 0,
750     >          data = '',
751     >        },
752     >      },
753     >    },
754     >  },
755     >}
756     >
757     >
758     >
759     >
760     >
761 - __teliva_timestamp:
762     >Thu Mar 17 21:32:05 2022
763   render:
764     >function render(window)
765     >  window:clear()
766     >  for row, line in ipairs(program.lines) do
767     >    for col, word in pairs(line.words) do
768     >      window:addstr(word)
769     >      window:addstr(' ')
770     >    end
771     >    window:addstr('\n')
772     >  end
773     >  window:refresh()
774     >end
775 - __teliva_timestamp:
776     >Thu Mar 17 21:32:22 2022
777   render:
778     >function render(window)
779     >  window:clear()
780     >  for row, line in ipairs(program.lines) do
781     >    for col, word in pairs(line.words) do
782     >      window:addstr(word.data)
783     >      window:addstr(' ')
784     >    end
785     >    window:addstr('\n')
786     >  end
787     >  window:refresh()
788     >end
789 - __teliva_timestamp:
790     >Thu Mar 17 21:33:00 2022
791   update:
792     >function update(window)
793     >  local key = window:getch()
794     >  if key == ' ' then
795     >    print('space')
796     >    window:getch()
797     >  end
798     >end
799 - __teliva_timestamp:
800     >Thu Mar 17 21:33:17 2022
801   update:
802     >function update(window)
803     >  local key = window:getch()
804     >  if key == string.byte(' ') then
805     >    print('space')
806     >    window:getch()
807     >  end
808     >end
809 - __teliva_timestamp:
810     >Thu Mar 17 21:33:31 2022
811   update:
812     >function update(window)
813     >  local key = string.char(window:getch())
814     >  if key == ' ' then
815     >    print('space')
816     >    window:getch()
817     >  end
818     >end
819 - __teliva_timestamp:
820     >Thu Mar 17 21:36:14 2022
821   __teliva_note:
822     >very initial skeleton
823     >- just render, no eval
824     >- no stack rendering
825     >- no cursor (just appends)
826   update:
827     >function update(window)
828     >  local key = string.char(window:getch())
829     >  if key == ' ' then
830     >    table.insert(program.lines[1].words, {data='', cursor=0})
831     >  else
832     >    local words = program.lines[1].words
833     >    words[#words].data = words[#words].data .. key
834     >  end
835     >end
836 - __teliva_timestamp:
837     >Thu Mar 17 21:39:24 2022
838   main:
839     >function main()
840     >  init_colors()
841     >
842     >  while true do
843     >    render(Window)
844     >    update(Window)
845     >  end
846     >end
847     >
848     >function test_basic_render()
849     >  
850 - __teliva_timestamp:
851     >Thu Mar 17 21:43:05 2022
852   main:
853     >function main()
854     >  init_colors()
855     >
856     >  while true do
857     >    render(Window, Program)
858     >    update(Window, Program)
859     >  end
860     >end
861 - __teliva_timestamp:
862     >Thu Mar 17 21:43:11 2022
863   render:
864     >function render(window, program)
865     >  window:clear()
866     >  for row, line in ipairs(program.lines) do
867     >    for col, word in pairs(line.words) do
868     >      window:addstr(word.data)
869     >      window:addstr(' ')
870     >    end
871     >    window:addstr('\n')
872     >  end
873     >  window:refresh()
874     >end
875     >
876     >function test_render()
877     >  local w = window{
878     >    kbd=kbd(''),
879     >    scr=scr{h=3, w=5},
880     >  }
881     >end
882 - __teliva_timestamp:
883     >Thu Mar 17 21:43:20 2022
884   update:
885     >function update(window, program, key)
886     >  if key == ' ' then
887     >    table.insert(program.lines[1].words, {data='', cursor=0})
888     >  else
889     >    local words = program.lines[1].words
890     >    words[#words].data = words[#words].data .. key
891     >  end
892     >end
893 - __teliva_timestamp:
894     >Thu Mar 17 21:58:10 2022
895   Program:
896     >Program = empty_program()
897 - __teliva_timestamp:
898     >Thu Mar 17 21:58:10 2022
899   empty_program:
900     >function empty_program()
901     >  return {
902     >    cursor_y = 1,
903     >    lines = {
904     >      {
905     >        cursor_x = 1,
906     >        words = {
907     >          {
908     >            cursor = 0,
909     >            data = '',
910     >          },
911     >        },
912     >      },
913     >    },
914     >  }
915     >end
916 - __teliva_timestamp:
917     >Thu Mar 17 22:36:24 2022
918   render:
919     >function render(window, program)
920     >  window:clear()
921     >  for row, line in ipairs(program.lines) do
922     >    for col, word in pairs(line.words) do
923     >      window:addstr(word.data)
924     >      window:addstr(' ')
925     >    end
926     >    window:addstr('\n')
927     >  end
928     >  window:refresh()
929     >end
930 - __teliva_timestamp:
931     >Thu Mar 17 22:41:01 2022
932   __teliva_note:
933     >a passing test
934   main:
935     >function main()
936     >  init_colors()
937     >
938     >  while true do
939     >    render(Window, Program)
940     >    local key = string.char(Window:getch())  -- nodelay can't be set
941     >    update(Window, Program, key)
942     >  end
943     >end
944     >
945     >function test_incomplete()
946     >  local w = window{
947     >    kbd=kbd('ab c'),
948     >    scr=scr{h=3, w=5},
949     >  }
950     >  local prog = empty_program()
951     >  while true do
952     >    render(w, prog)
953     >    local key = w:getch()
954     >    if key == nil then break end
955     >    update(w, prog, string.char(key))
956     >  end
957     >  check_screen(w, 'ab c '..
958     >                  '     '..
959     >                  '     ',
960     >               'test_main_incomplete')
961     >end
962 - __teliva_timestamp: original
963   doc:blurb:
964     >A concatenative programming language with a live-updating debug UI.
965     >by Kartik Agaram and Sumeet Agarwal
966     >
967     >Highly incomplete.
968     >
969     >Previous prototypes:
970     >  - https://archive.org/details/akkartik-2min-2020-12-06
971     >  - https://merveilles.town/@akkartik/105759816342173743