Sha256: 1268c1a34092af78a317676901462f619545c1c0fd7087e47e4a958217e33229

Contents?: true

Size: 1.28 KB

Versions: 7

Compression:

Stored size: 1.28 KB

Contents

require 'remedy/keyboard'
require 'remedy/ansi'

module Remedy
  class Interaction
    def initialize message = nil
      @message = message
    end

    attr_accessor :message

    def confirm message = 'Confirm?'
      ANSI.cursor.home!
      ANSI.command.clear_line!

      print message, ' y/n '
      if Keyboard.get === :y then
        yield if block_given?
        true
      else
        false
      end
    end

    def quit!
      confirm 'Are you sure you want to quit? You will lose everything. :(' do
        ANSI.cursor.home!
        ANSI.command.clear_down!
        ANSI.cursor.show!

        puts " -- Bye!"
        exit
      end
    end

    def debug!
      require 'pry'
      binding.pry
    end

    def display key
      ANSI.command.clear_line!
      print " -- You pressed: #{key.inspect}"
    end

    def loop
      Keyboard.raise_on_control_c!

      super do
        print " -- #{message}" if message

        ANSI.cursor.hide!
        key = Keyboard.get

        if key == ?\C-q then
          display key
          quit!
        elsif key == ?\C-d and defined? Pry then
          display key
          debug!
        end

        yield key
      end
    end

    def get_key
      print " -- #{message}" if message

      ANSI.cursor.hide!
      key = Keyboard.get
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
remedy-0.4.0 lib/remedy/interaction.rb
remedy-0.3.1 lib/remedy/interaction.rb
remedy-0.3.0 lib/remedy/interaction.rb
remedy-0.2.0 lib/remedy/interaction.rb
remedy-0.1.2 lib/remedy/interaction.rb
remedy-0.1.1 lib/remedy/interaction.rb
remedy-0.1.0 lib/remedy/interaction.rb