Merge pull request 'feat: implement config management flags' (#4) from feat/manage-config into main

Reviewed-on: #4
This commit was merged in pull request #4.
This commit is contained in:
2026-05-02 03:13:52 +00:00
+200 -24
View File
@@ -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" <<EOF
# color-tool configuration
# Source: $REPO_URL
[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
}
reset_config() {
write_default_config
echo "Configuration reset to defaults: $CONFIG_FILE"
}
# ── Help ──────────────────────────────────────────────────────────────────────
show_help() {
@@ -121,9 +303,12 @@ show_help() {
printf " ${bold}${cyan}--[no-]json${reset} Output as a JSON table of selected formats\n"
printf " ${bold}${cyan}--[no-]name${reset} Fetch nearest color name from thecolorapi.com ${dim}(requires curl, jq)${reset}\n"
printf " ${bold}${cyan}--[no-]swatch${reset} Include a color swatch in the terminal output\n"
printf " ${bold}${cyan}--[no-]copy${reset} Copy result to clipboard ${dim}(wl-copy preferred, xclip as fallback)${reset}\n"
printf " ${bold}${cyan}--[no-]copy${reset} Copy result to clipboard ${dim}(wl-clipboard preferred, xclip as fallback)${reset}\n"
printf " ${bold}${cyan}--[no-]notify${reset} Show desktop notification ${dim}(on by default in --desktop)${reset}\n"
printf " ${bold}${cyan}--desktop${reset} GUI mode: pick → copy → notify ${dim}(for app menu / .desktop launcher)${reset}\n"
printf " ${bold}${cyan}--get-config${reset} Print the current configuration\n"
printf " ${bold}${cyan}--set-config${reset} Update the configuration ${dim}(e.g. --set-config desktop --copy)${reset}\n"
printf " ${bold}${cyan}--reset-config${reset} Restore the configuration file to its default values\n"
printf " ${bold}${cyan}--install${reset} Install to ~/.local/share/color-tool/ and symlink into ~/.local/bin/\n"
printf " ${bold}${cyan}--help${reset}, ${bold}${cyan}-h${reset} Show this help message\n\n"
@@ -278,30 +463,8 @@ do_install() {
local config_dir="$HOME/.config/color-tool"
local config_file="$config_dir/config.toml"
mkdir -p "$config_dir"
if [[ ! -f "$config_file" ]]; then
cat >"$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