Beautify examples/client-cqchat.lua
This commit is contained in:
@@ -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[K[%s] %s",
|
io.stdout:write(string.format("\r[K[1;1m[%s][0;0m %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[K ! No such room")
|
print("\r[K[1;31m/!\\[0;0m 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[K ! Choose a room using '/room <room_id>'")
|
print("\r[K[1;31m/!\\[0;0m 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[[37m%s[0m] <[36m%s[0m> %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[K * Logged in as " .. client.user_id)
|
print("\r[K[1;32m *[0;0m Logged in as " .. client.user_id)
|
||||||
end)
|
end)
|
||||||
:hook("joined", function (client, room)
|
:hook("joined", function (client, room)
|
||||||
print("\r[K * Joined room " .. room.room_id)
|
room:update_aliases()
|
||||||
|
local extra = ""
|
||||||
|
if #room.aliases > 0 then
|
||||||
|
extra = " (" .. table.concat(room.aliases, ", ") .. ")"
|
||||||
|
end
|
||||||
|
print("\r[K[1;32m *[0;0m 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[K * Left room " .. room.room_id)
|
print("\r[K[1;32m *[0;0m Left room " .. room.room_id)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
tty:wrap(main, client, arg[2], arg[3])
|
tty:wrap(main, client, arg[2], arg[3])
|
||||||
|
|||||||
Reference in New Issue
Block a user