From c798d4c06f4118fa85d928360ed04673cdc0bbe0 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Fri, 21 Jul 2017 06:48:04 +0000 Subject: [PATCH] Add basic code --- .rubocop.yml | 3 +++ exe/client | 7 ++++++ lib/main.rb | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 lib/main.rb diff --git a/.rubocop.yml b/.rubocop.yml index aa744f8..79e5ca5 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -8,6 +8,9 @@ Layout/AccessModifierIndentation: Metrics/LineLength: Max: 120 +Style/Documentation: + Enabled: false + Style/TrailingCommaInArguments: EnforcedStyleForMultiline: comma diff --git a/exe/client b/exe/client index 1991ec6..7447a8e 100755 --- a/exe/client +++ b/exe/client @@ -1,2 +1,9 @@ #!/usr/bin/env ruby # frozen_string_literal: true + +lib = File.expand_path('../lib', __dir__).freeze +$LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib + +require 'main' + +Main.new diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 0000000..342e582 --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +require 'thread' +require 'curses' + +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 + end + + def after_loop + Curses.close_screen + end + + def before_iteration; end + + def after_iteration + loop do + event = Curses.getch + break if event.nil? + handle event + end + end + + def handle(event); end +end