summaryrefslogtreecommitdiff
path: root/install
diff options
context:
space:
mode:
authorbuttering <54806742+buttering@users.noreply.github.com>2024-12-01 22:21:12 +0800
committerGitHub <noreply@github.com>2024-12-01 23:21:12 +0900
commitac508a1ce42452dc5f52549e39e8f1ba0edc70ac (patch)
tree1b938fb315a32eb93611edc17ed8f6260986c3ce /install
parentd7fc1e09b1395656591fbefd309150b47f72ce6a (diff)
downloadfzf-ac508a1ce42452dc5f52549e39e8f1ba0edc70ac.tar.gz
Enhance install script to handle commented and uncommented lines (#3632) (#4112)
* 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 <junegunn.c@gmail.com>
Diffstat (limited to 'install')
-rwxr-xr-xinstall39
1 files changed, 24 insertions, 15 deletions
diff --git a/install b/install
index 5b67e30a..556d9dfa 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"
- else
- if [ $update -eq 1 ]; then
- [ -f "$file" ] && echo >> "$file"
- echo "$line" >> "$file"
- echo " + Added"
- else
- echo " ~ Skipped"
+
+ 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
+ echo " ~ Skipped"
+ fi
+
echo
set +e
}