Class: MightyTest::Console
- Inherits:
-
Object
- Object
- MightyTest::Console
- Defined in:
- lib/mighty_test/console.rb
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize(stdin: $stdin, sound_player: "/usr/bin/afplay", sound_paths: SOUNDS) ⇒ Console
constructor
A new instance of Console.
- #play_sound(name, wait: false) ⇒ Object
- #read_keypress_nonblock ⇒ Object
- #with_raw_input ⇒ Object
Constructor Details
#initialize(stdin: $stdin, sound_player: "/usr/bin/afplay", sound_paths: SOUNDS) ⇒ Console
Returns a new instance of Console.
6 7 8 9 10 |
# File 'lib/mighty_test/console.rb', line 6 def initialize(stdin: $stdin, sound_player: "/usr/bin/afplay", sound_paths: SOUNDS) @stdin = stdin @sound_player = sound_player @sound_paths = sound_paths end |
Instance Method Details
#clear ⇒ Object
12 13 14 15 16 17 |
# File 'lib/mighty_test/console.rb', line 12 def clear return false unless tty? $stdout.clear_screen true end |
#play_sound(name, wait: false) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mighty_test/console.rb', line 29 def play_sound(name, wait: false) return false unless tty? paths = sound_paths.fetch(name) { raise ArgumentError, "Unknown sound name #{name}" } path = paths.find { |p| File.exist?(p) } return false unless path && File.executable?(sound_player) thread = Thread.new { system(sound_player, path) } thread.join if wait true end |
#read_keypress_nonblock ⇒ Object
25 26 27 |
# File 'lib/mighty_test/console.rb', line 25 def read_keypress_nonblock stdin.getc if stdin.wait_readable(0) end |
#with_raw_input ⇒ Object
19 20 21 22 23 |
# File 'lib/mighty_test/console.rb', line 19 def with_raw_input(&) return yield unless stdin.respond_to?(:raw) && tty? stdin.raw(intr: true, &) end |