Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion papirus-folders
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ usage() {
cat <<- EOF
USAGE
$ $PROGNAME [options] -t <theme> {-C --color} <color>
$ $PROGNAME [options] -t <theme> {-N --new} <color> --shadow <#rrggbb> --accent-dark -<#rrggbb> -accent <#rrggbb>
$ $PROGNAME [options] -t <theme> {-D --default}
$ $PROGNAME [options] -t <theme> {-R --restore}

OPERATIONS
-C --color <color> change color of folders
-N --new <color> 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

Expand Down Expand Up @@ -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=()

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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") ;;
Expand All @@ -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
;;
Expand Down Expand Up @@ -516,6 +581,9 @@ main() {
change-color)
do_change_color
;;
new-scheme)
do_new_scheme
;;
revert-default)
do_revert_default
;;
Expand Down