From 364d0e4074c4d7f506ea0440a87b00634ea82969 Mon Sep 17 00:00:00 2001 From: RadsammyT Date: Tue, 16 Jun 2026 19:54:25 -0400 Subject: [PATCH] os.clock -> ly.clock --- res/example.lua | 12 ++++++------ src/animations/Lua.zig | 20 +++++++++++++++++++- src/animations/ly.lua | 1 - src/main.zig | 1 + 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/res/example.lua b/res/example.lua index 378f2de..ab01f0d 100644 --- a/res/example.lua +++ b/res/example.lua @@ -6,13 +6,13 @@ -- { -- height: number -- The height of the terminal -- width: number -- The width of the terminal --- frame_delay: number -- The amount of frames to delay an update to the animation -- putCell(byte, fg, bg, x, y) -- Put the cell a buffer. This buffer will be drawn -- on screen after draw() is called. It will not clear -- itself automatically, see `ly.clear()`. -- All arguments to this function are numbers, and -- must be in the unsigned 32-bit integer range: 0 to 2^32-1. -- clear() -- Clear the buffer. +-- clock() -- The time, in microseconds. -- } -- -- A function named `draw()` must be declared in the script. This is ran every frame. @@ -24,7 +24,7 @@ local SQUARE_HEIGHT = 5 local SQUARE_COUNT = 25 -local FPS = 120 +local FPS = 60 local squares = {} @@ -42,12 +42,12 @@ for i = 0, SQUARE_COUNT-1 do } end -local timer = os.clock() +local timer = ly.clock() function draw() -- Rather than progressing the animation by frame, do it based on - -- seconds, like from os.clock(). - if timer + (1/FPS) < os.clock() then + -- seconds, like from ly.clock(). + if timer + ((1/FPS)*1000000)< ly.clock() then ly.clear() for i, v in ipairs(squares) do v.x = v.x + v.vx @@ -62,6 +62,6 @@ function draw() if v.y <= 0 then v.vy = 1; v.color = math.random(0xFFFFFF) end if v.y + SQUARE_HEIGHT >= ly.height-1 then v.vy = -1; v.color = math.random(0xFFFFFF) end end - timer = os.clock() + timer = ly.clock() end end diff --git a/src/animations/Lua.zig b/src/animations/Lua.zig index 5bb7fa1..4d3efff 100644 --- a/src/animations/Lua.zig +++ b/src/animations/Lua.zig @@ -6,6 +6,7 @@ const TerminalBuffer = ly_ui.TerminalBuffer; const Cell = ly_ui.Cell; const Allocator = std.mem.Allocator; const InfoLine = @import("../components/InfoLine.zig"); +const Lang = @import("../config/Lang.zig"); const zlua = @import("zlua"); @@ -28,6 +29,8 @@ info_line: *InfoLine, fg: u32, bg: u32, +lang: *Lang, + lua_error: bool = false, lua_error_logged: bool = false, lua_str: ?[:0]const u8 = null, @@ -43,6 +46,7 @@ pub fn init( info_line: *InfoLine, fg: u32, bg: u32, + lang: *Lang, ) !Lua { var self: Lua = .{ .lua = try zlua.Lua.init(alloc), @@ -58,13 +62,13 @@ pub fn init( .info_line = info_line, .fg = fg, .bg = bg, + .lang = lang, }; // exclude IO and debug libraries self.lua.openBase(); self.lua.openBit(); self.lua.openMath(); - self.lua.openOS(); self.lua.openString(); self.lua.openTable(); @@ -72,6 +76,7 @@ pub fn init( const zf = std.mem.concatWithSentinel(alloc, u8, &[1][]const u8{file}, 0) catch |e| { self.log.err(self.io, "lua", "failed to allocate file path: {}", .{e}) catch unreachable; self.lua_str = "failed to allocate file path!"; + self.info_line.addMessage(lang.err_alloc, self.bg, self.fg) catch {}; self.lua_error = true; break :file_loading; }; @@ -85,6 +90,11 @@ pub fn init( break :file_loading; }; self.propogateTerminalBounds(); + _ = self.lua.getGlobal("ly"); + _ = self.lua.pushString("clock"); + self.lua.pushFunction(luaLyClock); + self.lua.setTable(-3); + self.lua.setGlobal("ly"); self.lua.doFile(zf) catch { const errorStr = self.lua.toString(-1) catch unreachable; self.lua_str = try self.allocator.dupeSentinel(u8, errorStr, 0); @@ -93,6 +103,7 @@ pub fn init( break :file_loading; }; } + return self; } @@ -255,3 +266,10 @@ fn propogateTerminalBounds(self: *Lua) void { self.lua.setGlobal("ly"); } } + +fn luaLyClock(state: ?*zlua.LuaState) callconv(.c) c_int { + var threaded = std.Io.Threaded.init_single_threaded; + const lua: *zlua.Lua = @ptrCast(@alignCast(state orelse unreachable)); + lua.pushInteger(std.Io.Timestamp.now(threaded.io(), .real).toMicroseconds()); + return 1; +} diff --git a/src/animations/ly.lua b/src/animations/ly.lua index 763ec7b..c45812b 100644 --- a/src/animations/ly.lua +++ b/src/animations/ly.lua @@ -1,5 +1,4 @@ ly = { - frame_delay = 0, width = 0, height = 0, } diff --git a/src/main.zig b/src/main.zig index 66cce9f..cf87637 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1111,6 +1111,7 @@ pub fn main(init: std.process.Init) !void { &state.info_line, state.config.error_fg, state.config.error_bg, + &state.lang, ); animation = lua.widget(); },