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 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 <room_id>'")
print("\r/!\\ Choose a room using '/room <room_id>'")
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])