diff --git a/papirus-folders b/papirus-folders index d5b407c4..2f47411f 100755 --- a/papirus-folders +++ b/papirus-folders @@ -52,11 +52,13 @@ usage() { cat <<- EOF USAGE $ $PROGNAME [options] -t {-C --color} + $ $PROGNAME [options] -t {-N --new} --shadow <#rrggbb> --accent-dark -<#rrggbb> -accent <#rrggbb> $ $PROGNAME [options] -t {-D --default} $ $PROGNAME [options] -t {-R --restore} OPERATIONS -C --color change color of folders + -N --new create new color scheme and switch to it -D --default back to the default color -R --restore restore the last used color from the config file @@ -113,6 +115,31 @@ _is_valid_color() { return 1 } +# Validate a single hex: must be #rrggbb +_is_valid_hex() { + local hex="$1" + if ! [[ "$hex" =~ ^\#[0-9A-Fa-f]{6}$ ]]; then + err "Invalid hex color: '$hex'. Must be in format #rrggbb" + return 0 + else + return 1 + fi +} + +# Validate all hexes +validate_hexes() { + local colors=("$COLOR_SHADOW" "$COLOR_ACCENT_DARK" "$COLOR_ACCENT") + local validated=1 + for c in "${colors[@]}"; do + if _is_valid_hex "$c"; then + validated=0 + fi + done + if [ ! $validated -eq 1 ]; then + fatal "One or more arguments have invalid hex colors." + fi +} + declare_colors() { local -a colors=() @@ -310,6 +337,34 @@ do_change_color() { esac } +do_new_scheme() { + # Check if the color scheme exist already + if _is_valid_color "$SELECTED_COLOR"; then + # Switch if the scheme already exists + msg 'Theme exists already.' + do_change_color + else + # Check if new hex arguments are valid + validate_hexes + verify_privileges + + # Copy blue file folder + msg "Copying default files to new color scheme..." + local regex_blue=".*-blue\(\.\|\-\).*" + find "$THEME_DIR"/*/places -type f -regex "$regex_blue" -print0 | while IFS= read -r -d '' src; do + dst="${src/blue/${SELECTED_COLOR}}"; + cp -p "$src" "$dst" + done + + # Replace hex values + local regex=".*-${SELECTED_COLOR}\(\.\|\-\).*" + find "$THEME_DIR"/*/places -type f -regex "$regex" -print0 | xargs --null sed -i -e "s/#1d344f/$COLOR_SHADOW/g" -e "s/#4877b1/$COLOR_ACCENT_DARK/g" -e "s/#5294e2/$COLOR_ACCENT/g" + + # Change theme to newly created theme + do_change_color + fi +} + do_revert_default() { verify_privileges @@ -425,9 +480,13 @@ parse_args() { --update-caches) args+=( -u ) ;; --verbose) args+=( -v ) ;; --color|--colour) args+=( -C ) ;; + --new) args+=( -N ) ;; --default) args+=( -D ) ;; --restore) args+=( -R ) ;; --version) args+=( -V ) ;; + --shadow) args+=( -s ) ;; + --accent-dark) args+=( -d ) ;; + --accent) args+=( -a ) ;; --[0-9a-zA-Z]*) err "illegal option -- '$arg'" usage 2 @@ -439,11 +498,14 @@ parse_args() { # Reset the positional parameters to the short options set -- "${args[@]}" - while getopts ":C:DRlot:uvVh" opt; do + while getopts ":C:N:DRlot:uvVh:s:d:a:" opt; do case "$opt" in C ) OPERATIONS+=("change-color") SELECTED_COLOR="$OPTARG" ;; + N ) OPERATIONS+=("new-scheme") + SELECTED_COLOR="$OPTARG" + ;; D ) OPERATIONS+=("revert-default") ;; R ) OPERATIONS+=("restore-color") ;; l ) OPERATIONS+=("list-colors") ;; @@ -455,6 +517,9 @@ parse_args() { exit 0 ;; h ) usage 0 ;; + s ) COLOR_SHADOW="$OPTARG" ;; + d ) COLOR_ACCENT_DARK="$OPTARG" ;; + a ) COLOR_ACCENT="$OPTARG" ;; : ) err "option requires an argument -- '-$OPTARG'" usage 2 ;; @@ -516,6 +581,9 @@ main() { change-color) do_change_color ;; + new-scheme) + do_new_scheme + ;; revert-default) do_revert_default ;;