Class: Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/subcl/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

Returns a new instance of Runner.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/subcl/runner.rb', line 4

def initialize(options = {})
  @options = {
    :tty => true,
    :out_stream => STDOUT,
    :err_stream => STDERR,
    :mock_player => nil,
    :mock_api => nil
  }.merge! options

  #TODO refactor this away
  if File.exist?('debug')
    puts "DEBUGGING"
    @options[:debug] = true
  end
end

Instance Method Details

#parse_options!(args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
68
69
70
71
72
73
74
75
# File 'lib/subcl/runner.rb', line 20

def parse_options! args
  OptionParser.new do |opts|
    opts.banner = "Usage: subcl [options] command"
    opts.separator %{
  Queue Commands
    clear queue and immediately start playing
        play[-song|-album|-artist|-playlist] <pattern>
        ps|pl|pr|pp <pattern>
    clear queue and immediately start playing random songs
        play-random <count, default 10>
        r <count, default 10>
    add to end of queue
        queue-last[-song|-album|-artist|-playlist] <pattern>
        ls|ll|lr|lp <pattern>
    add after the current song
        queue-next[-song|-album|-artist|-playlist] <pattern>
        ns|nl|nr|np <pattern>
    albumart-url [size] - print url of albumart to terminal,
        optionally with a specified image size

  Playback Commands
    play
    pause
    toggle (play when pause, pause when played)
    stop
    next
    previous
    rewind (get to start of song, or previous song when at start)

  Options }

    @usage = opts

    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      @options[:verbose] = v
    end
    opts.on('-1', '--use-first', 'On multiple matches, use the first match instead of asking interactively') do
      @options[:interactive] = false
    end
    opts.on('-s', '--shuffle', "Shuffle playlist before queueing") do
      @options[:shuffle] = true
    end
    opts.on('-c', '--current', 'Use info currently playing song instead of commandline argument') do
      @options[:current] = true
    end
    opts.on('-h', '--help', 'Display this screen') do
      @options[:out_stream].puts opts
      exit
    end
    opts.on("--version", "Print version information") do
      @options[:out_stream].puts Subcl::VERSION
      exit
    end

  end.parse! args
end

#run(args) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/subcl/runner.rb', line 77

def run(args)
  LOGGER.debug { "args = #{args}" }

  parse_options!(args)

  LOGGER.debug { "args = #{args}" }

  unless args.size >= 1
    @options[:err_stream].puts @usage
    exit 3
  end

  unless system('tty -s')
    #not running in a tty, so no use for interactivity
    @options[:tty] = false
    @options[:interactive] = false
  end

  subcl = Subcl.new @options

  arg = args[1,args.length-1].join(" ") #put rest of args together so no quotes are required

  command = args[0].downcase
  case command

  when /^play-song$|^ps$/
    subcl.queue(arg, :song, {:play => true, :clear => true})
  when /^play-artist$|^pr$/
    subcl.queue(arg, :artist, {:play => true, :clear => true})
  when /^play-album$|^pl$/
    subcl.queue(arg, :album, {:play => true, :clear => true})
  when /^play-playlist$|^pp$/
    subcl.queue(arg, :playlist, {:play => true, :clear => true})
  when /^play-random$|^r$/
    subcl.queue(arg, :randomSong, {:play => true, :clear => true})
  when /^play-any$|^pn$|^p$/
    subcl.queue(arg, :any, {:play => true, :clear => true})
  when /^queue-next-song$|^ns$/
    subcl.queue(arg, :song, {:insert => true})
  when /^queue-next-artist$|^nr$/
    subcl.queue(arg, :artist, {:insert => true})
  when /^queue-next-album$|^nl$/
    subcl.queue(arg, :album, {:insert => true})
  when /^queue-next-playlist$|^np$/
    subcl.queue(arg, :playlist, {:insert => true})
  when /^queue-next-any$|^nn$|^n$/
    subcl.queue(arg, :any, {:insert => true})
  when /^queue-last-song$|^ls$/
    subcl.queue(arg, :song)
  when /^queue-last-artist$|^lr$/
    subcl.queue(arg, :artist)
  when /^queue-last-album$|^ll$/
    subcl.queue(arg, :album)
  when /^queue-last-playlist$|^lp$/
    subcl.queue(arg, :playlist)
  when /^queue-last-any$|^ln$|^l$/
    subcl.queue(arg, :any)
  when "albumart-url"
    arg = nil if arg.empty?
    @options[:out_stream].puts subcl.albumart_url(arg)
  when /^album-list$|^al$/
    subcl.albumlist
  when "test-notify"
    subcl.testNotify
  else
    begin
      #pass through for player commands
      subcl.send(command, [])
    rescue NoMethodError
      if subcl.configs[:play_any_on_unknown_command]
        subcl.queue(args.join(" "), :any, {:play => true, :clear => true})
      else
        unknown(command)
      end
    end
  end
end

#unknown(command) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/subcl/runner.rb', line 155

def unknown(command)
  if @options[:tty] then
    @options[:err_stream].puts "Unknown command '#{command}'"
    @options[:err_stream].puts @usage
  else
    subcl.notifier.notify "Unknown command '#{command}'"
  end
  exit 3
end