Archived
1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
cli-old/lib/main.rb

133 lines
2.6 KiB
Ruby
Raw Normal View History

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'
require 'widgets/peers/list'
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:46:15 -04:00
Style.default = Style.new
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-22 08:51:01 -04:00
@menu = Widgets::Menu.new 0, 0, nil, Curses.stdscr.maxy
@messenger = Widgets::Messenger.new @menu.width, 0, Curses.stdscr.maxx - @menu.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
end