stk-tracks 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/bin/bash
  2. printf '\033]2;Browse and edit STK addon tracks\a'
  3. dirs="$HOME/.local/share/supertuxkart/addons/tracks"
  4. id=Zaphod
  5. putimg() {
  6. {
  7. printf "{"
  8. printf '"%s": "%s", ' "action" "add" "identifier" "$id" "scaler" "distort" "x" "0" "y" "0" "width" "$width" "height" "$height" "path" "$1"
  9. printf '"%s": "%s" }\n' "synchronously_draw" "True"
  10. } > "$UEBERZUG_FIFO"
  11. }
  12. remimg() {
  13. {
  14. printf "{"
  15. printf '"%s": "%s", ' "action" "remove" "identifier" "$id"
  16. printf '"%s": "%s" }\n' "synchronously_draw" "True"
  17. } > "$UEBERZUG_FIFO"
  18. }
  19. getattr() {
  20. xmllint --nonet --xpath "string(//track/@$1)" - <<<"$xml"
  21. }
  22. cleanup() {
  23. tabs
  24. rm -f "$UEBERZUG_FIFO"
  25. tput cnorm
  26. }
  27. do_setup() {
  28. width=$(( (COLUMNS/3)*2 ))
  29. height=$(( LINES ))
  30. rest="$((COLUMNS - (width+1) ))"
  31. tabs $((width+1))
  32. }
  33. tab() { echo -en '\t'; }
  34. tablines() { while read -r line; do tab; echo "$line"; done; }
  35. f() { fold -s -w "$rest"; }
  36. tput civis
  37. trap cleanup EXIT
  38. setup=1
  39. offset=''
  40. while getopts "no:" opt; do
  41. case $opt in
  42. n)
  43. setup=0
  44. ;;
  45. o)
  46. offset="$OPTARG"
  47. ;;
  48. esac
  49. done
  50. export UEBERZUG_FIFO="/tmp/stk-tracks-ueberzug-fifo"
  51. rm -f "$UEBERZUG_FIFO"
  52. mkfifo "$UEBERZUG_FIFO"
  53. ueberzug layer --parser json < "$UEBERZUG_FIFO" &
  54. exec 3>"$UEBERZUG_FIFO"
  55. [[ "$setup" == 0 ]] && do_setup || {
  56. testimg="/usr/share/supertuxkart/data/gamerzilla/supertuxkart.png"
  57. #~ /usr/share/supertuxkart/data/skins/cartoon/background.jpg
  58. #~ /usr/share/supertuxkart/data/skins/common/background.jpg
  59. #~ /usr/share/supertuxkart/data/tracks/cornfield_crossing/screenshot.jpg
  60. txt="Terminal setup\n==============
  61. The image should be roughly 4:3 and there must be at least 20 char width to the right of it."
  62. OK=0
  63. wait=2 # seconds to wait before redraw
  64. while [[ "${OK,}" != y ]]; do
  65. clear
  66. (( COLUMNS<60 || LINES<10 )) && {
  67. remimg
  68. echo -e "${txt}\n\nTerminal too small. Current size: ${COLUMNS}x${LINES}. Please resize to at least 60x10, recommended: 80x24." | fold -s -w $COLUMNS
  69. sleep $wait
  70. } || {
  71. do_setup
  72. putimg "$testimg"
  73. echo -e "${txt}\n\nIf you're happy with the new size, press y _" | f | tablines
  74. read -s -n1 -t $wait OK
  75. }
  76. done
  77. }
  78. txt="\n e - edit\n n - next (default)\n r - remove track\n q - Quit"
  79. for dir in "$dirs"/*; do
  80. [[ "${dir##*/}" == "$offset"* ]] || continue
  81. offset=""
  82. [ -r "$dir/track.xml" ] || continue
  83. xml="$(<"$dir/track.xml")"
  84. clear
  85. putimg "$dir/$(getattr screenshot)"
  86. {
  87. echo "Track:"
  88. getattr name | f
  89. echo -e "Directory:\n${dir##*/}"
  90. echo -n "Laps: "
  91. getattr default-number-of-laps
  92. } | tablines
  93. echo -e "$txt" | tablines
  94. while { tab; read -s -n1 -p "Action? _" x; }; do
  95. case "${x,}" in
  96. e) remimg; nano --softwrap --atblanks "$dir/track.xml"; tput civis; putimg "$dir/$(getattr screenshot)"
  97. ;;
  98. n|'') break
  99. ;;
  100. r) echo -e "\nReally delete ~${dir#$HOME} (y/N)? _" | f | tablines; read -s -n1 y
  101. [[ "${y,}" == y ]] && rm -r "$dir" && break
  102. ;;
  103. q) clear && exit
  104. ;;
  105. *) echo -e "\n\tinvalid input: $x"
  106. ;;
  107. esac
  108. done
  109. remimg
  110. done