feat(config): maintain comment alignment in set_config
Updated set_config to dynamically adjust leading whitespace of trailing comments when toggling between true/false (or any values of different lengths). This ensures that inline comments in config.toml remain vertically aligned after programmatic updates.
This commit is contained in:
+27
-1
@@ -194,7 +194,33 @@ set_config() {
|
||||
|
||||
if [[ -n "$new_val" ]]; then
|
||||
if [[ "$original_line" =~ ^([[:space:]]*[A-Za-z_]+[[:space:]]*=[[:space:]]*)([^[:space:]#]+)(.*)$ ]]; then
|
||||
echo "${BASH_REMATCH[1]}$new_val${BASH_REMATCH[3]}" >> "$temp_file"
|
||||
local prefix="${BASH_REMATCH[1]}"
|
||||
local old_val="${BASH_REMATCH[2]}"
|
||||
local suffix="${BASH_REMATCH[3]}"
|
||||
|
||||
# Adjust whitespace to keep comments aligned if the value length changed
|
||||
if [[ "$key" != "output" ]]; then
|
||||
local old_len=${#old_val}
|
||||
local new_len=${#new_val}
|
||||
local diff=$((old_len - new_len))
|
||||
|
||||
if [[ $diff -gt 0 ]]; then
|
||||
# new value is shorter, add spaces to suffix
|
||||
local spaces=""
|
||||
for ((i=0; i<diff; i++)); do spaces+=" "; done
|
||||
suffix="$spaces$suffix"
|
||||
elif [[ $diff -lt 0 ]]; then
|
||||
# new value is longer, remove spaces from suffix
|
||||
local remove=$((-diff))
|
||||
for ((i=0; i<remove; i++)); do
|
||||
if [[ "$suffix" == " "* ]]; then
|
||||
suffix="${suffix# }"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "$prefix$new_val$suffix" >> "$temp_file"
|
||||
continue
|
||||
elif [[ "$original_line" =~ ^([[:space:]]*[A-Za-z_]+[[:space:]]*=[[:space:]]*)(.*)$ ]]; then
|
||||
echo "${BASH_REMATCH[1]}$new_val" >> "$temp_file"
|
||||
|
||||
Reference in New Issue
Block a user