radio.sh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env bash
  2. # -----------------------------------------------------------------------------
  3. # Info:
  4. # author: Colin Powell
  5. # file: rofi-radio.sh
  6. # created: 25.10.2020.-08:06:54
  7. # revision: ---
  8. # version: 1.0
  9. # -----------------------------------------------------------------------------
  10. # Requirements:
  11. # rofi
  12. # Description:
  13. # Use rofi to play radio stations
  14. # Usage:
  15. # rofi-radio.sh
  16. # -----------------------------------------------------------------------------
  17. # Script:
  18. # From: https://stackoverflow.com/questions/5014632/how-can-i-parse-a-yaml-file-from-a-linux-shell-script
  19. function parse_yaml {
  20. local prefix=$2
  21. local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
  22. sed -ne "s|^\($s\):|\1|" \
  23. -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
  24. -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
  25. awk -F$fs '{
  26. indent = length($1)/2;
  27. vname[indent] = $2;
  28. for (i in vname) {if (i > indent) {delete vname[i]}}
  29. if (length($3) > 0) {
  30. vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
  31. printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
  32. }
  33. }'
  34. }
  35. # Stations directory
  36. STATIONS_FILE=~/.config/stations.yml
  37. # Save find result to F_ARRAY
  38. readarray -t F_ARRAY <<< $(parse_yaml $STATIONS_FILE)
  39. # Associative array for storing stations
  40. # key => station name
  41. # value => absolute path to the file
  42. # STATIONS['filename']='path'
  43. declare -A STATIONS
  44. # Add elements to STATIONS array
  45. get_stations() {
  46. # if [ ${#F_ARRAY[@]} != 0 ]; then
  47. if [[ ! -z ${F_ARRAY[@]} ]]; then
  48. for i in "${!F_ARRAY[@]}"
  49. do
  50. path=${F_ARRAY[$i]}
  51. file=$(basename "${F_ARRAY[$i]}")
  52. STATIONS+=(["$file"]="$path")
  53. done
  54. else
  55. echo "$STATIONS_DIR is empty!"
  56. echo "Please put some stations in it."
  57. echo "Only .pdf files are accepted."
  58. exit
  59. fi
  60. }
  61. # List for rofi
  62. gen_list(){
  63. for i in "${!STATIONS[@]}"
  64. do
  65. echo "$i"
  66. done
  67. }
  68. main() {
  69. get_stations
  70. station=$( (gen_list) | rofi -dmenu -i -matching fuzzy -no-custom -location 0 -p "Book > " )
  71. if [ -n "$station" ]; then
  72. echo $station
  73. echo $STATIONS[$station]
  74. fi
  75. }
  76. main
  77. exit 0