From 456071f2daaa42be92255b8b2da30e10b410fdf7 Mon Sep 17 00:00:00 2001 From: rootiest Date: Fri, 1 May 2026 23:13:06 -0400 Subject: [PATCH] feat: implement config management flags - Add `--get-config` to print the current configuration. - Add `--set-config` to update specific configuration keys in the TOML file. - Add `--reset-config` to restore the default configuration. - Refactor `do_install` to use the new `write_default_config` function. - Update `--help` text to document the new configuration management flags. --- color-tool | 224 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 200 insertions(+), 24 deletions(-) diff --git a/color-tool b/color-tool index b2c77a5..9df24bd 100755 --- a/color-tool +++ b/color-tool @@ -23,6 +23,7 @@ set -euo pipefail # Resolve the real directory of this script so we can find bundled helpers SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" CONFIG_FILE="$HOME/.config/color-tool/config.toml" +REPO_URL="https://git.rootiest.dev/rootiest/color-tool" # ── Initial Defaults ────────────────────────────────────────────────────────── @@ -57,6 +58,156 @@ cli_output="" # ── Config Loader ───────────────────────────────────────────────────────────── +get_config() { + if [[ ! -f "$CONFIG_FILE" ]]; then + echo "Error: Config file not found at $CONFIG_FILE" >&2 + exit 1 + fi + + local first_section=1 + while IFS= read -r line || [[ -n "$line" ]]; do + local stripped="${line%%#*}" + stripped="${stripped%"${stripped##*[![:space:]]}"}" + if [[ -z "$stripped" ]]; then + continue + fi + + if [[ "$stripped" =~ ^\[([A-Za-z_-]+)\]$ ]]; then + if [[ $first_section -eq 0 ]]; then + echo "" + fi + first_section=0 + echo "$stripped" + elif [[ "$stripped" =~ ^[[:space:]]*([A-Za-z_]+)[[:space:]]*=[[:space:]]*(.*)$ ]]; then + local key="${BASH_REMATCH[1]}" + local val="${BASH_REMATCH[2]}" + printf "%-6s = %s\n" "$key" "$val" + fi + done < "$CONFIG_FILE" +} + +set_config() { + local target_section="defaults" + if [[ $# -gt 0 ]]; then + if [[ "$1" == "default" || "$1" == "defaults" ]]; then + target_section="defaults" + shift + elif [[ "$1" == "desktop" ]]; then + target_section="desktop" + shift + fi + fi + + local new_output="" + local new_json="" + local new_swatch="" + local new_name="" + local new_copy="" + local new_pick="" + local new_notify="" + local new_alpha="" + + while [[ $# -gt 0 ]]; do + case "$1" in + --output=*) new_output="${1#*=}" ;; + output=*) new_output="${1#*=}" ;; + --json) new_json="true" ;; + --no-json) new_json="false" ;; + --alpha) new_alpha="true" ;; + --no-alpha) new_alpha="false" ;; + --name) new_name="true" ;; + --no-name) new_name="false" ;; + --swatch) new_swatch="true" ;; + --no-swatch) new_swatch="false" ;; + --copy) new_copy="true" ;; + --no-copy) new_copy="false" ;; + --notify) new_notify="true" ;; + --no-notify) new_notify="false" ;; + --pick) new_pick="true" ;; + --no-pick) new_pick="false" ;; + -*) + echo "Error: Unknown option for --set-config: $1" >&2 + exit 1 + ;; + *) + echo "Error: Unknown argument for --set-config: $1" >&2 + exit 1 + ;; + esac + shift + done + + if [[ -n "$new_output" ]]; then + new_output="${new_output#\"}" + new_output="${new_output%\"}" + fi + + if [[ ! -f "$CONFIG_FILE" ]]; then + echo "Error: Config file not found at $CONFIG_FILE. Run --install first." >&2 + exit 1 + fi + + local temp_file + temp_file="$(mktemp)" + local current_section="" + + while IFS= read -r line || [[ -n "$line" ]]; do + local original_line="$line" + local parsed_line="${line%%#*}" + + if [[ "$parsed_line" =~ ^\[([A-Za-z_-]+)\]$ ]]; then + current_section="${BASH_REMATCH[1]}" + echo "$original_line" >> "$temp_file" + continue + fi + + if [[ "$current_section" == "$target_section" && "$parsed_line" =~ ^[[:space:]]*([A-Za-z_]+)[[:space:]]*= ]]; then + local key="${BASH_REMATCH[1]}" + local new_val="" + case "$key" in + output) + if [[ -n "$new_output" ]]; then new_val="\"$new_output\""; fi + ;; + json) + if [[ -n "$new_json" ]]; then new_val="$new_json"; fi + ;; + alpha) + if [[ -n "$new_alpha" ]]; then new_val="$new_alpha"; fi + ;; + swatch) + if [[ -n "$new_swatch" ]]; then new_val="$new_swatch"; fi + ;; + name) + if [[ -n "$new_name" ]]; then new_val="$new_name"; fi + ;; + copy) + if [[ -n "$new_copy" ]]; then new_val="$new_copy"; fi + ;; + pick) + if [[ -n "$new_pick" ]]; then new_val="$new_pick"; fi + ;; + notify) + if [[ -n "$new_notify" ]]; then new_val="$new_notify"; fi + ;; + esac + + if [[ -n "$new_val" ]]; then + if [[ "$original_line" =~ ^([[:space:]]*[A-Za-z_]+[[:space:]]*=[[:space:]]*)([^[:space:]#]+)(.*)$ ]]; then + echo "${BASH_REMATCH[1]}$new_val${BASH_REMATCH[3]}" >> "$temp_file" + continue + elif [[ "$original_line" =~ ^([[:space:]]*[A-Za-z_]+[[:space:]]*=[[:space:]]*)(.*)$ ]]; then + echo "${BASH_REMATCH[1]}$new_val" >> "$temp_file" + continue + fi + fi + fi + + echo "$original_line" >> "$temp_file" + done < "$CONFIG_FILE" + + mv "$temp_file" "$CONFIG_FILE" +} + # Parse ~/.config/color-tool/config.toml and apply [defaults] and [desktop] values. load_config() { [[ ! -f "$CONFIG_FILE" ]] && return 0 @@ -98,6 +249,37 @@ load_config() { done <"$CONFIG_FILE" } +write_default_config() { + mkdir -p "$(dirname "$CONFIG_FILE")" + cat >"$CONFIG_FILE" <"$config_file" <<'EOF' -# color-tool configuration -# https://github.com/rootiest/color-tool - -[defaults] -# 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 from thecolorapi.com -copy = false # copy result to clipboard -pick = false # auto-launch color picker when invoked with no arguments -notify = false # show desktop notification - -[desktop] -# 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 -EOF + write_default_config printf " config %s (sample created)\n" "$config_file" else printf " config %s\n" "$config_file" @@ -521,6 +684,19 @@ while [[ $# -gt 0 ]]; do do_install exit 0 ;; + --get-config) + get_config + exit 0 + ;; + --set-config) + shift + set_config "$@" + exit 0 + ;; + --reset-config) + reset_config + exit 0 + ;; -*) echo "Unknown option: $1" >&2 exit 1 -- 2.52.0