# frozen_string_literal: true
require 'thread'
require 'faker'
require 'screen'
class Main
INITIAL_STATE = {
focus: :sidebar,
sidebar: {
focus: :menu,
menu: {
active: 0,
top: 0,
items: 1.upto(Curses.stdscr.maxy).map do
{
name: Faker::Name.name,
online: [false, true].sample,
}
end,
},
chat: {
focus: :new_message,
info: {
public_key: SecureRandom.hex(32),
new_message: {
text: '',
cursor_pos: 0,
history: {
messages: 1.upto(100).map do
out: rand <= 0.2,
time: Faker::Time.forward,
text: Faker::Lorem.sentence,
}.freeze
def self.inherited(_base)
raise "#{self} is final"
end
def self.mutex
(@mutex ||= Mutex.new).tap { freeze }
def initialize
raise "#{self.class} is singleton" unless self.class.mutex.try_lock
call
private
def call
before_loop
loop do
before_iteration
sleep
after_iteration
after_loop
def sleep
super 0.01
def before_loop
@screen = Screen.new
Style.default = Style.new
def after_loop
@screen.close
def before_iteration
@screen.render
def after_iteration
@screen.poll