Files
bbs-server/lib/ui/windows/chat_window.rb
2026-05-13 21:00:54 +02:00

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