at main 2.6 kB view raw
1local comlink = require('comlink') 2local watcher = require('watcher') 3 4local function run_command_and_trim(command) 5 -- Execute the command and capture output 6 local handle = io.popen(command) 7 assert(handle, "Failed to execute command") 8 9 -- Read the output 10 local result = handle:read("*a") 11 local success, exit_type, exit_code = handle:close() 12 13 -- Check if command executed successfully 14 assert(success, "Command failed with exit code: " .. (exit_code or "unknown")) 15 16 -- Trim whitespace (including newlines) 17 if result then 18 result = result:match("^%s*(.-)%s*$") 19 end 20 21 return result 22end 23 24local config = { 25 server = 'chat.sr.ht', 26 user = 'reykjalin', 27 nick = 'reykjalin', 28 password = run_command_and_trim('op read "op://Private/chat.sr.ht app password/password"'), 29 tls = true, 30 real_name = 'Kristófer Reykjalín', 31} 32 33-- Set channel watch config to receive notifications on any message in the provided channels. 34local connection = comlink.connect(config) 35connection.on_message = watcher.on_message 36 37watcher.channels = { 38 "#comlink", 39 "#ghostty", 40 "#vaxis", 41} 42 43-- Keybindings. 44comlink.bind("ctrl+c", "quit") 45comlink.bind("ctrl+n", "next-channel") 46comlink.bind("ctrl+j", "next-channel") 47comlink.bind("ctrl+p", "prev-channel") 48comlink.bind("ctrl+l", "redraw") 49 50local function mark_read(_) 51 local maybe_channel = comlink.selected_channel() 52 if maybe_channel then 53 maybe_channel:mark_read() 54 end 55end 56comlink.bind("ctrl+r", mark_read) 57 58-- /tableflip and /shrug. 59local function tableflip(_) 60 local channel = comlink.selected_channel() 61 if channel then 62 channel:send_msg("(╯°□°)╯︵ ┻━┻") 63 end 64end 65 66local function shrug(_) 67 local channel = comlink.selected_channel() 68 if channel then 69 channel:send_msg("¯\\_(ツ)_/¯") 70 end 71end 72 73comlink.add_command("tableflip", tableflip) 74comlink.add_command("shrug", shrug) 75 76-- /fmt <text>. 77local function replace(string, old, new) 78 local s = string 79 local search_start_idx = 1 80 81 while true do 82 local start_idx, end_idx = s:find(old, search_start_idx, true) 83 if not start_idx then 84 break 85 end 86 87 local postfix = s:sub(end_idx + 1) 88 s = s:sub(1, (start_idx - 1)) .. new .. postfix 89 90 search_start_idx = -1 * postfix:len() 91 end 92 93 return s 94end 95 96local function format(cmdline) 97 local channel = comlink.selected_channel() 98 if channel then 99 local s = replace(cmdline, "**", "\x02") 100 s = replace(s, "*", "\x1D") 101 s = replace(s, "~", "\x1E") 102 s = replace(s, "_", "\x1F") 103 s = replace(s, "<blue>", "\x0302") 104 s = replace(s, "</blue>", "\x03") 105 channel:send_msg(s) 106 end 107end 108comlink.add_command("fmt", format)