Class: Tune::Task

Inherits:
Thor
  • Object
show all
Defined in:
lib/tune/task.rb

Instance Method Summary collapse

Constructor Details

#initialize(args, opts = {}, conf = {}) ⇒ Task

Returns a new instance of Task.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tune/task.rb', line 10

def initialize(args, opts={}, conf={})
  unless %w[
    help power
  ].include?(conf[:current_command].name)
    unless connect
      puts 'please power on ;)'
      exit
    end
  end
  super
end

Instance Method Details

#listObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tune/task.rb', line 41

def list
  all_names = channels
  var_width = all_names.length.to_s.length
  all_names.each_with_index do |channel, index|
    var = index.to_s.rjust var_width, '0'
    if channel == playing
      puts "\033[1;30m$\[#{var}\] \033[1;31m#{channel}\033[m"
    else
      puts "\033[1;30m$\[#{var}\] \033[1;34m#{channel}\033[m"
    end
  end
end

#offObject



74
75
76
77
78
79
# File 'lib/tune/task.rb', line 74

def off
  if name = playing
    @player.turnOff
    puts name
  end
end

#play(channel = '') ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tune/task.rb', line 57

def play channel=''
  if channel =~ /^(\$*)(\d+)$/
    name = channels[$2.to_i]
  end
  if channel.empty?
    # same as `show`
    puts playing
  elsif !name
    puts "$#{channel} does not exist"
  else
    @player.playRadio name
    puts name
  end
end

#power(action = '') ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tune/task.rb', line 25

def power action=''
  pid = `ps aux | grep '[r]adiotray' | awk '{print $2}'`
  res = pid.empty?
  case action
  when /^on$/i
    res = `radiotray 1>/dev/null 2>&1 &`.nil? unless connect
  when /^off$/i
    res = `kill -15 #{pid}` if connect
  end
  on  = "\033[1;32mon\033[m"
  off = "\033[1;31moff\033[m"
  puts (res ? off : on)
end

#showObject



83
84
85
# File 'lib/tune/task.rb', line 83

def show
  puts playing
end

#volume(action = nil, value = '1') ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/tune/task.rb', line 90

def volume action=nil, value='1'
  if action =~ /^(up|down)$/
    value = value.to_i > 5 ? 5 : value.to_i
    value.times do
      @player.send "volume#{action.capitalize}".to_sym
    end
  end
end