slightly more robust on-disk format

Looks like Lua supports a little bit of programmability in its
multi-line string literals. Even though I can't find this documented
anywhere.
This commit is contained in:
Kartik K. Agaram 2021-11-14 00:49:40 -08:00
parent b64a21771e
commit 5be743324c
3 changed files with 33 additions and 33 deletions

View File

@ -1,7 +1,7 @@
teliva_program = {
window = [[window = curses.stdscr()]],
n = [[n = 0]],
render = [[
window = [==[window = curses.stdscr()]==],
n = [==[n = 0]==],
render = [==[
function render(window)
window:clear()
window:attron(curses.A_BOLD)
@ -12,17 +12,17 @@ function render(window)
window:attroff(curses.A_BOLD)
curses.refresh()
end
]],
menu = [[menu = {Enter="increment"}]],
update = [[
]==],
menu = [==[menu = {Enter="increment"}]==],
update = [==[
function update(window)
local key = curses.getch()
if key == 10 then
n = n+1
end
end
]],
main = [[
]==],
main = [==[
function main()
for i=1,7 do
curses.init_pair(i, 0, i)
@ -33,5 +33,5 @@ function main()
update(window)
end
end
]],
]==],
}

View File

@ -1,5 +1,5 @@
teliva_program = {
render = [[function render(window)
render = [==[function render(window)
window:clear()
local lines, cols = window:getmaxyx()
local line = math.floor(lines/2)
@ -8,16 +8,16 @@ teliva_program = {
render_tower(window, line, i*col, i, t)
end
curses.refresh()
end]],
lines = [[function lines(window)
end]==],
lines = [==[function lines(window)
local lines, cols = window:getmaxyx()
return lines
end]],
pop = [[function pop(array)
end]==],
pop = [==[function pop(array)
return table.remove(array)
end]],
window = [[window = curses.stdscr()]],
render_tower = [[function render_tower(window, line, col, tower_index, tower)
end]==],
window = [==[window = curses.stdscr()]==],
render_tower = [==[function render_tower(window, line, col, tower_index, tower)
window:attron(curses.A_BOLD)
window:mvaddch(line+2, col, string.char(96+tower_index))
window:attroff(curses.A_BOLD)
@ -34,9 +34,9 @@ end]],
window:attroff(curses.color_pair(7))
line = line - 1
end
end]],
tower = [[tower = {{6, 5, 4, 3, 2}, {}, {}}]],
render_disk = [[function render_disk(window, line, col, size)
end]==],
tower = [==[tower = {{6, 5, 4, 3, 2}, {}, {}}]==],
render_disk = [==[function render_disk(window, line, col, size)
col = col-size+1
for i=1,size do
window:attron(curses.color_pair(size))
@ -44,8 +44,8 @@ end]],
window:attroff(curses.color_pair(size))
col = col+2
end
end]],
main = [[function main()
end]==],
main = [==[function main()
for i=1,7 do
curses.init_pair(i, 0, i)
end
@ -55,27 +55,27 @@ end]],
update(window)
end
end
]],
len = [[function len(array)
]==],
len = [==[function len(array)
local result = 0
for k in pairs(array) do
result = result+1
end
return result
end]],
update = [[function update(window)
end]==],
update = [==[function update(window)
window:mvaddstr(lines(window)-2, 5, "tower to remove top disk from? ")
local from = curses.getch() - 96
window:mvaddstr(lines(window)-1, 5, "tower to stack it on? ")
local to = curses.getch() - 96
make_move(from, to)
end]],
make_move = [[function make_move(from, to)
end]==],
make_move = [==[function make_move(from, to)
local disk = pop(tower[from])
table.insert(tower[to], disk)
end]],
cols = [[function cols(window)
end]==],
cols = [==[function cols(window)
local lines, cols = window:getmaxyx()
return cols
end]],
end]==],
}

View File

@ -345,9 +345,9 @@ static void save_image (lua_State *L) {
for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) {
const char* key = lua_tostring(L, -2);
const char* value = lua_tostring(L, -1);
fprintf(fp, " %s = [[", key);
fprintf(fp, " %s = [==[", key);
fprintf(fp, "%s", value);
fprintf(fp, "]],\n");
fprintf(fp, "]==],\n");
}
fprintf(fp, "}\n");
fclose(fp);