From 948b43389c1f0c328154f8a6dfc91b90db3ba151 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Fri, 7 Mar 2025 16:14:17 -0600 Subject: [PATCH] lua: add channel:insert_text --- docs/comlink_channel.3.scd | 5 +++++ src/lua.zig | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/docs/comlink_channel.3.scd b/docs/comlink_channel.3.scd index 0b01a7e..b6c1966 100644 --- a/docs/comlink_channel.3.scd +++ b/docs/comlink_channel.3.scd @@ -10,6 +10,8 @@ comlink_channel - a lua type representing an IRC channel *local name = channel:name*() +*channel:insert_text*() + *channel:mark_read*() *channel:send_msg*(_msg_) @@ -18,6 +20,9 @@ comlink_channel - a lua type representing an IRC channel A *channel* represents an IRC channel. +*channel:insert_text* + Insert text into the text input at the cursor position + *channel:mark_read* Mark this channel as read. This will update the unread indicator to the position of the last read message. diff --git a/src/lua.zig b/src/lua.zig index bb5b571..066efae 100644 --- a/src/lua.zig +++ b/src/lua.zig @@ -459,6 +459,7 @@ const Channel = struct { fn initTable(lua: *Lua, channel: *irc.Channel) void { const fns = [_]ziglua.FnReg{ .{ .name = "send_msg", .func = ziglua.wrap(Channel.sendMsg) }, + .{ .name = "insert_text", .func = ziglua.wrap(Channel.insertText) }, .{ .name = "name", .func = ziglua.wrap(Channel.name) }, .{ .name = "mark_read", .func = ziglua.wrap(Channel.markRead) }, }; @@ -496,6 +497,22 @@ const Channel = struct { return 0; } + fn insertText(lua: *Lua) i32 { + lua.argCheck(lua.isTable(1), 1, "expected a table"); // [table] + lua.argCheck(lua.isString(2), 2, "expected a string"); // [table,string] + const msg = lua.toString(2) catch unreachable; + lua.pop(1); // [table] + const lua_type = lua.getField(1, "_ptr"); // [table, lightuserdata] + lua.argCheck(lua_type == .light_userdata, 2, "expected lightuserdata"); + const channel = lua.toUserdata(irc.Channel, 2) catch unreachable; + lua.pop(1); // [] + + channel.text_field.insertSliceAtCursor(msg) catch { + lua.raiseErrorStr("couldn't insert text", .{}); + }; + return 0; + } + fn name(lua: *Lua) i32 { lua.argCheck(lua.isTable(1), 1, "expected a table"); // [table] const lua_type = lua.getField(1, "_ptr"); // [table, lightuserdata] -- 2.51.2