#!/bin/sh

# more osascript than bash, but it does have /bin/sh up there...
# also, fuck osascript. undocumented garbage.
# this script is very responsive now, though, so that's a thing...

trap "stty echo; tput cnorm; exit" EXIT
stty -echo; tput civis
osascript -e '
	set key_up to do shell script "printf \\\\e[A"
	set key_dn to do shell script "printf \\\\e[B"
	set sqr to do shell script "printf \\\\xE2\\\\x96\\\\xA0"
	set endl to do shell script "tput el; tput cuu1"
	set vol to output volume of (get volume settings)
	repeat
		set pips to vol * 16 / 100
		set bar to "volume: ["
		repeat pips times
			set bar to bar & sqr
		end repeat
		repeat 16 - pips times
			set bar to bar & " "
		end repeat
		set bar to bar & "] "
		log bar & vol & endl
		set input to do shell script "
			read -n3 key < /dev/fd/10
			printf $key
		"
		quoted form of input -- force coersion to string
		if      (key_up is equal to input) then
			set vol to vol + 1
		else if (key_dn is equal to input) then
			set vol to vol - 1
		end if
		if (vol > 100) then
			set vol to 100
		end if
		if (vol < 0) then
			set vol to 0
		end if
		set volume output volume vol
	end repeat
' 10<&0
