2017-07-21 02:48:04 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'thread'
|
|
|
|
require 'curses'
|
|
|
|
|
2017-07-21 11:23:43 -04:00
|
|
|
require 'faker'
|
|
|
|
|
2017-07-21 10:49:39 -04:00
|
|
|
require 'widgets/menu'
|
2017-07-21 10:43:08 -04:00
|
|
|
require 'widgets/peers'
|
2017-07-21 11:19:59 -04:00
|
|
|
require 'widgets/chat'
|
2017-07-21 11:11:03 -04:00
|
|
|
require 'widgets/search'
|
|
|
|
require 'widgets/list'
|
2017-07-21 10:00:01 -04:00
|
|
|
|
2017-07-21 02:48:04 -04:00
|
|
|
class Main
|
|
|
|
def self.inherited(_base)
|
|
|
|
raise "#{self} is final"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.mutex
|
|
|
|
(@mutex ||= Mutex.new).tap { freeze }
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
raise "#{self.class} is singleton" unless self.class.mutex.try_lock
|
|
|
|
call
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def call
|
|
|
|
before_loop
|
|
|
|
loop do
|
|
|
|
before_iteration
|
|
|
|
sleep
|
|
|
|
after_iteration
|
|
|
|
end
|
|
|
|
after_loop
|
|
|
|
end
|
|
|
|
|
|
|
|
def sleep
|
|
|
|
super 0.01
|
|
|
|
end
|
|
|
|
|
|
|
|
def before_loop
|
|
|
|
Curses.init_screen
|
|
|
|
Curses.start_color
|
|
|
|
Curses.noecho # do no echo input
|
|
|
|
Curses.curs_set 0 # invisible cursor
|
|
|
|
Curses.timeout = 0 # non-blocking input
|
|
|
|
Curses.stdscr.keypad = true
|
|
|
|
|
|
|
|
Curses.init_pair 1, Curses::COLOR_WHITE, Curses::COLOR_BLACK
|
|
|
|
Curses.init_pair 2, Curses::COLOR_BLACK, Curses::COLOR_WHITE
|
2017-07-21 08:54:09 -04:00
|
|
|
Curses.init_pair 3, Curses::COLOR_BLUE, Curses::COLOR_BLACK
|
2017-07-21 09:12:51 -04:00
|
|
|
Curses.init_pair 4, Curses::COLOR_BLACK, Curses::COLOR_BLUE
|
2017-07-21 11:00:11 -04:00
|
|
|
Curses.init_pair 5, Curses::COLOR_BLACK, Curses::COLOR_CYAN
|
2017-07-21 07:41:05 -04:00
|
|
|
|
|
|
|
initials
|
2017-07-21 02:48:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def after_loop
|
|
|
|
Curses.close_screen
|
|
|
|
end
|
|
|
|
|
2017-07-21 07:41:05 -04:00
|
|
|
def before_iteration
|
|
|
|
render
|
|
|
|
end
|
2017-07-21 02:48:04 -04:00
|
|
|
|
|
|
|
def after_iteration
|
|
|
|
loop do
|
|
|
|
event = Curses.getch
|
|
|
|
break if event.nil?
|
|
|
|
handle event
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-21 07:54:07 -04:00
|
|
|
def initials
|
2017-07-21 11:06:21 -04:00
|
|
|
menu_width = Curses.stdscr.maxx / 5
|
2017-07-21 11:11:40 -04:00
|
|
|
peers_width = Curses.stdscr.maxx / 4
|
2017-07-21 11:19:59 -04:00
|
|
|
chat_width = Curses.stdscr.maxx - menu_width - peers_width
|
2017-07-21 10:49:39 -04:00
|
|
|
|
2017-07-21 11:19:59 -04:00
|
|
|
menu_left = 0
|
|
|
|
peers_left = menu_width
|
|
|
|
chat_left = menu_width + peers_width
|
|
|
|
|
|
|
|
@menu = Widgets::Menu.new menu_left, 0, menu_width, Curses.stdscr.maxy
|
|
|
|
@peers = Widgets::Peers.new peers_left, 0, peers_width, Curses.stdscr.maxy
|
|
|
|
@chat = Widgets::Chat.new chat_left, 0, chat_width, Curses.stdscr.maxy
|
2017-07-21 07:54:07 -04:00
|
|
|
end
|
2017-07-21 07:41:05 -04:00
|
|
|
|
2017-07-21 10:01:35 -04:00
|
|
|
def render
|
|
|
|
Curses.clear
|
|
|
|
|
2017-07-21 11:00:11 -04:00
|
|
|
@menu.render
|
2017-07-21 10:43:08 -04:00
|
|
|
@peers.render
|
2017-07-21 11:19:59 -04:00
|
|
|
@chat.render
|
2017-07-21 10:01:35 -04:00
|
|
|
|
|
|
|
Curses.refresh
|
|
|
|
end
|
|
|
|
|
2017-07-21 08:03:53 -04:00
|
|
|
def handle(event)
|
|
|
|
case event
|
2017-07-21 08:57:38 -04:00
|
|
|
when /[a-zA-Z0-9 _-]/
|
2017-07-21 10:43:08 -04:00
|
|
|
@peers.putc event
|
2017-07-21 09:21:43 -04:00
|
|
|
when Curses::Key::LEFT
|
2017-07-21 10:43:08 -04:00
|
|
|
@peers.left
|
2017-07-21 09:21:43 -04:00
|
|
|
when Curses::Key::RIGHT
|
2017-07-21 10:43:08 -04:00
|
|
|
@peers.right
|
2017-07-21 09:16:53 -04:00
|
|
|
when Curses::Key::HOME
|
2017-07-21 10:43:08 -04:00
|
|
|
@peers.home
|
|
|
|
when Curses::Key::END
|
|
|
|
@peers.endk
|
2017-07-21 08:59:16 -04:00
|
|
|
when Curses::Key::BACKSPACE
|
2017-07-21 10:43:08 -04:00
|
|
|
@peers.backspace
|
2017-07-21 09:52:28 -04:00
|
|
|
when Curses::Key::DC
|
2017-07-21 10:43:08 -04:00
|
|
|
@peers.delete
|
2017-07-21 08:03:53 -04:00
|
|
|
when Curses::Key::UP
|
2017-07-21 10:43:08 -04:00
|
|
|
@peers.up
|
2017-07-21 08:03:53 -04:00
|
|
|
when Curses::Key::DOWN
|
2017-07-21 10:43:08 -04:00
|
|
|
@peers.down
|
2017-07-21 08:25:17 -04:00
|
|
|
end
|
2017-07-21 08:03:53 -04:00
|
|
|
end
|
2017-07-21 08:38:11 -04:00
|
|
|
end
|