✏️ Added more to help/fixed regex

This commit is contained in:
z3rOR0ne 2022-12-22 19:44:47 -08:00
parent db19605ebe
commit c9f1a4a757

View file

@ -10,15 +10,17 @@ options:
examples: examples:
dye -x "#ffffff" dye -x "#ffffff"
dye -r 255 255 255 dye -r 255 255 255
dye -r "rgb(255, 255, 255)"
dye -x "#43fF6480" dye -x "#43fF6480"
dye -r 67 255 100 0.5 dye -r 67 255 100 0.5
dye -r "rgba(67, 255, 100, 0.5)"
EOM EOM
exit 1 exit 0
} }
error() { error() {
printf "error: %s\n" "$1" 1>&2 printf "error: %s\n" "$1" 1>&2
${2:+exit $2} exit 2
} }
dependencycheck() { dependencycheck() {
@ -73,11 +75,18 @@ hex_to_rgb() {
printf "%s\n" "rgba($r, $g, $b, $a)" printf "%s\n" "rgba($r, $g, $b, $a)"
} }
# parse_rgb() takes rgb string to extract # parse_rgb() takes rgb string to pass to rgb_to_hex()
# numeric values from it and then pass that to rgb_to_hex() parse_rgb() {
# parse_rgb() { local regex='^\s?rgb[a]?[(][0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}[)]$|^rgb[a]?[(][0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}, [0-1]?.[0-9][0-9]?[)]\s?$'
# if [[ $2 =~ $regex ]]; then
# } local numbers
numbers=$(echo "$2" | sed -n 's/.*\(([^()]*)\).*/\1/p' | sed 's/[()]//g' | sed 's/,\s/ /g')
read -r -a rgb <<< "$numbers"
rgb_to_hex "${rgb[$"0"]}" "${rgb[$"1"]}" "${rgb[$"2"]}" "${rgb[$"3"]}"
else
error "pattern passed is not valid rgba format" 2
fi
}
rgb_to_hex() { rgb_to_hex() {
local a local a
@ -99,7 +108,6 @@ main() {
x) x)
if [[ "$#" -gt 2 ]]; then if [[ "$#" -gt 2 ]]; then
error "-x only parses one following argument" error "-x only parses one following argument"
exit 2
fi fi
local rgbval clipit local rgbval clipit
rgbval=$(hex_to_rgb "${2:-''}") rgbval=$(hex_to_rgb "${2:-''}")
@ -111,22 +119,23 @@ main() {
fi fi
;; ;;
r) r)
# parse an 'rgb()' or 'rgba' string here
# regex compare to string
if [[ "$#" -gt 5 ]]; then
error "-r parses up to four following argument"
exit 2
elif [[ "$#" -lt 4 ]]; then
error "-r parses no less than three arguments"
exit 2
fi
local hexval clipit local hexval clipit
hexval=$(rgb_to_hex "${@:2}") if [[ "$#" -eq 2 ]]; then
printf "%s\n" "$hexval" hexval=$(parse_rgb "$@")
read -e -r -p "save code to clipboard?(y/n): " clipit printf "%s\n" "$hexval"
if [[ "$clipit" == "yes" || "$clipit" == "y" ]]; then read -e -r -p "save code to clipboard?(y/n): " clipit
dependencycheck xclip if [[ "$clipit" == "yes" || "$clipit" == "y" ]]; then
printf "%s" "$hexval" | xclip -sel clip dependencycheck xclip
printf "%s" "$hexval" | xclip -sel clip
fi
else
hexval=$(rgb_to_hex "${@:2}")
printf "%s\n" "$hexval"
read -e -r -p "save code to clipboard?(y/n): " clipit
if [[ "$clipit" == "yes" || "$clipit" == "y" ]]; then
dependencycheck xclip
printf "%s" "$hexval" | xclip -sel clip
fi
fi fi
;; ;;
?) ?)