#!/usr/bin/env sh
#
# dependencies: fzf | pastel

# shellcheck disable=SC2016

# print -l ${(z):-{}}
# args="${(z)@}" => args="$(escape "$@")"
# shellcheck disable=SC2312
# escape() {
#   printf "'%s'\\n" "$(printf '%s' "$1" | sed -e "s/'/'\\\\''/g")"
# }

# rs 0 5 => to convert vector into stream
pastel random -n25 \
  | pastel format hex \
  | rs 0 5 \
  | fzf --height=40% \
    --bind='enter:become:print -l ${(z):-{}}' \
    --preview-window=nohidden,up,40% \
    --preview "\
        print -l \${(z):-{}} \
          | xargs -S9999 -I% zsh -c '\
            fg=\$(pastel textcolor \"%\" | pastel mix gray -s RGB -f 0.2); \
            pastel -f paint \${fg} -n -o \"%\" \"\$(pastel format hex \"%\")~\"; \
            pastel -f paint \${fg} -n -o \"%\" \"\$(pastel format name \"%\")~\"; \
            pastel -f paint \${fg} -n -o \"%\" \"\$(pastel format rgb \"%\")~\"; \
            pastel -f paint \"%\" \"  \"
          ' \
          | perl -p \
            -e 's/;(\d)(?!\d\d)/;0\$1/g;' \
            -e 's/;(\d\d)(?!\d)/;0\$1/g;' \
            -e 's/0?(38|48);00(2|5)(?=(;\d\d\d)+)/\$1;\$2/g' \
          | column -ts~ \
      " \
    | pastel -f color
