Files
profile/profile.d/conf.sh
2026-06-02 10:13:22 +02:00

358 lines
12 KiB
Bash
Executable File

#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
# Protected by the BSD3 license. Please read bellow for details.
#
# * Redistribution and use in source and binary forms,
# * with or without modification, are permitted provided
# * that the following conditions are met:
# *
# * Redistributions of source code must retain the above
# * copyright notice, this list of conditions and the
# * following disclaimer.
# *
# * Redistributions in binary form must reproduce the above
# * copyright notice, this list of conditions and the following
# * disclaimer in the documentation and/or other materials
# * provided with the distribution.
# *
# * Neither the name of the copyright holder nor the names
# * of any other contributors may be used to endorse or
# * promote products derived from this software without
# * specific prior written permission.
# *
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# * OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Save or update a key=value pair in a section of the profile configuration file.
# The user configuration ($HOME/.profile.conf) is updated when it exists,
# otherwise the installation configuration ($PROFILE_CONF or $MYPATH/profile.conf)
# is used. The section header is created automatically when absent.
#
# Usage: conf_save <section> <key> <value>
# section : INI section name without brackets, e.g. "prompt" for [prompt]
# key : variable name to set (alphanumeric and underscore only)
# value : value to assign (may be empty)
conf_save()
{
if [[ $# -ne 3 ]]; then
disp E "Usage: conf_save <section> <key> <value>"
return 1
fi
local section="$1" key="$2" value="$3"
if ! [[ "$section" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
disp E "conf_save: invalid section name '${section}' (alphanumeric and underscore only)."
return 1
fi
if ! [[ "$key" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
disp E "conf_save: invalid key name '${key}' (alphanumeric and underscore only)."
return 1
fi
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_file%/*}"
[[ -d "$conf_dir" ]] || mkdir -p "$conf_dir" || {
disp E "conf_save: unable to create configuration directory: ${conf_dir}"
return 1
}
if [[ ! -e "$conf_file" ]]; then
{
printf "[%s]\n" "$section"
printf "%s=%s\n" "$key" "$value"
} > "$conf_file" || {
disp E "conf_save: unable to write configuration file: ${conf_file}"
return 1
}
return 0
fi
local tmp_file="${conf_file}.tmp.$$"
awk -v sec="$section" -v key="$key" -v val="$value" '
BEGIN {
in_sec = 0
saw_sec = 0
wrote = 0
}
{
if ($0 ~ /^\[[^]]+\][[:space:]]*$/) {
if (in_sec && !wrote) {
print key "=" val
wrote = 1
}
if ($0 ~ ("^\\[" sec "\\][[:space:]]*$")) {
in_sec = 1
saw_sec = 1
} else {
in_sec = 0
}
print
next
}
if (in_sec && $0 ~ ("^[[:space:]]*" key "[[:space:]]*=")) {
if (!wrote) {
print key "=" val
wrote = 1
}
next
}
print
}
END {
if (in_sec && !wrote) {
print key "=" val
}
if (!saw_sec) {
print ""
print "[" sec "]"
print key "=" val
}
}
' "$conf_file" > "$tmp_file" || {
rm -f "$tmp_file"
disp E "conf_save: unable to update configuration file: ${conf_file}"
return 1
}
mv "$tmp_file" "$conf_file" || {
rm -f "$tmp_file"
disp E "conf_save: unable to replace configuration file: ${conf_file}"
return 1
}
}
export -f conf_save
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Display the profile configuration file, with optional section and key filters.
# The same file resolution as conf_save is used: $HOME/.profile.conf when
# present, otherwise $PROFILE_CONF or $MYPATH/profile.conf.
#
# Usage: conf_dump [options] [pattern]
# -s, --section NAME : Only display the given section
# pattern : Only display keys whose name contains this substring
conf_dump()
{
local section="" key_pattern=""
local PARSED
PARSED=$(getopt -o hs: --long help,section: -n 'conf_dump' -- "$@")
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
disp E "Invalid options, use \"conf_dump --help\" to display usage."
return 1
fi
eval set -- "$PARSED"
while true; do
case "$1" in
-h|--help)
printf "conf_dump: Display the profile configuration file, with optional filters.\n\n"
printf "Usage: conf_dump [options] [pattern]\n\n"
printf "Options:\n"
printf "\t-h, --help\t\tDisplay this help screen\n"
printf "\t-s, --section NAME\tOnly display the given section\n\n"
printf "Arguments:\n"
printf "\tpattern\tOnly display keys whose name contains this substring\n"
return 0
;;
-s|--section)
section="$2"
shift 2
;;
--)
shift
break
;;
esac
done
[[ $# -gt 0 ]] && key_pattern="$1"
local conf_file
if [[ -f "$HOME/.profile.conf" ]]; then
conf_file="$HOME/.profile.conf"
else
conf_file="${PROFILE_CONF:-${MYPATH}/profile.conf}"
fi
if [[ ! -f "$conf_file" ]]; then
disp E "conf_dump: configuration file not found: ${conf_file}"
return 1
fi
# Colours are passed via ENVIRON to avoid awk -v escape interpretation.
_CONF_DUMP_SEC="${Blue:-}" \
_CONF_DUMP_KEY="${BIWhite:-}" \
_CONF_DUMP_RST="${RESETCOL:-}" \
awk -v sec_filter="$section" -v key_filter="$key_pattern" '
BEGIN {
c_sec = ENVIRON["_CONF_DUMP_SEC"]
c_key = ENVIRON["_CONF_DUMP_KEY"]
c_rst = ENVIRON["_CONF_DUMP_RST"]
# Shell colour vars contain literal \e[…m strings; convert to
# the actual ESC byte so awk print emits real ANSI sequences.
gsub(/\\e/, "\033", c_sec)
gsub(/\\e/, "\033", c_key)
gsub(/\\e/, "\033", c_rst)
in_target = 0
current_sec = ""
hdr_printed = 0
found = 0
}
{
sub(/\r$/, "")
if ($0 ~ /^\[[^]]+\][[:space:]]*$/) {
current_sec = $0
sub(/^\[/, "", current_sec)
sub(/\][[:space:]]*$/, "", current_sec)
in_target = (sec_filter == "" || current_sec == sec_filter)
hdr_printed = 0
next
}
if (!in_target) next
if ($0 !~ /^[[:space:]]*[A-Za-z_][A-Za-z0-9_]*[[:space:]]*=/) next
key = $0; sub(/[[:space:]]*=.*$/, "", key); sub(/^[[:space:]]*/, "", key)
val = $0; sub(/^[^=]*=/, "", val)
if (key_filter != "" && index(key, key_filter) == 0) next
if (!hdr_printed) {
if (found) print ""
print c_sec "[" current_sec "]" c_rst
hdr_printed = 1
found = 1
}
print " " c_key key c_rst "=" val
}
' "$conf_file"
}
export -f conf_dump
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Set TERM to the best terminal capability available on this system.
# Called automatically at sourcing time.
#
# If TERM is already set to a specific value (not empty, not the sentinel
# "smart"), it is honoured as-is — standard Unix behaviour.
# If TERM is empty or set to "smart", terminal emulator hints are checked first
# (COLORTERM, TERM_PROGRAM, VTE_VERSION, WT_SESSION, TMUX, STY), then terminfo
# is probed in preference order: xterm-256color → xterm-color → xterm → vt100.
#
# Usage: term_set
term_set()
{
local _current="${TERM:-}"
# Specific value already set — nothing to do.
if [[ -n "$_current" && "$_current" != "smart" ]]; then
export TERM="$_current"
return 0
fi
# Return true when terminfo has an entry for the given terminal type.
local _term_has
_term_has() { tput -T "$1" longname >/dev/null 2>&1; }
local _candidate=""
# 1. Explicit truecolor hint set by modern terminal emulators.
if [[ "${COLORTERM:-}" == "truecolor" || "${COLORTERM:-}" == "24bit" ]]; then
local _t
for _t in xterm-direct xterm-256color; do
_term_has "$_t" && { _candidate="$_t"; break; }
done
fi
# 2. Terminal programme name hints.
if [[ -z "$_candidate" ]]; then
case "${TERM_PROGRAM:-}" in
iTerm.app)
_term_has "xterm-256color" && _candidate="xterm-256color"
;;
WezTerm)
_term_has "wezterm" && _candidate="wezterm"
[[ -z "$_candidate" ]] && _term_has "xterm-256color" && _candidate="xterm-256color"
;;
Hyper|vscode)
_term_has "xterm-256color" && _candidate="xterm-256color"
;;
esac
fi
# 3. VTE-based terminals (GNOME Terminal, Tilix, Xfce Terminal, …).
if [[ -z "$_candidate" && -n "${VTE_VERSION:-}" ]]; then
_term_has "vte-256color" && _candidate="vte-256color"
[[ -z "$_candidate" ]] && _term_has "xterm-256color" && _candidate="xterm-256color"
fi
# 4. Windows Terminal.
if [[ -z "$_candidate" && -n "${WT_SESSION:-}" ]]; then
_term_has "xterm-256color" && _candidate="xterm-256color"
fi
# 5. tmux — prefer tmux-256color, fall back to screen-256color.
if [[ -z "$_candidate" && -n "${TMUX:-}" ]]; then
_term_has "tmux-256color" && _candidate="tmux-256color"
[[ -z "$_candidate" ]] && _term_has "screen-256color" && _candidate="screen-256color"
fi
# 6. GNU screen — prefer screen-256color, fall back to screen.
if [[ -z "$_candidate" && -n "${STY:-}" ]]; then
_term_has "screen-256color" && _candidate="screen-256color"
[[ -z "$_candidate" ]] && _term_has "screen" && _candidate="screen"
fi
# 7. Generic terminfo probe in preference order.
if [[ -z "$_candidate" ]]; then
local _t
for _t in xterm-256color xterm-color xterm vt100; do
_term_has "$_t" && { _candidate="$_t"; break; }
done
fi
unset -f _term_has
export TERM="${_candidate:-vt100}"
}
export -f term_set
# ------------------------------------------------------------------------------
term_set
# EOF