prompt-themes/bin/prompt-theme.sh
2025-05-05 23:16:08 +03:00

77 lines
2.1 KiB
Bash
Executable file

#!/bin/bash
#config_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
config_dir="$HOME/.config/prompt-themes"
config_file="$config_dir/config.yaml"
shell_colors="$config_dir/dircolors"
# Accept theme as CLI arg, fallback to config
theme="$1"
# if no parameter given, look for default in config.yaml
# use 'yq' to parse the config.yaml file
# Ensure yq is installed
if ! command -v yq >/dev/null 2>&1; then
echo "⚠️ Warning: 'yq' is not installed. Cannot parse $config_file."
return 0
fi
# If no theme given, attempt to get it from config
if [[ -z "$theme" && -f "$config_file" ]]; then
theme=$(yq -r '.default' "$config_file")
fi
# Fail silently if no theme could be determined
if [[ -z "$theme" ]]; then
echo "⚠️ Warning: No theme specified and no default set in config file."
return 0
fi
# Determine which dircolors command to use
# give a warning and fail silently if not found
if [[ "$(uname)" == "Darwin" ]]; then
if command -v gdircolors >/dev/null 2>&1; then
dircolors_cmd="gdircolors"
else
echo "❌ Error: gdircolors not found. Please install via 'brew install coreutils'."
return 0
fi
else
if command -v dircolors >/dev/null 2>&1; then
dircolors_cmd="dircolors"
else
echo "❌ Error: dircolors not found. Please install coreutils."
return 0
fi
fi
# debugging
# echo "shell_colors: $shell_colors"
# echo "theme: $theme"
# echo "dir_colors_cmd: $dircolors_cmd"
# Apply dircolors theme or fallback
if [[ -r "$shell_colors/$theme" ]]; then
eval "$($dircolors_cmd -b "$shell_colors/$theme")"
elif [[ -r "$HOME/.dircolors" ]]; then
eval "$($dircolors_cmd -b "$HOME/.dircolors")"
else
echo "⚠️ Warning: No readable dircolors file found for '$theme' or ~/.dircolors. Using default ls colors."
fi
# get prompt from config.yaml if available
if [[ -f "$config_file" ]]; then
prompt=$(yq -r ".prompts.$theme" "$config_file")
else
prompt=""
fi
# apply prompt or fallback
if [[ -n "$prompt" && "$prompt" != "null" ]]; then
export PS1="$prompt"
else
echo "⚠️ Warning: No prompt found for theme '$theme'. Using fallback."
export PS1='\[\033[01;34m\]\u@\h \[\033[01;32m\]\w\[\033[00m\] \$ '
fi