Skip to content

Instantly share code, notes, and snippets.

@ohnonot
Last active March 25, 2018 18:20
Show Gist options
  • Save ohnonot/4eefc44245f21cfb4462a95e1beed0ed to your computer and use it in GitHub Desktop.
Save ohnonot/4eefc44245f21cfb4462a95e1beed0ed to your computer and use it in GitHub Desktop.
printVolCont - print ascii volume bar
#!/bin/bash
# printVolCont
# continuously print alsa or pulse master volume as ascii slider
# to be used with tint2 executor (example at the bottom).
# needs: awk, sed, grep, alsactl
# 2017/2018 by bronto, fixed by o9000
debug="false"
tooltip=false
channel=Master
chars="20"
# ─│� Ξ · ‖ ǁ ‖ • © ┼ × ┬ ┤├ ≠ ┴ ┬ ∈∋ ∊∍ ∞ ∅ ┉ ╋ ☼ ┯ ━ ◘ ◙ ○ ▓ ▮ ▒ ░ ǂ Ѫ Ж ф Ѳ ⑉ ⑄ ╵╷ ╴
begin="┌"
end="┐"
fil="─"
pos="┴" # will default to percentage
pos_mut="•"
# pulse
# https://unix.stackexchange.com/questions/132230/read-out-pulseaudio-volume-from-commandline-i-want-pactl-get-sink-volume
pulse () {
sink=$( pactl list short sinks | sed -e 's,^\([0-9][0-9]*\)[^0-9].*,\1,' | head -n 1 )
volPercents=$( pactl list sinks | grep '^[[:space:]]Volume:' | head -n $(( sink + 1 )) | tail -n 1 | sed -e 's,.* \([0-9][0-9]*\)%.*,\1,' )
# missing mute
mute=$( pactl list sinks | grep 'Mute' | head -n $(( sink + 1 )) )
# patch mute state to alsa (where 'off' equals 'Mute: yes')
if [[ "$mute" = *"yes"* ]]; then
mute="off"
fi
# i'm pulse
mode=" Pulse" # Pulse
}
# alsa
alsa () {
# -M: same as alsamixer, more human
mapfile -t <<<$(amixer -M sget $channel)
volPercents="${MAPFILE[$((${#MAPFILE[@]} - 1))]}"
unset MAPFILE
mute="${volPercents##*\[}"
mute="${mute%%]*}"
volPercents="${volPercents%%\%]*}"
volPercents="${volPercents##*\[}"
# i'm alsa
mode="" # Alsa
}
alsaOrPulse () {
alsa
#echo "alsa"
if [[ -z "$volPercents" ]]; then
pulse
#echo "pulse"
fi
}
[[ "$fil" == "" ]] && fil="-"
(echo ""; stdbuf -oL alsactl monitor) |
while read
do
#alsa || pulse # if alsa function errors, use pulse function
alsaOrPulse
if (( volPercents > 100 )); then
volPercents=100
fi
if (( volPercents < 0 )); then
volPercents=0
fi
# if mute
if [ "$mute" == "off" ]; then
[[ "$pos_mut" != "" ]] && posnow="$pos_mut" || posnow="M"
tooltip="$channel Muted $volPercents%"
else
[[ "$pos" == "" ]] && posnow="$volPercents" || posnow="$pos"
tooltip="Volume $channel $volPercents%"
fi
[ "$debug" = true ] && printf "$posnow posnow\n$volPercents volPercents\n"
preloop=$(( volPercents * chars / 100 ))
[ "$debug" = true ] && echo "$preloop preloop"
# take width of current positional string into account, so that the bar's
# width stays the same (at least with monospaced fonts)
if (( preloop == chars )); then
postloop=0
preloop=$(( preloop - ${#posnow} ))
elif (( preloop == 0 )); then
postloop=$(( chars - ${#posnow} ))
else
postloop=$(( chars - preloop ))
if (( postloop > preloop )); then
postloop=$(( postloop - ${#posnow} / 2 - ${#posnow} % 2 ))
preloop=$(( preloop - ${#posnow} / 2 ))
else
preloop=$(( preloop - ${#posnow} / 2 - ${#posnow} % 2 ))
postloop=$(( postloop - ${#posnow} / 2 ))
fi
fi
[ "$debug" = true ] && { echo "$preloop preloop" ;}
[ "$debug" = true ] && { echo "$postloop postloop" ;}
string=""
for ((i=0;i<preloop;i++)); do
string="$string$fil"
done
string="$string$posnow"
for ((i=0;i<postloop;i++)); do
string="$string$fil"
done
printf "%s%s%s%s\n" "$begin" "$string" "$end" "$mode"
[[ "$tooltip" == "true" ]] && >&2 printf '\e[2J%s' "$tooltip"
done
# tint2 executor example:
#~ execp = new
#~ execp_centered = 1
#~ execp_has_icon = 0
#~ execp_command = printVolCont
#~ execp_continuous = 1
#~ execp_font = Misc Fixed Bold Semi-Condensed 10
#~ execp_font_color = #ffffff 55
#~ execp_padding = 4 0 0
#~ execp_mclick_command = amixer set Master toggle
#~ execp_rclick_command = amixer set Master 5%+
#~ execp_lclick_command = amixer set Master 5%-
#~ execp_uwheel_command = amixer set Master 1-
#~ execp_dwheel_command = amixer set Master 1+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment