Sha256: dd72900b87619c650ad1168b7b0fbcf1e9836ddc020878cd591f9f01bb8b5b94

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

module Vedeu
  ModeSwitch = Class.new(StandardError)

  class Application
    def self.start(options = {})
      new(options).start
    end

    def initialize(options = {})
      @options = options
    end

    def start
      Terminal.open(mode) do
        Terminal.set_cursor_mode

        Vedeu.events.trigger(:_initialize_)

        runner { main_sequence }
      end
    end

    private

    attr_reader :options

    def runner
      if interactive?
        interactive { yield }

      else
        run_once    { yield }

      end
    end

    def main_sequence
      Input.capture
    end

    def interactive?
      options.fetch(:interactive)
    end

    def interactive
      loop { yield }

    rescue ModeSwitch
      if Terminal.raw_mode?
        Application.start({ mode: :cooked })

      else
        Application.start({ mode: :raw })

      end
    end

    def run_once
      yield
    end

    def mode
      options.fetch(:mode)
    end

    def debug
      Vedeu::API::Trace.call if options.fetch(:debug)
    end

    def options
      defaults.merge!(@options)
    end

    def defaults
      {
        debug:       false,
        interactive: true,
        mode:        :raw
      }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vedeu-0.1.15 lib/vedeu/application.rb
vedeu-0.1.14 lib/vedeu/application.rb
vedeu-0.1.13 lib/vedeu/application.rb
vedeu-0.1.12 lib/vedeu/application.rb
vedeu-0.1.10 lib/vedeu/application.rb