sound-control.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env zsh
  2. setopt rcquotes
  3. function print_help {
  4. echo "usage: sound-control.sh [-n] [-h] <command>"
  5. echo 'Options:'
  6. echo ' -h: print this message, then exit'
  7. echo ' -n: don''t notify the volume'
  8. echo 'Commands:'
  9. echo ' up: increase the volume (default: 5%)'
  10. echo ' down: decrease the volume (default: 5%)'
  11. echo ' set: set the volume'
  12. echo ' mute: toggle the mute with no argument, or set it with one'
  13. echo ' help: print this message, then exit'
  14. echo ' notify: notify the volume, then exit (do nothing with -n)'
  15. }
  16. let notify=1
  17. function notify_vol {
  18. ((${notify})) || return
  19. let vol="$(pamixer --get-volume)"
  20. local icon
  21. if [[ "$(pamixer --get-mute)" == 'true' ]]; then
  22. icon='󰸈'
  23. elif (( ${vol} >= 50 )); then
  24. icon='󰕾'
  25. elif ((${vol} > 0)); then
  26. icon='󰖀'
  27. elif ((${vol} == 0)); then
  28. icon='󰕿'
  29. else
  30. icon='?'
  31. fi
  32. notify-send -h string:x-canonical-private-synchronous:sound-control-sh \
  33. -t 4000 \
  34. -u normal \
  35. -a "Volume" \
  36. -h int:value:"${vol}" \
  37. "${icon} ${vol}%"
  38. }
  39. function check_number {
  40. if ! [[ -z "${1}" ]] && ! [[ "${1}" =~ '^[0-9]+%?$' ]]; then
  41. echo "error: not a valid value: \"${1}\""
  42. exit 1
  43. fi
  44. }
  45. if [[ "${1}" == '-n' ]]; then
  46. shift 1
  47. notify=0
  48. fi
  49. case "${1}" in
  50. -h|help|'')
  51. print_help
  52. ;;
  53. up)
  54. check_number "${2}"
  55. pamixer -i "${${2:-5}%'%'}"
  56. notify_vol
  57. ;;
  58. down)
  59. check_number "${2}"
  60. pamixer -d "${${2:-5}%'%'}"
  61. notify_vol
  62. ;;
  63. set)
  64. (( ${#} >= 2 )) || { echo 'error: no input value'; exit 1 }
  65. check_number "${2}"
  66. pamixer --set-volume "${2%'%'}"
  67. notify_vol
  68. ;;
  69. mute)
  70. if (( ${#} >= 2 )); then
  71. if [[ "${2:l}" =~ '^y(es?)?$' ]]; then
  72. pamixer -m
  73. elif [[ "${2:l}" =~ '^no?$' ]]; then
  74. pamixer -u
  75. else
  76. echo "error: must be one of 'yes' or 'no': \"${2}\""
  77. exit 1
  78. fi
  79. else
  80. pamixer -t
  81. fi
  82. notify_vol
  83. ;;
  84. notify)
  85. notify_vol
  86. ;;
  87. *)
  88. echo "error: unknown command: \"${1}\""
  89. print_help
  90. exit 1
  91. ;;
  92. esac