diff --git a/README.md b/README.md index 61ff282..6883120 100644 --- a/README.md +++ b/README.md @@ -84,20 +84,22 @@ You can define your preferred defaults in `~/.config/color-tool/config.toml`. Th ```toml [defaults] -output = "hex" # default output format(s) +# Set any option to true to enable it by default when using the terminal +output = "hex" # default output format(s): hex, rgb, hsl, rgba, hsla, hexa, all json = false # output in JSON format swatch = false # show color swatch in terminal -name = false # fetch color name +name = false # fetch color name from thecolorapi.com copy = false # copy result to clipboard -pick = false # auto-launch picker +pick = false # auto-launch color picker when invoked with no arguments notify = false # show desktop notification [desktop] -output = "hex" -json = false -name = false -copy = true -notify = true +# Defaults for --desktop mode (launched from the app menu; copy is always enabled by default) +output = "hex" # format to copy +json = false # copy JSON format instead of plain text +name = false # fetch color name (requires network) +copy = true # copy result to clipboard +notify = true # show desktop notification with the copied value ``` ## 🤝 Credits diff --git a/color-tool b/color-tool index c58123b..616a847 100755 --- a/color-tool +++ b/color-tool @@ -63,7 +63,7 @@ load_config() { local section="" key val while IFS= read -r line; do - line="${line%%#*}" # strip inline comments + line="${line%%#*}" # strip inline comments if [[ "$line" =~ ^[[:space:]]*$ ]]; then continue; fi # Section header @@ -76,25 +76,26 @@ load_config() { if [[ "$line" =~ ^[[:space:]]*([A-Za-z_]+)[[:space:]]*=[[:space:]]*([^[:space:]]+) ]]; then key="${BASH_REMATCH[1]}" val="${BASH_REMATCH[2],,}" - val="${val#\"}" ; val="${val%\"}" + val="${val#\"}" + val="${val%\"}" case "$section:$key" in - defaults:json) [[ "$val" == "true" ]] && json_mode=1 || json_mode=0 ;; - defaults:alpha) [[ "$val" == "true" ]] && alpha_mode=1 || alpha_mode=0 ;; - defaults:name) [[ "$val" == "true" ]] && name_mode=1 || name_mode=0 ;; - defaults:copy) [[ "$val" == "true" ]] && copy_mode=1 || copy_mode=0 ;; - defaults:pick) [[ "$val" == "true" ]] && config_pick=1 || config_pick=0 ;; - defaults:notify) [[ "$val" == "true" ]] && notify_mode=1 || notify_mode=0 ;; - defaults:swatch) [[ "$val" == "true" ]] && swatch_mode=1 || swatch_mode=0 ;; - defaults:output) output_formats="$val" ;; - desktop:json) [[ "$val" == "true" ]] && desktop_json=1 || desktop_json=0 ;; - desktop:alpha) [[ "$val" == "true" ]] && desktop_alpha=1 || desktop_alpha=0 ;; - desktop:name) [[ "$val" == "true" ]] && desktop_name=1 || desktop_name=0 ;; - desktop:notify) [[ "$val" == "true" ]] && desktop_notify=1 || desktop_notify=0 ;; - desktop:copy) [[ "$val" == "true" ]] && desktop_copy=1 || desktop_copy=0 ;; - desktop:output) desktop_output="$val" ;; + defaults:json) [[ "$val" == "true" ]] && json_mode=1 || json_mode=0 ;; + defaults:alpha) [[ "$val" == "true" ]] && alpha_mode=1 || alpha_mode=0 ;; + defaults:name) [[ "$val" == "true" ]] && name_mode=1 || name_mode=0 ;; + defaults:copy) [[ "$val" == "true" ]] && copy_mode=1 || copy_mode=0 ;; + defaults:pick) [[ "$val" == "true" ]] && config_pick=1 || config_pick=0 ;; + defaults:notify) [[ "$val" == "true" ]] && notify_mode=1 || notify_mode=0 ;; + defaults:swatch) [[ "$val" == "true" ]] && swatch_mode=1 || swatch_mode=0 ;; + defaults:output) output_formats="$val" ;; + desktop:json) [[ "$val" == "true" ]] && desktop_json=1 || desktop_json=0 ;; + desktop:alpha) [[ "$val" == "true" ]] && desktop_alpha=1 || desktop_alpha=0 ;; + desktop:name) [[ "$val" == "true" ]] && desktop_name=1 || desktop_name=0 ;; + desktop:notify) [[ "$val" == "true" ]] && desktop_notify=1 || desktop_notify=0 ;; + desktop:copy) [[ "$val" == "true" ]] && desktop_copy=1 || desktop_copy=0 ;; + desktop:output) desktop_output="$val" ;; esac fi - done < "$CONFIG_FILE" + done <"$CONFIG_FILE" } # ── Help ────────────────────────────────────────────────────────────────────── @@ -176,7 +177,7 @@ notify_result() { # Generate the internal Python helper for KDE Plasma color picking generate_picker_script() { local target="$1" - cat > "$target" <<'EOF' + cat >"$target" <<'EOF' #!/usr/bin/python3 # # Original work Copyright (C) 2024 SASUPERNOVA @@ -215,22 +216,22 @@ EOF run_color_picker() { check_kde_wayland || return 1 - + local picker_path="$SCRIPT_DIR/wl-colorpicker-plasma.py" local cleanup=0 - + if [[ ! -f "$picker_path" ]]; then picker_path=$(mktemp /tmp/wl-colorpicker-XXXXXX.py) generate_picker_script "$picker_path" cleanup=1 fi - + local picked picked=$(python3 "$picker_path") local exit_code=$? - + [[ $cleanup -eq 1 ]] && rm -f "$picker_path" - + if [[ $exit_code -eq 0 ]]; then echo "$picked" else @@ -273,7 +274,7 @@ do_install() { local config_file="$config_dir/config.toml" mkdir -p "$config_dir" if [[ ! -f "$config_file" ]]; then - cat > "$config_file" <<'EOF' + cat >"$config_file" <<'EOF' # color-tool configuration # https://github.com/rootiest/color-tool @@ -281,7 +282,6 @@ do_install() { # Set any option to true to enable it by default when using the terminal output = "hex" # default output format(s): hex, rgb, hsl, rgba, hsla, hexa, all json = false # output in JSON format -alpha = false # include alpha channel (8-digit hex) swatch = false # show color swatch in terminal name = false # fetch color name from thecolorapi.com copy = false # copy result to clipboard @@ -292,7 +292,6 @@ notify = false # show desktop notification # Defaults for --desktop mode (launched from the app menu; copy is always enabled by default) output = "hex" # format to copy json = false # copy JSON format instead of plain text -alpha = false # include alpha channel name = false # fetch color name (requires network) copy = true # copy result to clipboard notify = true # show desktop notification with the copied value @@ -306,7 +305,7 @@ EOF local desktop_file="$app_dir/color-tool.desktop" local bin_path="$HOME/.local/bin/color-tool" mkdir -p "$app_dir" - cat > "$desktop_file" <"$desktop_file" <&2; return 1; } + formats_json=$(get_all_formats "$input") || { + echo "Error: Invalid color: $input" >&2 + return 1 + } local name="" if [[ $name_mode -eq 1 ]]; then @@ -409,7 +411,7 @@ process_color() { local selected_fmts=() local valid_fmts=("hex" "hexa" "rgb" "rgba" "hsl" "hsla") - IFS=',' read -ra ADDR <<< "$output_formats" + IFS=',' read -ra ADDR <<<"$output_formats" for fmt in "${ADDR[@]}"; do if [[ "$fmt" == "all" ]]; then selected_fmts=("${valid_fmts[@]}") @@ -436,7 +438,10 @@ process_color() { if [[ $json_mode -eq 1 ]]; then output_text="$json_obj" else - output_text=$(IFS=' ' ; echo "${display_parts[*]}") + output_text=$( + IFS=' ' + echo "${display_parts[*]}" + ) [[ -n "$name" ]] && output_text="$output_text ($name)" fi @@ -446,7 +451,8 @@ process_color() { r=$(echo "$formats_json" | jq -r '._raw.r') g=$(echo "$formats_json" | jq -r '._raw.g') b=$(echo "$formats_json" | jq -r '._raw.b') - if [[ $json_mode -eq 1 ]]; then printf "\033[48;2;${r};${g};${b}m \033[0m\n" + if [[ $json_mode -eq 1 ]]; then + printf "\033[48;2;${r};${g};${b}m \033[0m\n" else printf "\033[48;2;${r};${g};${b}m \033[0m "; fi fi echo -e "$output_text" @@ -465,26 +471,41 @@ do_pick=0 # First pass: collect CLI overrides while [[ $# -gt 0 ]]; do case "$1" in - --help|-h) show_help; exit 0 ;; - --output) cli_output="$2"; shift ;; - --json) cli_json=1 ;; - --no-json) cli_json=0 ;; - --alpha) cli_alpha=1; cli_output="hexa" ;; - --no-alpha)cli_alpha=0 ;; - --name) cli_name=1 ;; - --no-name) cli_name=0 ;; - --swatch) cli_swatch=1 ;; - --no-swatch)cli_swatch=0 ;; - --copy) cli_copy=1 ;; - --no-copy) cli_copy=0 ;; - --notify) cli_notify=1 ;; - --no-notify)cli_notify=0 ;; - --pick) cli_pick=1 ;; - --no-pick) cli_pick=0 ;; - --desktop) desktop_mode=1 ;; - --install) do_install; exit 0 ;; - -*) echo "Unknown option: $1" >&2; exit 1 ;; - *) args+=("$1") ;; + --help | -h) + show_help + exit 0 + ;; + --output) + cli_output="$2" + shift + ;; + --json) cli_json=1 ;; + --no-json) cli_json=0 ;; + --alpha) + cli_alpha=1 + cli_output="hexa" + ;; + --no-alpha) cli_alpha=0 ;; + --name) cli_name=1 ;; + --no-name) cli_name=0 ;; + --swatch) cli_swatch=1 ;; + --no-swatch) cli_swatch=0 ;; + --copy) cli_copy=1 ;; + --no-copy) cli_copy=0 ;; + --notify) cli_notify=1 ;; + --no-notify) cli_notify=0 ;; + --pick) cli_pick=1 ;; + --no-pick) cli_pick=0 ;; + --desktop) desktop_mode=1 ;; + --install) + do_install + exit 0 + ;; + -*) + echo "Unknown option: $1" >&2 + exit 1 + ;; + *) args+=("$1") ;; esac shift done @@ -525,7 +546,8 @@ elif [[ ${#args[@]} -eq 0 ]]; then picked="$(run_color_picker)" || exit 1 [[ -n "$picked" ]] && args+=("$picked") elif [[ -t 0 ]]; then - show_help; exit 0 + show_help + exit 0 fi fi