Sha256: f4834ee819995a7e74e220e3d2794f2752729e6793e9def310847f8a3177960a

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
remedy-0.0.5 lib/remedy/interaction.rb
remedy-0.0.4 lib/remedy/interaction.rb
remedy-0.0.4.pre lib/remedy/interaction.rb