From ac508a1ce42452dc5f52549e39e8f1ba0edc70ac Mon Sep 17 00:00:00 2001 From: buttering <54806742+buttering@users.noreply.github.com> Date: Sun, 1 Dec 2024 22:21:12 +0800 Subject: [PATCH] Enhance install script to handle commented and uncommented lines (#3632) (#4112) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Enhance install script to handle commented and uncommented lines (#3632) Resolves #3632 Enhance install script to handle commented and uncommented lines in shell file with user prompts for modification. - Track commented and uncommented lines in the file. - Prompt user to append or skip if the line is commented. - Ensure new lines are added only when necessary, based on user input. - To the `fish_user_key_bindings.fish`, the original logic would append the line to the end if no corresponding statement was found. I’ve adopted the same behavior for commented lines. * Refactor append_line function to improve line existence check. - Replaced `lno` variable with `lines` to store matching lines and simplified the logic. - Improved line existence check, now prints all matching lines directly and handles commented lines separately. - Removed unnecessary variables like `all_commented`, `commented_lines`, and `non_commented_lines`. * Fix indentation --------- Co-authored-by: Junegunn Choi --- install | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/install b/install index 5b67e30..556d9df 100755 --- a/install +++ b/install @@ -295,35 +295,44 @@ EOF fi append_line() { - set -e - - local update line file pat lno + local update line file pat lines update="$1" line="$2" file="$3" pat="${4:-}" - lno="" + lines="" echo "Update $file:" echo " - $line" if [ -f "$file" ]; then if [ $# -lt 4 ]; then - lno=$(\grep -nF "$line" "$file" | sed 's/:.*//' | tr '\n' ' ') + lines=$(\grep -nF "$line" "$file") else - lno=$(\grep -nF "$pat" "$file" | sed 's/:.*//' | tr '\n' ' ') + lines=$(\grep -nF "$pat" "$file") fi fi - if [ -n "$lno" ]; then - echo " - Already exists: line #$lno" + + if [ -n "$lines" ]; then + echo " - Already exists:" + sed 's/^/ Line /' <<< "$lines" + + update=0 + if ! grep -qv "^[0-9]*:[[:space:]]*#" <<< "$lines" ; then + echo " - But they all seem to be commented" + ask " - Continue modifying $file?" + update=$? + fi + fi + + set -e + if [ "$update" -eq 1 ]; then + [ -f "$file" ] && echo >> "$file" + echo "$line" >> "$file" + echo " + Added" else - if [ $update -eq 1 ]; then - [ -f "$file" ] && echo >> "$file" - echo "$line" >> "$file" - echo " + Added" - else - echo " ~ Skipped" - fi + echo " ~ Skipped" fi + echo set +e }