diff --git a/README.md b/README.md index 71040d1..9b3251b 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,8 @@ A bar-style prompt showing current time, execution time of the last command | `busy` | fun | Monitor /dev/urandom for a hex pattern — look busy | | `check_updates` | updates | Check whether a newer profile version is available online; when called with `-q` at startup a 3-second network timeout is applied so a slow or absent network never delays the prompt | | `clean` | filefct | Erase backup files in given directories, optionally recursive | +| `conf_dump` | conf | Display the profile configuration file; `-s/--section NAME` restricts output to one section; an optional pattern argument filters keys by substring match | +| `conf_save` | conf | Save or update a key=value pair in a configuration section: `conf_save
`; creates the section header automatically if absent | | `disp` | disp | Display formatted info / warning / error / debug messages; long messages are word-wrapped and continuation lines are indented to align with the message text | | `dwl` | net | Download a URL using curl, wget, or fetch transparently; supports `-t ` / `--timeout ` to cap the transfer time | | `expandlist` | filefct | Expand glob expressions into a quoted, separated list | diff --git a/profile.d/conf.sh b/profile.d/conf.sh index 4188924..9cfac55 100755 --- a/profile.d/conf.sh +++ b/profile.d/conf.sh @@ -149,4 +149,111 @@ 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"] + 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 +# ------------------------------------------------------------------------------ + + # EOF diff --git a/profile.d/help.sh b/profile.d/help.sh index af7f605..dc80753 100644 --- a/profile.d/help.sh +++ b/profile.d/help.sh @@ -57,11 +57,12 @@ help() printf "busy\t\tMonitor /dev/urandom for a hex pattern — look busy\n" printf "check_updates\tCheck for new versions of profile\n" printf "clean\t\tErase backup files in given directories, optionally recursive\n" + printf "conf_dump\tDisplay the profile configuration file\n" printf "conf_save\tSave or update a key=value pair in a profile configuration section\n" printf "disp\t\tDisplay formatted info/warning/error/debug messages\n" printf "dwl\t\tDownload a URL using curl, wget, or fetch transparently\n" printf "expandlist\tExpand glob expressions into a quoted, separated list\n" - printf "fake_compile\tSimulate a long compilation process — look busy again\n" + printf "fake_compile\tSimulate a long compilation process — look busy\n" printf "file_stats\tDisplay file size statistics for a path\n" printf "findbig\t\tFind the biggest files in the given or current directory\n" printf "finddead\tFind dead symbolic links in the given or current directory\n"