From bb23740c1c95d8b9e241ae1bfa3d5eba61916dfe Mon Sep 17 00:00:00 2001 From: Adrian Perez de Castro Date: Sat, 9 Jul 2016 00:32:25 +0300 Subject: [PATCH] Beautify examples/client-cqchat.lua --- examples/client-cqchat.lua | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/examples/client-cqchat.lua b/examples/client-cqchat.lua index 9f5c711..2d748bb 100644 --- a/examples/client-cqchat.lua +++ b/examples/client-cqchat.lua @@ -101,18 +101,20 @@ local function main(tty, client, username, password) local cq = cqueues.new() local ok, err, obj = cq:wrap(function () - print(string.format("Terminal size: %dx%d", tty:size())) client:login_with_password(username, password) + + local running = true + local client_should_stop = function () return not running end local clientqueue = cq:wrap(function () - client:sync() + client:sync(client_should_stop) end) local current_room - while true do + while running do local line = "" while true do - io.stdout:write(string.format("\r[%s] %s", - current_room and current_room.room_id or "*", + io.stdout:write(string.format("\r[%s] %s", + current_room and current_room:get_alias_or_id() or "*", line)) io.stdout:flush() @@ -141,14 +143,14 @@ local function main(tty, client, username, password) if client.rooms[params] then current_room = client.rooms[params] else - print("\r ! No such room") + print("\r/!\\ No such room") end end else if current_room then current_room:send_text(line) else - print("\r ! Choose a room using '/room '") + print("\r/!\\ Choose a room using '/room '") end end end @@ -159,7 +161,7 @@ local function main(tty, client, username, password) end local function print_room_message(room, sender, message, event) - print(string.format("\rK[%s] <%s> %s", room.room_id, sender, message.body)) + print(string.format("\rK[%s] <%s> %s", room.room_id, sender, message.body)) end if #arg ~= 3 then @@ -167,16 +169,24 @@ if #arg ~= 3 then os.exit(1) end -local client = matrix.client(arg[1]) +-- +-- Force usage of "chttp", the cqueues-based HTTP client library +-- +local client = matrix.client(arg[1], nil, "chttp") :hook("logged-in", function (client) - print("\r * Logged in as " .. client.user_id) + print("\r * Logged in as " .. client.user_id) end) :hook("joined", function (client, room) - print("\r * Joined room " .. room.room_id) + room:update_aliases() + local extra = "" + if #room.aliases > 0 then + extra = " (" .. table.concat(room.aliases, ", ") .. ")" + end + print("\r * Joined room " .. room.room_id .. extra) room:hook("message", print_room_message) end) :hook("left", function (client, room) - print("\r * Left room " .. room.room_id) + print("\r * Left room " .. room.room_id) end) tty:wrap(main, client, arg[2], arg[3])