-
-
Save entropie/2fc09aee78787276ee6c82e6d8e90f66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "pp" | |
module M | |
def self.run(arg) | |
r = `#{arg} 2>&1` | |
return r | |
end | |
def self.call(args) | |
_player.each do |mp| | |
mp.call_if_authoritative(args) | |
end | |
end | |
def self._player | |
@_player ||= [] | |
end | |
def self.<<(o) | |
_player << o | |
end | |
class Player | |
def self.inherited(o) | |
M << o | |
end | |
def self.authoritative? | |
false | |
end | |
def self.call_if_authoritative(args) | |
if authoritative? | |
new.do_command(args) | |
end | |
end | |
def do_command(arg, *rest) | |
if arg.to_s.size == 0 | |
return | |
end | |
cmd = send(arg) | |
puts ">> #{self.class}:send: `#{cmd}` #{arg} #{PP.pp(rest, "")}" | |
M.run(cmd) | |
end | |
def self.status | |
@status ||= M.run(status_command).strip | |
end | |
def play | |
toggle | |
end | |
end | |
class MPD < Player | |
def self.status_command | |
"mpc status" | |
end | |
def self.authoritative? | |
if status =~ /^\[playing\]/ || status =~ /^\[paused\]/ | |
return true | |
end | |
false | |
end | |
def stop | |
"mpc stop" | |
end | |
def toggle | |
"mpc toggle" | |
end | |
def next | |
"mpc next" | |
end | |
def prev | |
"mpc prev" | |
end | |
end | |
class Spotify < Player | |
def self.status_command | |
"playerctl -p spotify status" | |
end | |
def self.authoritative? | |
status == "Playing" || status == "Paused" | |
end | |
def stop | |
"sp stop" | |
end | |
def toggle | |
#"dbus-send --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause" | |
"sp play" | |
end | |
def next | |
#"dbus-send --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next" | |
"sp next" | |
end | |
def prev | |
"sp prev" | |
#"dbus-send --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause" | |
end | |
end | |
class Brave < Player | |
def self.status_command | |
"playerctl -p brave status" | |
end | |
def self.authoritative? | |
status == "Playing" || status == "Paused" | |
end | |
def mk_cmd(what) | |
"playerctl -p brave %s" % what | |
end | |
def stop | |
mk_cmd("stop") | |
end | |
def pause | |
mk_cmd("pause") | |
end | |
def toggle | |
if Brave.status == "Playing" | |
pause | |
else | |
play | |
end | |
end | |
def next | |
mk_cmd("next") | |
end | |
def prev | |
mk_cmd("previous") | |
end | |
def play | |
mk_cmd("play") | |
end | |
end | |
end | |
M.call(ARGV.join) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment