Sha256: 6929e51c6558d94a1d66a2fdc18189df08b40d8f1cf00547e5d028cde4fec75f

Contents?: true

Size: 1.34 KB

Versions: 22

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

# Top level comment missing!
module Cotcube
  # noinspection ALL
  module Helpers
    def keystroke(quit: false)
      begin
        # save previous state of stty
        old_state = `stty -g`
        # disable echoing and enable raw (not having to press enter)
        system 'stty raw -echo'
        c = $stdin.getc.chr rescue '_' # rubocop:disable Style/RescueModifier
        # gather next two characters of special keys
        if c == "\e"
          extra_thread = Thread.new do
            c += $stdin.getc.chr
            c += $stdin.getc.chr
          end
          # wait just long enough for special keys to get swallowed
          extra_thread.join(0.00001)
          # kill thread so not-so-long special keys don't wait on getc
          extra_thread.kill
        end
      rescue StandardError => e
        puts "#{e.class}: #{e.message}"
        puts e.backtrace
      ensure
        # restore previous state of stty
        system "stty #{old_state}"
      end
      c.each_byte do |x| # rubocop:disable Lint/UnreachableLoop
        case x
        when 3
          puts 'Strg-C captured, exiting...'
          quit ? exit : (return true)
        when 13
          return '_return_'
        when 27
          puts 'ESCAPE gathered'
          return '_esc_'
        else
          return c
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
cotcube-helpers-0.2.4 lib/cotcube-helpers/input.rb
cotcube-helpers-0.2.3 lib/cotcube-helpers/input.rb
cotcube-helpers-0.2.2.5 lib/cotcube-helpers/input.rb
cotcube-helpers-0.2.2.4 lib/cotcube-helpers/input.rb
cotcube-helpers-0.2.2.3 lib/cotcube-helpers/input.rb
cotcube-helpers-0.2.1.1 lib/cotcube-helpers/input.rb
cotcube-helpers-0.2.0 lib/cotcube-helpers/input.rb
cotcube-helpers-0.1.10 lib/cotcube-helpers/input.rb
cotcube-helpers-0.1.9.2 lib/cotcube-helpers/input.rb
cotcube-helpers-0.1.9.1 lib/cotcube-helpers/input.rb
cotcube-helpers-0.1.9 lib/cotcube-helpers/input.rb
cotcube-helpers-0.1.8 lib/cotcube-helpers/input.rb
cotcube-helpers-0.1.7.4 lib/cotcube-helpers/input.rb
cotcube-helpers-0.1.7.3 lib/cotcube-helpers/input.rb
cotcube-helpers-0.1.7.2 lib/cotcube-helpers/input.rb
cotcube-helpers-0.1.7.1 lib/cotcube-helpers/input.rb
cotcube-helpers-0.1.7 lib/cotcube-helpers/input.rb
cotcube-helpers-0.1.6 lib/cotcube-helpers/input.rb
cotcube-helpers-0.1.5.4 lib/cotcube-helpers/input.rb
cotcube-helpers-0.1.5.1 lib/cotcube-helpers/input.rb