sound-control.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. else
  28. icon='?'
  29. fi
  30. notify-send -h string:x-canonical-private-synchronous:sound-control-sh \
  31. -t 4000 \
  32. -u normal \
  33. -a "Volume" \
  34. -h int:value:"${vol}" \
  35. "${icon} ${vol}%"
  36. }
  37. function check_number {
  38. if ! [[ -z "${1}" ]] && ! [[ "${1}" =~ '^[0-9]+%?$' ]]; then
  39. echo "error: not a valid value: \"${1}\""
  40. exit 1
  41. fi
  42. }
  43. if [[ "${1}" == '-n' ]]; then
  44. shift 1
  45. notify=0
  46. fi
  47. case "${1}" in
  48. -h|help|'')
  49. print_help
  50. ;;
  51. up)
  52. check_number "${2}"
  53. pamixer -i "${${2:-5}%'%'}"
  54. notify_vol
  55. ;;
  56. down)
  57. check_number "${2}"
  58. pamixer -d "${${2:-5}%'%'}"
  59. notify_vol
  60. ;;
  61. set)
  62. (( ${#} >= 2 )) || { echo 'error: no input value'; exit 1 }
  63. check_number "${2}"
  64. pamixer --set-volume "${2%'%'}"
  65. notify_vol
  66. ;;
  67. mute)
  68. if (( ${#} >= 2 )); then
  69. if [[ "${2:l}" =~ '^y(es?)?$' ]]; then
  70. pamixer -m
  71. elif [[ "${2:l}" =~ '^no?$' ]]; then
  72. pamixer -u
  73. else
  74. echo "error: must be one of 'yes' or 'no': \"${2}\""
  75. exit 1
  76. fi
  77. else
  78. pamixer -t
  79. fi
  80. notify_vol
  81. ;;
  82. notify)
  83. notify_vol
  84. ;;
  85. *)
  86. echo "error: unknown command: \"${1}\""
  87. print_help
  88. exit 1
  89. ;;
  90. esac