diff options
author | Saumit Dinesan <justsaumit@protonmail.com> | 2022-09-30 06:21:32 +0530 |
---|---|---|
committer | Saumit Dinesan <justsaumit@protonmail.com> | 2022-09-30 06:21:32 +0530 |
commit | 5fd7b9cebdca3f7fda60bb35d3d19bab9d06b2bb (patch) | |
tree | df2ee429955ebed55d20704da238bf11b2a12f7f /.local/usr/wal-telegram/wal-telegram | |
parent | 1d70accabd7e66dc4fc3ec256da3eb383e59e528 (diff) |
wal-telegram changes
Diffstat (limited to '.local/usr/wal-telegram/wal-telegram')
-rwxr-xr-x | .local/usr/wal-telegram/wal-telegram | 212 |
1 files changed, 192 insertions, 20 deletions
diff --git a/.local/usr/wal-telegram/wal-telegram b/.local/usr/wal-telegram/wal-telegram index 0a1f24b..1585841 100755 --- a/.local/usr/wal-telegram/wal-telegram +++ b/.local/usr/wal-telegram/wal-telegram @@ -1,20 +1,182 @@ #!/usr/bin/env bash # -# A script to create Telegram palettes that use colors generated by pywal/wal. -# Requires pywal or wal, bash and cat. +# A script to create Telegram palettes that use colors generated by pywal/wal +# or from a different file containing the hex colors (see the README.md for the +# format of the file). + +# Help function called by the --help|-h parameter. + +set -E +trap '[ "$?" -ne 77 ] || exit 77' ERR + +help() { + cat <<EOF +A script to create Telegram palettes that use colors generated by pywal/wal +or from a different file containing the hex colors (see the README.md for the +format of the file). + +Usage: + $0 -w | --wal [-t|--tiled] [-b|--background=<path_to_background_image>] [-d|--destination=<destination_path>] [-g|--gaussian-blur=<blur_argument>] [-r|--restart] + $0 -p | --palette=</path/to/palette> [-t|--tiled] [-b|--background=<path_to_background_image>] [-d|--destination=<destination_path>] [-g|--gaussian-blur=<blur_argument>] [-r|--restart] + $0 -h | --help + +Options: + -h Show command usage. + --help Show this help screen. + -w --wal Generate the color palette using the colors generated + by pywal/wal (Default). + -p --palette Use colors from an sh file to generate the color palette. + -t --tiled Use a unified color background instead of an image. + -b --background Use a specific image as background. + -d --destination Specify a destination folder for the generated palette. + (Default: ~/.cache/wal-telegram) + -g --gaussian-blur Blur the background image, takes a blur argument. + see https://legacy.imagemagick.org/Usage/blur/#blur_args. + -g takes no argument and uses the default value. + (Default: 0x16) + -r --restart Restart the telegram app after generation. +EOF +} +usage() { + cat <<EOF +Usage: + $0 --wal [--tiled] [--background=<path_to_background_image>] [--destination=<destination_path>] [-g|--gaussian-blur=<blur_argument (default: 0x16)>] [-r|--restart] + $0 --palette=<path_to_sh_file> [--tiled] [--background=<path_to_background_image>] [--destination=<destination_path>] [-g|--gaussian-blur=<blur_argument (default: 0x16)>] [-r|--restart] +EOF +} +exit_abnormal(){ + usage + exit 1 +} + +script_dir=$(readlink -f ${0} | sed "s#/$(basename ${0})##") # shellcheck source=/dev/null -. "${HOME}/.cache/wal/colors.sh" +command -v zip > /dev/null || (printf '%b\n' "\\e[1;31m::\\e[0m \\e[1;37mA zipping utility is needed for this script, consider installing zip or gzip\\e[0m" && exit 77) + +options=$(getopt -o hwtp:b:d:gr --long help,wal,tiled,palette:,background:,destination:,gaussian-blur:,restart -- "$@") +if [ $? != 0 ] ; then exit_abnormal; fi +eval set -- "$options" + +while true; do + case "$1" in + -h) usage; exit 0 ;; + --help) help; exit 0 ;; + -w | --wal ) + if [[ "$mode" == 'palette' ]]; then + printf '%b\n\n' "\\e[1;31m::\\e[0m \\e[1;37mCannot use wal and palette together.\\e[0m" + exit_abnormal + fi + if [[ ! -d "${XDG_CACHE_HOME:-${HOME}/.cache}/wal" ]]; then + printf '%b\n\n' "\\e[1;31m::\\e[0m \\e[1;37mWal must be run before running wal-telegram.\\e[0m" + exit_abnormal + fi + mode='wal' + shift ;; + -t | --tiled ) + bg_mode="tiled" + shift ;; + -p | --palette ) + if [[ "$mode" == 'wal' ]]; then + printf '%b\n\n' "\\e[1;31m::\\e[0m \\e[1;37mCannot use wal and palette together.\\e[0m" + exit_abnormal + fi + if [[ ! -f "$2" ]]; then + printf '%b\n\n' "\\e[1;31m::\\e[0m \\e[1;37mPalette file '$2' could not be found.\\e[0m" + exit_abnormal + fi + mode='palette' + palette_file=$2 + shift 2 ;; + -b | --background ) + if [[ ! -f "$2" ]]; then + printf '%b\n\n' "\\e[1;31m::\\e[0m \\e[1;37mBackground file '$2' could not be found.\\e[0m" + exit_abnormal + fi + bg="$2" + shift 2 ;; + -d | --destination ) + if (mkdir -p "$2" 2> /dev/null); then + dest="$2" + else + printf '%b\n\n' "\\e[1;31m::\\e[0m \\e[1;37mCould not create '$2' verify the permissions in it's parent directory and retry.\\e[0m" + exit_abnormal + fi + shift 2 ;; + -g) + if [ -n "$blur" ]; then + printf '%b\n\n' "\\e[1;31m::\\e[0m \\e[1;37mDo not set -g and --gaussian-blur together.\\e[0m" + exit_abnormal + fi + blur="0x16" + shift ;; + --gaussian-blur ) + if [ -n "$blur" ]; then + printf '%b\n\n' "\\e[1;31m::\\e[0m \\e[1;37mDo not set -g and --gaussian-blur together.\\e[0m" + exit_abnormal + fi + if [ -z "$2" ] || [ "$(expr "$2" : '^[[:digit:]]\+x[[:digit:]]\+$')" -eq 0 ]; then + printf '%b\n\n' "\\e[1;31m::\\e[0m \\e[1;37mBlur option not recognized, see https://legacy.imagemagick.org/Usage/blur/#blur_args for reference.\\e[0m" + exit_abnormal + fi + blur="$2" + shift 2 ;; + -r | --restart ) + restart_on_gen=true + shift ;; + -- ) shift; break ;; + * ) break ;; + esac +done +# default values +[ -z "$mode" ] && mode='wal' +[ -z "$bg_mode" ] && bg_mode='background' +[ -z "$bg" ] && bg='wal' + +# source this here in case wal is not an option +if [[ "$mode" == 'wal' ]]; then + . "${XDG_CACHE_HOME:-${HOME}/.cache}/wal/colors.sh" +else + . "$palette_file" +fi + +if [[ "$bg_mode" == 'tiled' ]] && [[ "$bg" != 'wal' ]]; then + printf '%b\n' "\\e[1;31m::\\e[0m \\e[1;37mBackground option while using tiled mode is useless.\\e[0m" +fi # Prepare the conditions for the other functions. prepare() { - pre="${HOME}/.cache/wal-telegram" + pre="/tmp/wal-telegram/$(date +%s)" + [ -z "$dest" ] && dest="${XDG_CACHE_HOME:-${HOME}/.cache}/wal-telegram" mkdir -p "$pre" - rm -f "${pre}/background".* - rm -f "${pre}/colors.tdesktop-palette" - wal_path="$(<"${HOME}/.cache/wal/wal")" - wal_ext="${wal_path##*.}" - cp "$wal_path" "${pre}/background.${wal_ext}" + mkdir -p "$dest" + rm -f "${dest}/${mode}.tdesktop-theme" + + if [[ "$bg" == "wal" ]]; then # if no background is specified + if [[ "$mode" == wal ]] && [[ "$bg_mode" == "background" ]]; then + # wal mode without tiled + bg_path="$(<"${XDG_CACHE_HOME:-${HOME}/.cache}/wal/wal")" + bg_ext="${bg_path##*.}" + cp "$bg_path" "${pre}/background.${bg_ext}" + elif [[ "$mode" == "palette" ]] && [[ "$bg_mode" == "background" ]]; then + if [[ -f "${HOME}/.fehbg" ]]; then + bg_path="$(tail -n1 "${HOME}/.fehbg" \ + | sed "s/[^']*//" \ + | awk '{print $1}' \ + | sed "s/'//g")" + bg_ext="${bg_path##*.}" + if [[ -f "$bg_path" ]]; then + cp "$bg_path" "${pre}/background.${bg_ext}" + fi + else # if .fehbg exists + printf '%b\n' "\\e[1;31m::\\e[0m \\e[1;37m~/.fehbg not found, falling back to tiled background.\\e[0m" + bg="tiled" + fi # if .fehbg exists + fi # elif mode == palette + else # if bg == wal + bg_ext="${bg##*.}" + cp "$bg" "${pre}/background.${bg_ext}" + fi # if bg == wal } # Lighten/darken by 20%, 30%, 40%, 50%, 60% some colors using only pure bash. @@ -154,10 +316,10 @@ create_colors() { } # Create colors.tdesktop-palette using the colors generated by create_colors() -# and the colors.wal-constants file. +# and the colors.wt-constants file. create_palette() { - cat <<EOF > "${pre}/colors.tdesktop-palette" -// Colors imported from wal by init.sh. + cat <<EOF > "${pre}/colors.tdesktop-theme" +// Colors imported by wal-telegram. color0: $color0; color1: $color1; color2: $color2; @@ -168,7 +330,7 @@ color6: $color6; color7: $color7; color8: $color8; -// Lighter and darker variants of wal's colors. +// Lighter and darker variants of the colors. colorLighter0_20: $color0_lighter_20; colorLighter0_30: $color0_lighter_30; colorLighter0_40: $color0_lighter_40; @@ -224,7 +386,8 @@ colorAlpha0_74: ${color0}74; colorAlpha0_40: ${color0}40; colorAlpha0_4c: ${color0}4c; colorAlpha0_b2: ${color0}b2; -colorAlpha0_40: ${color0}40; +colorAlpha0_f2: ${color0}f2; +colorAlpha0_66: ${color0}66; colorAlpha1_10: ${color1}10; colorAlpha1_33: ${color1}33; colorAlpha2_c8: ${color2}c8; @@ -243,15 +406,24 @@ colorAlpha7_4c: ${color7}4c; colorAlpha7_6b: ${color7}6b; colorAlpha7_14: ${color7}14; EOF - const="$(<".local/usr/wal-telegram/colors.wal-constants")" - printf '%s' "$const" >> "${pre}/colors.tdesktop-palette" + const="$(<"$script_dir/colors.wt-constants")" + printf '%s' "$const" >> "${pre}/colors.tdesktop-theme" + cd "${pre}" || exit 1; + if [[ -z $bg_ext ]]; then bg_ext="png"; fi + if [[ -n $blur ]]; then blur="-blur $blur"; fi + if [[ "$bg_mode" = "tiled" ]]; then convert -size 256x256 "xc:$color0" tiled.png; + elif [[ "$bg_mode" = "background" ]]; then + size=$(magick identify -format "%w" ${bg_mode}.${bg_ext}) + # shellcheck disable=SC2086 + convert ${bg_mode}.${bg_ext} $blur -resize $(( size > 1920 ? size : 1920 )) ${bg_mode}.png + fi + zip -q ${mode}.tdesktop-theme colors.tdesktop-theme ${bg_mode}.png + cp ${mode}.tdesktop-theme "$dest" + [ -n "$restart_on_gen" ] && pkill -f telegram-desktop && telegram-desktop & } main() { - prepare - create_colors - create_palette - printf '%s\n' "Theme created succesfully." + prepare && create_colors && create_palette && printf '%b\n' "\\e[1;31m::\\e[0m \\e[1;37mPalette generated succesfully.\\e[0m" } main |