Skip to content

Commit 8aa137e

Browse files
committed
Add pad_left and use in log()
1 parent 4c8436d commit 8aa137e

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

doc/modules/pico8lib.strings.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ <h2><a href="#Miscellaneous">Miscellaneous </a></h2>
167167
<td class="summary">Check if a string starts with a given prefix
168168
returns true if it does, false otherwise</td>
169169
</tr>
170+
<tr>
171+
<td class="name" nowrap><a href="#pad_left">pad_left (s, l, p)</a></td>
172+
<td class="summary">Pad a string on the left</td>
173+
</tr>
170174
</table>
171175
<h2><a href="#tostr">tostr </a></h2>
172176
<table class="function_list">
@@ -662,6 +666,35 @@ <h3>Parameters:</h3>
662666

663667

664668

669+
</dd>
670+
<dt>
671+
<a name = "pad_left"></a>
672+
<strong>pad_left (s, l, p)</strong>
673+
</dt>
674+
<dd>
675+
Pad a string on the left
676+
677+
678+
<h3>Parameters:</h3>
679+
<ul>
680+
<li><span class="parameter">s</span>
681+
<span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
682+
String to pad
683+
</li>
684+
<li><span class="parameter">l</span>
685+
<span class="types"><span class="type">number</span></span>
686+
Length to pad to
687+
</li>
688+
<li><span class="parameter">p</span>
689+
<span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
690+
Padding character
691+
</li>
692+
</ul>
693+
694+
695+
696+
697+
665698
</dd>
666699
</dl>
667700
<h2 class="section-header "><a name="tostr"></a>tostr </h2>

pico8lib/log.lua

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ local P8LIBLOGUTC = false
2424
-- @tparam string prefix A prefix to prepend to the message
2525
local function log (any, prefix)
2626
tz = P8LIBLOGUTC and 80 or 90
27-
seconds = stat(tz+5)
2827
printh(stat(tz) .. "-" ..
29-
stat(tz+1) .. "-" ..
30-
stat(tz+2) .. " " ..
31-
stat(tz+3) .. ":" ..
32-
stat(tz+4) .. ":" ..
33-
(seconds < 10 and "0" .. seconds or seconds)
28+
pad_left(tostr(stat(tz+1)), 2, "0") .. "-" ..
29+
pad_left(tostr(stat(tz+2)), 2, "0") .. " " ..
30+
pad_left(tostr(stat(tz+3)), 2, "0") .. ":" ..
31+
pad_left(tostr(stat(tz+4)), 2, "0") .. ":" ..
32+
pad_left(tostr(stat(tz+5)), 2, "0")
3433
.. " " .. (prefix or "LOG") .. " - " .. tostr(any))
3534
end
3635

pico8lib/strings.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,16 @@ end
379379
local function starts_with(str, prefix)
380380
return ssub(str, 0, #prefix) == prefix
381381
end
382+
383+
384+
--- Pad a string on the left
385+
-- @tparam string s String to pad
386+
-- @tparam number l Length to pad to
387+
-- @tparam string p Padding character
388+
local function pad_left(s, l, p)
389+
while #s < l do
390+
s = p .. s
391+
end
392+
return s
393+
end
394+

0 commit comments

Comments
 (0)