Class: JimmyJukebox::UserInputHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/jimmy_jukebox/user_input_handler.rb

Defined Under Namespace

Classes: NoPlayLoopThreadException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jukebox) ⇒ UserInputHandler

Returns a new instance of UserInputHandler.



13
14
15
# File 'lib/jimmy_jukebox/user_input_handler.rb', line 13

def initialize(jukebox)
  self.jukebox = jukebox
end

Instance Attribute Details

#jukeboxObject

Returns the value of attribute jukebox.



11
12
13
# File 'lib/jimmy_jukebox/user_input_handler.rb', line 11

def jukebox
  @jukebox
end

Instance Method Details

#get_charObject



33
34
35
# File 'lib/jimmy_jukebox/user_input_handler.rb', line 33

def get_char
  @get_char_method.call
end

#replObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jimmy_jukebox/user_input_handler.rb', line 37

def repl
  set_get_char_method
  loop do
    case char = get_char
    when "q", "Q"
      raise Interrupt
    when "e", "E"
      jukebox.erase_song
    when "p", "P"
      if jukebox.current_song.paused?
        puts "Unpausing. To re-pause, enter 'p' again"
        jukebox.unpause_current_song
      else
        puts "Pausing. To unpause, enter 'p' again"
        jukebox.pause_current_song
      end
    when "r", "R"
      jukebox.replay_previous_song
    when "s", "S"
      jukebox.skip_song
    when "b", "B"
      jukebox.replay_song
    else
      puts "#{char.strip} is not a valid response" if char
    end
  end
rescue Interrupt, SystemExit => e
  puts "JimmyJukebox closed by user request. Bye!"
  jukebox.quit
  exit
end

#set_get_char_methodObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jimmy_jukebox/user_input_handler.rb', line 17

def set_get_char_method
  @get_char_method = if JimmyJukebox::RUNNING_JRUBY
                       lambda { STDIN.getch }
                     else
                       lambda {
                         begin
                           stty_state = `stty -g`
                           system("stty raw opost -echo -icanon isig")
                           STDIN.getc.chr
                         ensure
                           `stty #{stty_state}`
                         end
                       }
                     end
end