config

OpenBSD system configuration
git clone git://jacobedwards.org/config
Log | Files | Refs | README

commit f57202594bc34ff943853881a12e5e5004fd27fd
parent 591f5b64ae242f614b1de81b70ab42ee0003aecb
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Thu, 19 Nov 2020 12:47:57 -0800

Revise mpc shell alias module

Remove mpdconfig, mgrep, and mato functions replacing mato with a
script of the same name. The mato function was large and complex,
deserving of it's own script.

Vastly simplify the mf function by using the builtin mpc subcommand
`listall'.

Sort and modify shorthand aliases.

Diffstat:
Mmodules/aliases/.config/aliases/mpc | 43+++++++------------------------------------
Amodules/scripts/.local/bin/mato | 34++++++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+), 36 deletions(-)

diff --git a/modules/aliases/.config/aliases/mpc b/modules/aliases/.config/aliases/mpc @@ -1,42 +1,13 @@ # mpc aliases and functions alias m="mpc" -alias mp="m play" -alias mt="m toggle" -alias mk="m prev" -alias mj="m next" -alias ml="m playlist" -alias ms="m search" alias mP="m searchplay any" alias ma="m add" +alias mi="m play" +alias mj="m next" +alias ml="m playlist" +alias mo="m prev" +alias ms="m search any" +alias mt="m toggle" -# return mpd setting (must be of the regex format: `^setting ['"]?value["']?' -mpdconfig() { - awk -F"[[:space:]\"']+" -vreg=${1:?No expression} \ - 'match($1, reg) > 0 { sub("^~", ENVIRON["HOME"], $2); print $2; ++c } END { exit (c - 1) }' \ - $XDG_CONFIG_HOME/mpd/mpd.conf -} - -# grep mpc playlists -mgrep() { grep -h $@ `mpdconfig playlist_directory`/* | sort -u ;} - -# add current song to playlist(s) -mato() { - local track="`mpc current -f "%file%"`" - local dir=`mpdconfig playlist_directory` - while [ $# -gt 0 ]; do - # note it's not anchored. - fgrep -q "$track" "$dir/$1.m3u" - if [ $? -eq 1 ]; then - echo "$track" >> $dir/$1.m3u - else - warn "'$1': Already contains this track." - fi - shift - done -} - -mf() { - local d="$(mpdconfig music_directory)" - find "$d" -type d -name $1 | sed "s!^$d!!" -} +mf() { m listall | egrep $* ;} diff --git a/modules/scripts/.local/bin/mato b/modules/scripts/.local/bin/mato @@ -0,0 +1,34 @@ +#!/usr/local/bin/rc -ex +# Add the current song to a playlist + +progname = `{ basename $0 } + +fn warn { printf '%s: ' $progname; printf $* } +fn err { warn $*; exit 1 } +fn isduplicate { + awk '-vtrack='^$1 \ + 'length($0) == length(track) && index(track, $0) > 0 { + exit 1 + }' +} + +if (test -z $XDG_CONFIG_HOME) { + config = $HOME/.mpdconf; +} else config = $XDG_CONFIG_HOME/mpd/mpd.conf; + +dir = `{ grep -m1 '^playlist_directory' $config | cut -d" -f2 \ + | sed 's!^~!'$HOME'!' || err 'playlist_directory undefined\n' } +track = `{ mpc current -f '%file%' } + +for (m3u in $*) { + file = $dir/$m3u.m3u; + if (test ! -e $file) { + err '''%s'': Nonexistent.\n' $file + } else { + if (isduplicate $track <$file) { + echo 'echo $track >> $file' + } else { + warn '''%s'': Already contains this track.\n' $m3u + } + } +}