Class: MightyTest::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/mighty_test/console.rb

Instance Method Summary collapse

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

#clearObject



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_nonblockObject



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_inputObject



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