location-select 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. [[ "$0" == *'/'* ]] && { cd "${0%/*}" || exit 1; }
  3. for d in yad grep sed ./get_location_id; do
  4. type -f $d >/dev/null || exit 1
  5. done
  6. locations_file="files/selected_locations"
  7. conf="files/get_weather.d/conf"
  8. nl=$'\n'
  9. result=''
  10. selloc=() # selected locations
  11. yadcmd=(yad --center --window-icon weather --title "Weather locations")
  12. search_and_select() {
  13. query=''
  14. while [ -z "$query" ]; do
  15. fuzzy=''
  16. query="$("${yadcmd[@]}" --entry-label "Enter location query" --button=Search:0 --button=Fuzzy\ search:222 --button=Cancel:252 --entry)"
  17. case $? in
  18. 252) return
  19. ;;
  20. 222) fuzzy="-f"
  21. ;;
  22. esac
  23. done
  24. mapfile -t morloc < <(./get_location_id $fuzzy "$query")
  25. result="$("${yadcmd[@]}" --entry-label "Add location" --button=OK:0 --button=Cancel:252 --entry "${morloc[@]}")"
  26. case $? in
  27. 252) return
  28. ;;
  29. esac
  30. }
  31. if [ -r "$locations_file" ]; then
  32. mapfile -t selloc <"$locations_file"
  33. else
  34. search_and_select
  35. fi
  36. while [ -z "$result" ]; do
  37. result="$("${yadcmd[@]}" --entry-label "Choose location" --button=OK:0 --button=Add\ more:222 --button=Cancel:252 --entry "${selloc[@]}")"
  38. case $? in
  39. 252) exit
  40. ;;
  41. 222) search_and_select
  42. ;;
  43. esac
  44. result="${result%"${result##*[![:space:]]}"}"
  45. selloc=( "$result" "$(for i in "${selloc[@]}"; do [[ "$i" == "$result" ]] && continue; echo "$i"; done)" )
  46. done
  47. ere='^[[:space:]]*geoid=[0-9]+'
  48. [ -r "$conf" ] && grep -qE "$ere" "$conf" && sed -Ei "s/$ere/geoid=${result%% *}/" "$conf" || echo "geoid=${result%% *}" >> "$conf"
  49. conky -c conkyrc.lua &
  50. for i in "${selloc[@]}"; do echo "$i"; done > "$locations_file"