From 90d33ba25a651e1bbeaa52c2393e5cbee6db8834 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Mon, 24 Jul 2017 12:48:22 +0000 Subject: [PATCH] Add class Screen --- lib/main.rb | 91 +++------------------------------------------ lib/screen.rb | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 86 deletions(-) create mode 100644 lib/screen.rb diff --git a/lib/main.rb b/lib/main.rb index 7d35a9d..ff081a9 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,34 +1,10 @@ # frozen_string_literal: true require 'thread' -require 'curses' require 'faker' -require 'events' -require 'style' - -# Basic -require 'widgets/base' -require 'widgets/text' - -# Basic containers -require 'widgets/container' -require 'widgets/v_panel' - -require 'widgets/main' - -require 'widgets/menu' -require 'widgets/logo' - -require 'widgets/peers' -require 'widgets/peers/list' -require 'widgets/peers/search' - -require 'widgets/chat' -require 'widgets/chat/info' -require 'widgets/chat/history' -require 'widgets/chat/new_message' +require 'screen' class Main def self.inherited(_base) @@ -61,76 +37,19 @@ private 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 - + @screen = Screen.new Style.default = Style.new end def after_loop - Curses.close_screen + @screen.close end def before_iteration - render + @screen.render end def after_iteration - loop do - event = Curses.getch - break if event.nil? - handle event - end - end - - def window - @window ||= Widgets::Main.new( - nil, - 0, - 0, - Curses.stdscr.maxx, - Curses.stdscr.maxy, - ) - end - - def render - window.render - Curses.refresh - end - - def handle(char) - case char - when "\t".ord - window.trigger Events::Tab.new - - when Curses::Key::SLEFT - window.trigger Events::Window::Left.new - when Curses::Key::SRIGHT - window.trigger Events::Window::Right.new - - when Curses::Key::UP - window.trigger Events::Panel::Up.new - when Curses::Key::DOWN - window.trigger Events::Panel::Down.new - - when /[a-zA-Z0-9 ]/ - window.trigger Events::Text::Putc.new char - when Curses::Key::LEFT - window.trigger Events::Text::Left.new - when Curses::Key::RIGHT - window.trigger Events::Text::Right.new - when Curses::Key::HOME - window.trigger Events::Text::Home.new - when Curses::Key::END - window.trigger Events::Text::End.new - when Curses::Key::BACKSPACE - window.trigger Events::Text::Backspace.new - when Curses::Key::DC - window.trigger Events::Text::Delete.new - end + @screen.poll end end diff --git a/lib/screen.rb b/lib/screen.rb new file mode 100644 index 0000000..f60f968 --- /dev/null +++ b/lib/screen.rb @@ -0,0 +1,100 @@ +# frozen_string_literal: true + +require 'curses' + +# Additional classes +require 'events' +require 'style' + +# Basic +require 'widgets/base' +require 'widgets/text' + +# Basic containers +require 'widgets/container' +require 'widgets/v_panel' + +require 'widgets/main' +require 'widgets/menu' +require 'widgets/logo' + +require 'widgets/peers' +require 'widgets/peers/list' +require 'widgets/peers/search' + +require 'widgets/chat' +require 'widgets/chat/info' +require 'widgets/chat/history' +require 'widgets/chat/new_message' + +class Screen + def initialize + 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 + + @window ||= Widgets::Main.new( + nil, + 0, + 0, + Curses.stdscr.maxx, + Curses.stdscr.maxy, + ) + end + + def close + Curses.close_screen + end + + def render + @window.render + Curses.refresh + end + + def poll + loop do + ch = Curses.getch + break if ch.nil? + event = create_event ch + next if event.nil? + @window.trigger event + end + end + +private + + def create_event(ch) + case ch + when "\t".ord + Events::Tab.new + + when Curses::Key::SLEFT + Events::Window::Left.new + when Curses::Key::SRIGHT + Events::Window::Right.new + + when Curses::Key::UP + Events::Panel::Up.new + when Curses::Key::DOWN + Events::Panel::Down.new + + when /[a-zA-Z0-9 ]/ + Events::Text::Putc.new ch + when Curses::Key::LEFT + Events::Text::Left.new + when Curses::Key::RIGHT + Events::Text::Right.new + when Curses::Key::HOME + Events::Text::Home.new + when Curses::Key::END + Events::Text::End.new + when Curses::Key::BACKSPACE + Events::Text::Backspace.new + when Curses::Key::DC + Events::Text::Delete.new + end + end +end