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 13:03:06 -04:00
|
|
|
require 'events'
|
2017-07-22 05:06:15 -04:00
|
|
|
require 'style'
|
2017-07-21 13:03:06 -04:00
|
|
|
|
2017-07-21 13:40:34 -04:00
|
|
|
# Basic
|
2017-07-22 05:00:28 -04:00
|
|
|
require 'widgets/base'
|
2017-07-21 13:40:34 -04:00
|
|
|
require 'widgets/text'
|
2017-07-22 04:30:26 -04:00
|
|
|
require 'widgets/v_panel'
|
2017-07-21 13:40:34 -04:00
|
|
|
|
2017-07-21 12:37:22 -04:00
|
|
|
require 'widgets/messenger'
|
2017-07-21 13:51:46 -04:00
|
|
|
|
2017-07-21 10:49:39 -04:00
|
|
|
require 'widgets/menu'
|
2017-07-21 13:51:46 -04:00
|
|
|
|
2017-07-21 13:52:45 -04:00
|
|
|
require 'widgets/peers'
|
2017-07-21 14:17:40 -04:00
|
|
|
require 'widgets/peers/list'
|
2017-07-21 14:19:46 -04:00
|
|
|
require 'widgets/peers/search'
|
2017-07-21 13:52:45 -04:00
|
|
|
|
|
|
|
require 'widgets/chat'
|
2017-07-22 04:16:58 -04:00
|
|
|
require 'widgets/chat/info'
|
2017-07-21 14:04:59 -04:00
|
|
|
require 'widgets/chat/history'
|
2017-07-21 14:15:04 -04:00
|
|
|
require 'widgets/chat/new_message'
|
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
|
|
|
|
|
2017-07-22 05:06:15 -04:00
|
|
|
style
|
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-22 05:06:15 -04:00
|
|
|
def style
|
|
|
|
@style ||= Style.new
|
|
|
|
end
|
|
|
|
|
2017-07-21 07:54:07 -04:00
|
|
|
def initials
|
2017-07-21 12:37:22 -04:00
|
|
|
menu_width = Curses.stdscr.maxx / 5
|
|
|
|
messenger_width = Curses.stdscr.maxx - menu_width
|
2017-07-21 10:49:39 -04:00
|
|
|
|
2017-07-21 12:37:22 -04:00
|
|
|
menu_left = 0
|
|
|
|
messenger_left = menu_width
|
2017-07-21 11:19:59 -04:00
|
|
|
|
2017-07-21 12:37:22 -04:00
|
|
|
@menu = Widgets::Menu.new menu_left, 0, menu_width, Curses.stdscr.maxy
|
|
|
|
@messenger = Widgets::Messenger.new messenger_left, 0, messenger_width, Curses.stdscr.maxy
|
2017-07-21 13:36:20 -04:00
|
|
|
|
|
|
|
@messenger.focused = true
|
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 12:37:22 -04:00
|
|
|
@messenger.render
|
2017-07-21 10:01:35 -04:00
|
|
|
|
|
|
|
Curses.refresh
|
|
|
|
end
|
|
|
|
|
2017-07-21 13:03:06 -04:00
|
|
|
def handle(char)
|
|
|
|
case char
|
2017-07-21 13:17:49 -04:00
|
|
|
when Curses::Key::SLEFT
|
|
|
|
@messenger.trigger Events::Window::Left.new
|
|
|
|
when Curses::Key::SRIGHT
|
|
|
|
@messenger.trigger Events::Window::Right.new
|
|
|
|
|
2017-07-21 13:16:43 -04:00
|
|
|
when Curses::Key::UP
|
|
|
|
@messenger.trigger Events::Panel::Up.new
|
|
|
|
when Curses::Key::DOWN
|
|
|
|
@messenger.trigger Events::Panel::Down.new
|
|
|
|
|
2017-07-21 13:03:06 -04:00
|
|
|
when /[a-zA-Z0-9 ]/
|
|
|
|
@messenger.trigger Events::Text::Putc.new char
|
2017-07-21 09:21:43 -04:00
|
|
|
when Curses::Key::LEFT
|
2017-07-21 13:03:06 -04:00
|
|
|
@messenger.trigger Events::Text::Left.new
|
2017-07-21 09:21:43 -04:00
|
|
|
when Curses::Key::RIGHT
|
2017-07-21 13:03:06 -04:00
|
|
|
@messenger.trigger Events::Text::Right.new
|
2017-07-21 09:16:53 -04:00
|
|
|
when Curses::Key::HOME
|
2017-07-21 13:03:06 -04:00
|
|
|
@messenger.trigger Events::Text::Home.new
|
2017-07-21 10:43:08 -04:00
|
|
|
when Curses::Key::END
|
2017-07-21 13:03:06 -04:00
|
|
|
@messenger.trigger Events::Text::End.new
|
2017-07-21 08:59:16 -04:00
|
|
|
when Curses::Key::BACKSPACE
|
2017-07-21 13:03:06 -04:00
|
|
|
@messenger.trigger Events::Text::Backspace.new
|
2017-07-21 09:52:28 -04:00
|
|
|
when Curses::Key::DC
|
2017-07-21 13:03:06 -04:00
|
|
|
@messenger.trigger Events::Text::Delete.new
|
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
|