23 lines
710 B
Ruby
23 lines
710 B
Ruby
# frozen_string_literal: true
|
|
|
|
module UI
|
|
module Windows
|
|
module_function
|
|
|
|
def chat(app, services:)
|
|
username = app.context[:username] || 'Anonymous'
|
|
BBS::Windows::Stream.open(app,
|
|
title: ' Live Chat ',
|
|
initial_lines: services[:chat].history.map { |l| format_chat_line(l) },
|
|
placeholder: 'Type a message and press Enter…',
|
|
on_submit: ->(text, _a) { services[:chat].broadcast(username, text.strip) },
|
|
on_poll: ->(a) { services[:chat].drain(a.session_id).map { |l| format_chat_line(l) } }
|
|
)
|
|
end
|
|
|
|
def format_chat_line(line)
|
|
" \e[2;37m#{line.timestamp}\e[0m \e[1;33m#{line.username}\e[0m: #{line.text}"
|
|
end
|
|
end
|
|
end
|