yad-location-select 1.8 KB

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