made configuration saving generic

This commit is contained in:
fatalerrors
2026-05-28 12:04:30 +02:00
parent c13945ced5
commit 0a85d265cb
3 changed files with 155 additions and 102 deletions

View File

@@ -169,106 +169,6 @@ load_theme()
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Persist PROMPT_THEME in profile configuration ([prompt] section).
# Preference order: ~/.profile.conf when present, else PROFILE_CONF.
_set_theme_save_prompt_theme()
{
local theme_name="$1"
[[ -z "$theme_name" ]] && {
disp E "Cannot save an empty theme name."
return 1
}
local conf_file
if [[ -f "$HOME/.profile.conf" ]]; then
conf_file="$HOME/.profile.conf"
else
conf_file="${PROFILE_CONF:-${MYPATH}/profile.conf}"
fi
local conf_dir
conf_dir="${conf_file%/*}"
[[ -d "$conf_dir" ]] || mkdir -p "$conf_dir" || {
disp E "Unable to create configuration directory: $conf_dir"
return 1
}
local tmp_file="${conf_file}.tmp.$$"
if [[ ! -e "$conf_file" ]]; then
{
printf "[prompt]\n"
printf "PROMPT_THEME=%s\n" "$theme_name"
} > "$conf_file" || {
disp E "Unable to write configuration file: $conf_file"
return 1
}
return 0
fi
awk -v theme="$theme_name" '
BEGIN {
in_prompt = 0
saw_prompt = 0
wrote_key = 0
}
{
if ($0 ~ /^\[[^]]+\][[:space:]]*$/) {
if (in_prompt && !wrote_key) {
print "PROMPT_THEME=" theme
wrote_key = 1
}
if ($0 == "[prompt]") {
in_prompt = 1
saw_prompt = 1
} else {
in_prompt = 0
}
print
next
}
if (in_prompt && $0 ~ /^[[:space:]]*PROMPT_THEME[[:space:]]*=/) {
if (!wrote_key) {
print "PROMPT_THEME=" theme
wrote_key = 1
}
next
}
print
}
END {
if (in_prompt && !wrote_key) {
print "PROMPT_THEME=" theme
wrote_key = 1
}
if (!saw_prompt) {
print ""
print "[prompt]"
print "PROMPT_THEME=" theme
}
}
' "$conf_file" > "$tmp_file" || {
rm -f "$tmp_file"
disp E "Unable to update configuration file: $conf_file"
return 1
}
mv "$tmp_file" "$conf_file" || {
rm -f "$tmp_file"
disp E "Unable to replace configuration file: $conf_file"
return 1
}
}
# Not exported, it remains private
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Dynamically switch the prompt theme for the current shell session.
@@ -352,7 +252,7 @@ set_theme()
if (( list_only )) || [[ -z "$theme_name" && $preview -eq 0 ]]; then
if (( save )); then
if [[ -n "${PROMPT_THEME:-}" ]]; then
_set_theme_save_prompt_theme "$PROMPT_THEME" || return 1
conf_save "prompt" "PROMPT_THEME" "$PROMPT_THEME" || return 1
disp I "Saved current prompt theme '$PROMPT_THEME' to configuration."
return 0
fi
@@ -441,7 +341,7 @@ set_theme()
export PROMPT_THEME="$theme_name"
if (( save )); then
_set_theme_save_prompt_theme "$theme_name" || return 1
conf_save "prompt" "PROMPT_THEME" "$theme_name" || return 1
disp I "Prompt theme set to $theme_name and saved to configuration."
return 0
fi