ls | dmenu
zeigte mir dann auch relativ schnell die Funktionsweise. Darüber hinaus erschloss sich mir aber leider nicht sehr viel. Was ist dein Startpunkt?
Kannst du ein paar Code-Schnipsel posten? Würde mich gerne darüber hermachen
geschrieben 15. Oktober 2007 - 20:31
ls | dmenu
geschrieben 15. Oktober 2007 - 20:37
#!/usr/bin/env ruby
#
# Copyright © 2007 Johannes Krude
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# version 2007-09-29
#
def find_mpd_host(list)
list.find { |x| `MPD_HOST="#{x}" mpc>/dev/null; echo -n $?`=="0" }
end
$dmenu= ENV["DMENU"] or "dmenu -b"
$hosts= ["localhost", "musicbox", "trillian"]
$host= (ENV["MPD_HOST"] or find_mpd_host($hosts))
$mpc= "MPD_HOST=\"#{$host}\" mpc --no-status"
(`echo "mpd not running" | #{$dmenu}`;exit) unless $host
$find_num= /([1-9]\d*)/
def mpc(*args)
puts "#{$mpc}#{args.inject("") { |a, b| "#{a} \"#{b.escape}\""}}"
`#{$mpc}#{args.inject("") { |a, b| "#{a} \"#{b.escape}\""}}`
end
def load_playlist(p)
return unless `#{$mpc} lsplaylists`.to_a.include?("#{p}\n")
mpc "clear"
mpc("load", p)
mpc("play")
end
def get_playlist
$1 if `#{$mpc} --format "%artist% - %title%" playlist | #{$dmenu}`=~ $find_num
end
def get_playlists
`#{$mpc} lsplaylists | #{$dmenu}`
end
class String
def to_time
return 0 unless self=~ /([+-]?)((\d+:)*)(\d+):?/
($1=="-" ? -1 : 1) * ($4.to_i+($2.to_time*60))
end
def escape
self.gsub(/"/, "\\\"")
end
end
class Integer
def to_times
self.abs>=60 ? (self/60).to_times+":"+((self.abs`)>=10 ? "" : "0")+(self.abs`).to_s : self.to_s
end
end
def big_case(answer)
case answer
when ""
when "add"
x= `#{$mpc} listall | #{$dmenu}`
mpc("add", x) unless x.empty?
when /^add (.+)$/
mpc("add", $1)
when "crossfade"
mpc("crossfade", $1) if `echo "#{(0..99).to_a.join("\n")}" | #{$dmenu}`=~ /^#{$find_num}$/
when /^crossfade (#{$find_num}|0)$/
mpc("crossfade", $1)
when "disable"
mpc("disable", $1) if `#{$mpc} outputs | #{$dmenu}`=~ $find_num
when "enable"
mpc("enable", $1) if `#{$mpc} outputs | #{$dmenu}`=~ $find_num
when "load"
x= `#{$mpc} lsplaylists | #{$dmenu}`
mpc("load", x) unless x.empty?
when /^load (.+)$/
mpc("load", $1)
when "move"
return unless a= get_playlist
return unless b= get_playlist
mpc("move", a, b)
when /^move #{$find_num} #{$find_num}$/
mpc("move", $1, $2)
when /^play #{$find_num}$/
mpc("play", $1)
when "playlist"
big_case(`echo "#{`#{$mpc} --format "%artist% - %title%" playlist`}move\nremove" | #{$dmenu}`)
when "playlists"
x= `echo "#{`#{$mpc} lsplaylists`}remove_playlist" | #{$dmenu}`
case x
when "remove_playlist"
big_case(x)
when /^remove_playlist .+$/
big_case(x)
else
load_playlist(`echo "#{`#{$mpc} lsplaylists`}remove_playlist" | #{$dmenu}`)
end
when /^playlists (.+)$/
load_playlist($1)
when "outputs"
mpc(`echo "disable\nenable" | #{$dmenu}`, $1) if `#{$mpc} outputs | #{$dmenu}`=~ $find_num
when "previous"
mpc("prev")
when /^random (on|off)$/
mpc("random", $1)
when "remove"
mpc("del", x) if x= get_playlist
when /^remove #{$find_num}$/
mpc("del", $1)
when "remove_playlist"
mpc("rm", get_playlists())
when /^remove_playlist (.+)$/
mpc("rm", $1)
when /^repeat (on|off)$/
mpc("repeat", $1)
when "save"
mpc("save", `echo | #{dmenu}`)
when /^save (.+)$/
mpc("save", $1)
when "search"
return if (x= `echo "album\nartist\nfilename\ntitle" | #{$dmenu}`).empty?
return if (y= `echo | #{$dmenu}`).empty?
`#{$mpc} search #{x.escape} "#{y.escape}" | #{$mpc} add`
when /^search (artist|album|title|filename) (.+)$/
`#{$mpc} search #{$1} #{$2.escape} | #{$mpc} add`
when "search_one"
return if (x= `echo "album\nartist\nfilename\ntitle" | #{$dmenu}`).empty?
return if (y= `echo | #{$dmenu}`).empty?
mpc("add", `#{$mpc} search #{x.escape} "#{y.escape}" | #{$dmenu}`)
when /^search_one (artist|album|title|filename) (.+)$/
mpc("add", `#{$mpc} search #{$1} #{$2.escape} | #{$dmenu}`)
when "seek"
mpc("seek", x) unless (x= `echo "#{(0..100).to_a.join("%\n")}%\n#{((-$per)..(100-$per)).collect { |x| x<0 ? x : "+#{x}" }.join("%\n")}\n#{(0..$tot).collect { |x| x.to_times}.join("\n")}\n#{((-$tim)..($tot-$tim)).collect { |x| (x>=0 ? "+" : "")+x.to_times}.join("\n")}" | #{$dmenu}`).empty?
when /^seek ([+-]?\d+%|[+-]?((\d+:(?\d+;)?\d+)$/
mpc("seek", $1)
when "stats"
`#{$mpc} stats | sed -e '4 d' -e 's/ \\+/ /' | #{$dmenu}`
when "version"
`#{$mpc} version | #{$dmenu}`
when "volume"
mpc("volume", (($1 if $1.to_i<= 100) or 100)) if `echo "#{(0..100).to_a.join("\n")}" | #{$dmenu}`=~ $find_num
when /^volume #{$find_num}$/
mpc("volume", (($1 if $1.to_i<= 100) or 100))
when $find_num
mpc("play", $1)
else
mpc answer
end
end
on_off= ["off", "on"]
mpc_out= mpc
ran= on_off[on_off.index($1)^1] if mpc_out=~ /random: (on|off)/
rep= on_off[on_off.index($1)^1] if mpc_out=~ /repeat: (on|off)/
$per= $1.to_i if mpc_out=~ /\(#{$find_num}%\)/
if mpc_out=~ / (.+?)\/(.+?) /
$tim= $1.to_time
$tot= $2.to_time
end
big_case(`echo "add\nclear\ncrop\ncrossfade\ndisable\nenable\nload\nmove\nnext\noutputs\npause\nplay\nplaylist\nplaylists\nprevious\nrandom #{ran}\nremove\nremove_playlist\nrepeat #{rep}\nsave\nsearch\nsearch_one\nseek\nshuffle\nstats\nstop\nupdate\nversion\nvolume" | #{$dmenu}`)
Das gibt dir, sofern du MPD laufen hast, ein kleines Menü mit allen möglichen MPD-Optionen, also Play, Pause, Stop, etc, sowie eine Anzeige der Playlist, wenn man "Playlist" auswählt.ls -1 /usr/bin | dmenuwäre so das Standarddingens.

Dieser Beitrag wurde von ph030 bearbeitet: 15. Oktober 2007 - 20:51