Beautify examples/client-cqchat.lua

This commit is contained in:
Adrian Perez de Castro
2016-07-09 00:32:25 +03:00
parent c2de4bfd85
commit bb23740c1c

View File

@@ -101,18 +101,20 @@ local function main(tty, client, username, password)
local cq = cqueues.new() local cq = cqueues.new()
local ok, err, obj = cq:wrap(function () local ok, err, obj = cq:wrap(function ()
print(string.format("Terminal size: %dx%d", tty:size()))
client:login_with_password(username, password) client:login_with_password(username, password)
local running = true
local client_should_stop = function () return not running end
local clientqueue = cq:wrap(function () local clientqueue = cq:wrap(function ()
client:sync() client:sync(client_should_stop)
end) end)
local current_room local current_room
while true do while running do
local line = "" local line = ""
while true do while true do
io.stdout:write(string.format("\r[%s] %s", io.stdout:write(string.format("\r[%s] %s",
current_room and current_room.room_id or "*", current_room and current_room:get_alias_or_id() or "*",
line)) line))
io.stdout:flush() io.stdout:flush()
@@ -141,14 +143,14 @@ local function main(tty, client, username, password)
if client.rooms[params] then if client.rooms[params] then
current_room = client.rooms[params] current_room = client.rooms[params]
else else
print("\r ! No such room") print("\r/!\\ No such room")
end end
end end
else else
if current_room then if current_room then
current_room:send_text(line) current_room:send_text(line)
else else
print("\r ! Choose a room using '/room <room_id>'") print("\r/!\\ Choose a room using '/room <room_id>'")
end end
end end
end end
@@ -159,7 +161,7 @@ local function main(tty, client, username, password)
end end
local function print_room_message(room, sender, message, event) 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 end
if #arg ~= 3 then if #arg ~= 3 then
@@ -167,16 +169,24 @@ if #arg ~= 3 then
os.exit(1) os.exit(1)
end 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) :hook("logged-in", function (client)
print("\r * Logged in as " .. client.user_id) print("\r * Logged in as " .. client.user_id)
end) end)
:hook("joined", function (client, room) :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) room:hook("message", print_room_message)
end) end)
:hook("left", function (client, room) :hook("left", function (client, room)
print("\r * Left room " .. room.room_id) print("\r * Left room " .. room.room_id)
end) end)
tty:wrap(main, client, arg[2], arg[3]) tty:wrap(main, client, arg[2], arg[3])