commit 164daa6df7575bfb74b0a89f7b4e39db3812c455
parent ecfb2bf6258ab48eec31c35a5494fe5e175b7d2c
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Thu, 10 Sep 2020 12:31:31 -0700
Refactor using functions
Diffstat:
1 file changed, 21 insertions(+), 23 deletions(-)
diff --git a/scripts/.local/bin/mps b/scripts/.local/bin/mps
@@ -3,29 +3,27 @@
set -eu
+usage() { echo "usage: `basename $0` [-ls]" ;}
+err() { echo "`basename $0`: $@" 1>&2 ;}
+
+# save(file)
+save() {
+ local file="`mpc current -f "%file%" | tr '\n' ',' > "$1"`"
+ local time=`mpc status | awk -F"[ /]+" 'FNR == 2 { print $4; exit }'`
+ echo "${file}${time}" > "$1"
+}
+
+# load(file)
+load() {
+ state="`cat "$1"`"
+ local file=`echo $state | cut -d, -f1`
+ local time=`echo $state | cut -d, -f2-`
+ mpc -q insert "$file" && mpc -q next && mpc -q seek "$time"
+}
+
case "${1:-"-s"}"
in
- -s)
- file=${2:-"-"}
- [ "$file" = "-" ] && file="/dev/stdout"
- mpc current -f "%file%" | tr '\n' ',' > "$file"
- mpc status | egrep -o "[0-9]+:[0-9]+/[0-9]+:[0-9]+" | cut -d"/" -f1 >> "$file";;
- -l)
- if [ $# -lt 2 ]; then
- echo "No input specified." 1>&2
- exit 1
- elif [ ! -f "$2" ]; then
- echo "File does not exist." 1>&2
- exit 1
- else
- file="$2"
- track="$(cut -d"," -f1 "$file")"
- # if time is invalid unhandled error ocours due to mpc next
- mpc -q insert "${track:?track does not exist}" && mpc -q next
- mpc -q seek `cut -d"," -f2 "$file"`
- fi;;
- *)
- echo "usage, `basename $0` [-sl]"
- echo "\t-s, save state to file ('-' for stdout)"
- echo "\t-l, load state from file";;
+ -s) save "${2:-"/dev/stdout"}";;
+ -l) load "${2:-"/dev/stdin"}";;
+ *) usage; exit 1;;
esac