Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

better help

  • Loading branch information...
commit 584a7901ca3d7405e2640ecb9cf72704104f2885 1 parent 01bd8de
@jpablobr authored
Showing with 123 additions and 61 deletions.
  1. +123 −61 playr
View
184 playr
@@ -1,110 +1,166 @@
#!/usr/bin/env ruby
# coding: utf-8
require 'optparse'
+require 'term/ansicolor'
+include Term::ANSIColor
class Client
require 'librmpd'
- require 'thread'
require 'readline'
- require 'shellwords'
- require 'term/ansicolor'
- include Term::ANSIColor
- Readline.completion_proc = lambda do |word|
+ Readline.completion_proc = lambda { |word|
commands.each.grep(/^#{ Regexp.escape(word) }/)
- end
+ }
def initialize
@mpd = MPD.new
@mpd.connect
+ start!
end
- def start!
- while line = Readline.readline("#{blue(prompt)}").strip
- next if line.empty?
- unless Readline::HISTORY.to_a[-1] == line
- Readline::HISTORY.push line
- end
-
- if commands.include?(line.to_sym)
- self.send(line.to_sym)
- elsif line.eql?('quit')
- break
- else
- warn "Unknown command: #{line.inspect}"
- end
- end
- end
-
- def volume vol
- if vol.nil?
- puts "Volume: #{@mpd.volume}"
- else
- @mpd.volume = vol.to_i
- end
+ def song?
+ @mpd.current_song.each_pair { |k,v|
+ puts(blue(k+": ") + v)
+ }
end
def stats
- hash = @mpd.stats
- hash.each_pair do |key, value|
- puts "#{key} => #{value}"
- end
- end
-
- def commands
- cmds = Client.instance_methods -
- Object.instance_methods -
- Term::ANSIColor.instance_methods
- [:start!, :commands].each { |c| cmds.delete(c) }
- cmds
- end
-
- def song?
- @mpd.current_song.each_pair { |k,v| warn "#{blue(k+":")} #{v}" }
+ @mpd.stats.each_pair { |k,v| puts(k + ': ' + v) }
end
- def playlist?
- warn green("#{pl_name(@mpd.playlist.first.fetch('file'))}")
+ def playlist
+ warn green(pl_name(@mpd.playlist.first.fetch('file')))
@mpd.playlist.each { |l|
- warn "#{blue("song:")} #{song_name(l.fetch('file'))}"
+ puts(blue("song: ") + song_name(l.fetch('file')))
}
end
- def playlists?
- @mpd.playlists.each { |pl|
- warn "#{blue("pl:")} #{pl}"
- }
+ def playlists
+ @mpd.playlists.each { |p| warn "#{blue("pl:")} #{p}" }
end
- def pause; @mpd.pause = !mpd.paused? end
+ def pause; @mpd.pause = true end
+ def continue; @mpd.pause = false end
def stop; @mpd.stop end
def next; @mpd.next end
def prev; @mpd.previous end
def disconnect; @mpd.disconnect end
def repeat; @mpd.repeat = !@mpd.repeat? end
def random; @mpd.random = !@mpd.random? end
- def help; commands.each { |c| warn(c) } end
def play song; @mpd.play song end
+ def +; Vol.+ end
+ def -; Vol.- end
+ def mute; Vol.mute end
+ def vol v; Vol.vol(v) end
+
+ def help
+ commands
+ .group_by{|m| Client.instance_method(m)}
+ .map(&:last)
+ .sort {|a,b| a.to_s.size <=> b.to_s.size }
+ .select { |s|
+ warn blue(s[0].to_s) + (s[1].nil? ? '' : (' or: ' + green(s[1].to_s)))
+ }
+ end
+
+ alias :ls :playlist
+ alias :pls :playlists
+ alias :ps :pause
+ alias :p :play
+ alias :c :continue
+ alias :s :song?
+ alias :h :help
+ alias :n :next
+ alias :r :random
+ alias :v :vol
+ alias :d :disconnect
private
- def prompt; song_name(@mpd.current_song.fetch('file')) end
- def pl_name pl; pl.split('/')[0..-2].join('/') end
- def song_name song; song.gsub(/(.*\/)|(\..*$)/,'') end
+ def start!
+ while line = Readline.readline(Prompt.display(prompt_data)).strip
+ next if line.empty?
+ unless Readline::HISTORY.to_a[-1] == line
+ Readline::HISTORY.push line
+ end
+ break if line.match(/(^quit$|^q$)/)
+ process_cmd(line)
+ end
+ end
+
+ def commands
+ (Client.instance_methods -
+ Object.instance_methods -
+ Term::ANSIColor.instance_methods)
+ .select { |c| c.to_s !~ /start!/ }
+ end
+
+ def pl_name pl
+ pl.split('/')[0..-2].join('/')
+ end
+
+ def song_name song
+ song.gsub(/(.*\/)|(\..*$)/,'')
+ end
+
+ def prompt_data
+ song_name(@mpd.current_song.fetch('file'))
+ end
+
+ def process_cmd cmd
+ meth = cmd.split[0]
+ args = cmd.split[1..-1]
+ if commands.include?(meth.to_sym)
+ begin
+ self.send(meth, *args)
+ rescue ArgumentError => e
+ puts "Command \"" + meth + "\" " + e.message
+ end
+ else
+ warn "Unknown command: #{cmd.inspect}"
+ end
+ end
+end
+
+class Prompt
+ class << self
+ def display data, sign=''
+ prompt_color(data + ' ' + sign + ' ')
+ end
+
+ private
+
+ def prompt_color p
+ Vol.mute? ? red(p) : green(p)
+ end
+ end
+end
+
+class Vol
+ @@vol_set = 'amixer sset Master'
+ @@vol_get = 'amixer sget Master'
+ class << self
+ def +; `#{@@vol_set} 5%+` end
+ def -; `#{@@vol_set} 5%-` end
+ def mute; `#{@@vol_set} toggle` end
+ def vol v; `#{@@vol_set} "#{v}"` end
+ def mute?
+ `#{@@vol_get} | awk '/Mono:/{print $6}'`['off']
+ end
+ end
end
opts = OptionParser.new do |o|
o.banner = "Usage: playr [OPTION] [<args>]"
o.separator ""
o.on("-h", "--help", "Print this help.") {
- return $stderr.puts(opts)
+ $stderr.puts(opts)
}
o.on("-v", "--version", "Print version.") {
- return $stderr.puts("0.1.0")
+ $stderr.puts("0.1.0")
}
o.on("-s", "--start", "Run the mpd client.") {
- puts "staring"
- Client.new.start!
+ Client.new
}
o.on("-t", "--tests", "run tests.") {
eval(DATA.read)
@@ -133,4 +189,10 @@ class TestClient < MiniTest::Unit::TestCase
assert true, @c.commands.include?(m)
}
end
+
+ def test_process_cmd_should_raise_wrong_number_of_arguments
+ assert_output('"mute" does not accept arguments.') {
+ @c.process_cmd("mute invalid arg")
+ }
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.