diff --git a/profile.d/help.sh b/profile.d/help.sh index 8fed25e..5f9854c 100644 --- a/profile.d/help.sh +++ b/profile.d/help.sh @@ -72,6 +72,7 @@ help() printf " * setc\tSet locale to standard C (POSIX)\n" printf " * set*\tLocale shortcuts generated from SET_LOCALE in profile.conf\n" printf "settrace\tActivate or deactivate ERR trap to display backtrace on script errors\n" + printf "set_theme\tSwitch the prompt colour theme; no argument lists available themes\n" printf "showinfo\tDisplay welcome banner and system information (figlet + neofetch/fastfetch)\n" printf "ssr\t\tSSH into a server as root, forwarding extra ssh options\n" printf "taz\t\tCompress files and directories into a chosen archive format\n" diff --git a/profile.d/prompt.sh b/profile.d/prompt.sh index 99bc44d..d9312f2 100644 --- a/profile.d/prompt.sh +++ b/profile.d/prompt.sh @@ -164,6 +164,46 @@ load_theme() fi done < "$theme_file" } +# Not exported, it remains private +# ------------------------------------------------------------------------------ + + +# ------------------------------------------------------------------------------ +# Dynamically switch the prompt theme for the current shell session. +# Calls load_theme to apply the new colour values immediately, then updates +# PROMPT_THEME so subshells and the set_prompt fallback chain reflect the +# change. PROMPT_THEME_DIR is honoured when set. +# Usage: set_theme [theme_name_or_path] +# With no argument (or -l / --list), lists available .theme files. +set_theme() +{ + local theme_dir="${PROMPT_THEME_DIR:-${MYPATH}/profile.d/themes}" + + # -- list mode ----------------------------------------------------------- + if [[ $# -eq 0 || "$1" == "-l" || "$1" == "--list" ]]; then + printf "Available themes in %s:\n" "$theme_dir" + local f name + for f in "$theme_dir"/*.theme; do + [[ -f "$f" ]] || continue + name="${f##*/}" + name="${name%.theme}" + if [[ "$name" == "${PROMPT_THEME:-}" ]]; then + printf " * %s (active)\n" "$name" + else + printf " %s\n" "$name" + fi + done + return 0 + fi + + # -- apply mode ---------------------------------------------------------- + local theme_name="$1" + load_theme "$theme_name" || return 1 + + export PROMPT_THEME="$theme_name" + disp I "Prompt theme set to $theme_name." +} +export -f set_theme # ------------------------------------------------------------------------------