fzfimg.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. # This is just an example how ueberzug can be used with fzf.
  4. # Copyright (C) 2019 Nico Bäurer
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. readonly BASH_BINARY="$(which bash)"
  16. readonly REDRAW_COMMAND="toggle-preview+toggle-preview"
  17. readonly REDRAW_KEY="µ"
  18. declare -r -x DEFAULT_PREVIEW_POSITION="right"
  19. declare -r -x UEBERZUG_FIFO="$(mktemp --dry-run --suffix "fzf-$$-ueberzug")"
  20. declare -r -x PREVIEW_ID="preview"
  21. function is_option_key [[ "${@}" =~ ^(\-.*|\+.*) ]]
  22. function is_key_value [[ "${@}" == *=* ]]
  23. function map_options {
  24. local -n options="${1}"
  25. local -n options_map="${2}"
  26. for ((i=0; i < ${#options[@]}; i++)); do
  27. local key="${options[$i]}" next_key="${options[$((i + 1))]:---}"
  28. local value=true
  29. is_option_key "${key}" || \
  30. continue
  31. if is_key_value "${key}"; then
  32. <<<"${key}" \
  33. IFS='=' read key value
  34. elif ! is_option_key "${next_key}"; then
  35. value="${next_key}"
  36. fi
  37. options_map["${key}"]="${value}"
  38. done
  39. }
  40. function parse_options {
  41. declare -g -a script_options=("${@}")
  42. declare -g -A mapped_options
  43. map_options script_options mapped_options
  44. declare -g -r -x PREVIEW_POSITION="${mapped_options[--preview-window]%%:[^:]*}"
  45. }
  46. function start_ueberzug {
  47. mkfifo "${UEBERZUG_FIFO}"
  48. <"${UEBERZUG_FIFO}" \
  49. ueberzug layer --parser bash --silent &
  50. # prevent EOF
  51. 3>"${UEBERZUG_FIFO}" \
  52. exec
  53. }
  54. function finalise {
  55. 3>&- \
  56. exec
  57. &>/dev/null \
  58. rm "${UEBERZUG_FIFO}"
  59. &>/dev/null \
  60. kill $(jobs -p)
  61. }
  62. function calculate_position {
  63. # TODO costs: creating processes > reading files
  64. # so.. maybe we should store the terminal size in a temporary file
  65. # on receiving SIGWINCH
  66. # (in this case we will also need to use perl or something else
  67. # as bash won't execute traps if a command is running)
  68. < <(</dev/tty stty size) \
  69. read TERMINAL_LINES TERMINAL_COLUMNS
  70. case "${PREVIEW_POSITION:-${DEFAULT_PREVIEW_POSITION}}" in
  71. left|up|top)
  72. X=1
  73. Y=1
  74. ;;
  75. right)
  76. X=$((TERMINAL_COLUMNS - COLUMNS - 2))
  77. Y=1
  78. ;;
  79. down|bottom)
  80. X=1
  81. Y=$((TERMINAL_LINES - LINES - 1))
  82. ;;
  83. esac
  84. }
  85. function draw_preview {
  86. calculate_position
  87. >"${UEBERZUG_FIFO}" declare -A -p cmd=( \
  88. [action]=add [identifier]="${PREVIEW_ID}" \
  89. [x]="${X}" [y]="${Y}" \
  90. [width]="${COLUMNS}" [height]="${LINES}" \
  91. [scaler]=forced_cover [scaling_position_x]=0.5 [scaling_position_y]=0.5 \
  92. [path]="${@}")
  93. # add [synchronously_draw]=True if you want to see each change
  94. }
  95. function print_on_winch {
  96. # print "$@" to stdin on receiving SIGWINCH
  97. # use exec as we will only kill direct childs on exiting,
  98. # also the additional bash process isn't needed
  99. </dev/tty \
  100. exec perl -e '
  101. require "sys/ioctl.ph";
  102. while (1) {
  103. local $SIG{WINCH} = sub {
  104. ioctl(STDIN, &TIOCSTI, $_) for split "", join " ", @ARGV;
  105. };
  106. sleep;
  107. }' \
  108. "${@}" &
  109. }
  110. if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
  111. trap finalise EXIT
  112. parse_options "${@}"
  113. # print the redraw key twice as there's a run condition we can't circumvent
  114. # (we can't know the time fzf finished redrawing it's layout)
  115. print_on_winch "${REDRAW_KEY}${REDRAW_KEY}"
  116. start_ueberzug
  117. export -f draw_preview calculate_position
  118. SHELL="${BASH_BINARY}" \
  119. fzf --preview "draw_preview {}" \
  120. --preview-window "${DEFAULT_PREVIEW_POSITION}" \
  121. --bind "${REDRAW_KEY}:${REDRAW_COMMAND}" \
  122. "${@}"
  123. fi