Compare commits

..

5 Commits

Author SHA1 Message Date
179ee35985
Use fmt on title screen. 2023-01-01 12:53:16 -08:00
367f03be4a
fix section before first "%"
oops, need to special-case the first part of the split list.

amusingly, I don't need to special-case zero-length format strings because that will skip the entire loop and output "", which seems correct. (I am also not special-casing zero-length segments because `s[1] == nil` will go into the error handler and that seems fine.
2023-01-01 12:50:35 -08:00
2ce259304d
fix double-consume, add %!
I discovered your tostring function for debugging, might as well make it reachable from fmt.  I also realized I forgot to convert to using "p" when I introduced it so I fixed that
2023-01-01 12:42:37 -08:00
e0ebb035e1
fix sprintf and rename to fmt
the "split on %" strategy makes parsing "%%" complicated, so the "actually literally %" placeholder is now "%~" rather than "%%". since thsi resembles a real sprintf even less now I renamed it to "fmt".

also actually closes the `if m == "~"` block. (which was `if m == "%"` before this patch)
2023-01-01 12:37:11 -08:00
b0bca27239
implement fake sprintf
this spends extra tokens to handle "invalid format character" to catch using a % where %% is intended. this is designed to grow with further format chars if needed; I have ideas for not-exactly-POSIX %d, %x, %h, %!, %#, %c, and possibly others but there is absolutely no reason to spend tokens on these things until we need them. (that said, existing debugging output might benefit from some of these other formats, but that debug code is commented out, so maybe nevert.)
2023-01-01 12:33:16 -08:00

View File

@ -38,6 +38,39 @@ function cycle(tbl,period)
return tbl[t()%period*#tbl\period+1] return tbl[t()%period*#tbl\period+1]
end end
-- fake sprintf function
-- %~ for literal "%"
-- %v for param
-- %! for tostring(param)
-- which dumps tables
function fmt(f, ...)
local out, i = "", 0
for s in all(split(f,"%")) do
if i == 0 then
-- before first format directive
out ..= s
i = 1
else
local m = s[1]
if m == "~" then
out ..= "%"
else
local p = select(i,...)
i+=1
if m == "v" then
out ..= p
elseif m == "!" then
out ..= tostring(p)
else
out ..= "(?!:"..m..":"..tostring(p)..")"
end
end
out ..=sub(s,2)
end
end
return out
end
mnames={} mnames={}
function names(root) function names(root)
local n=mnames[root] local n=mnames[root]
@ -269,7 +302,7 @@ function title:draw()
print("pyrex",32,73,7) print("pyrex",32,73,7)
print("[nyeogmi]",62,73,7) print("[nyeogmi]",62,73,7)
print("kistaro",32,79,7) print("kistaro",32,79,7)
local lvlstr = "⬅️ "..start_level.." ➡️" local lvlstr = fmt("⬅️ %v ➡️",start_level)
print(lvlstr,50,91,1) print(lvlstr,50,91,1)
print(lvlstr,51,90,blinkcol) print(lvlstr,51,90,blinkcol)
end end