extra: init function "printfmt"

Roughly equivalent to:

    io.stdout:write(string.format(fstring, ...), '\n')

Only possible because of the newly bundled string.format replacement.
This commit is contained in:
Jeremy Baxter 2024-03-24 20:22:06 +13:00
parent 3fa54b0cfc
commit 0aa68af3d7

View file

@ -12,11 +12,37 @@
* @module extra * @module extra
*/ */
#include <string.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include <lua/lauxlib.h> #include <lua/lauxlib.h>
#include <lua/lua.h> #include <lua/lua.h>
#include "util.h"
static int
extra_printfmt(lua_State *L)
{
const char *outstring;
int i, nargs;
nargs = lua_gettop(L);
lua_pushcfunction(L, lstrfmt);
for (i = 1; i <= nargs; i++) {
lua_pushvalue(L, i);
}
lua_call(L, nargs, 1);
outstring = lua_tostring(L, -1);
lua_pop(L, 1);
write(1, outstring, strlen(outstring));
write(1, "\n", 1);
return 0;
}
/*** /***
* Waits the specified amount of seconds. * Waits the specified amount of seconds.
* *
@ -57,6 +83,7 @@ extra_sleep(lua_State *L)
/* clang-format off */ /* clang-format off */
static const luaL_Reg extlib[] = { static const luaL_Reg extlib[] = {
{"printfmt", extra_printfmt},
{"sleep", extra_sleep}, {"sleep", extra_sleep},
{NULL, NULL} {NULL, NULL}
}; };