Debian Codegraph @

uninstall

raw
1 #!/usr/bin/env bash
2
3 xdg=0
4 prefix='~/.fzf'
5 prefix_expand=~/.fzf
6 fish_dir=${XDG_CONFIG_HOME:-$HOME/.config}/fish
7
8 help() {
9 cat << EOF
10 usage: $0 [OPTIONS]
11
12 --help Show this message
13 --xdg Remove files generated under \$XDG_CONFIG_HOME/fzf
14 EOF
15 }
16
17 for opt in "$@"; do
18 case $opt in
19 --help)
20 help
21 exit 0
22 ;;
23 --xdg)
24 xdg=1
25 prefix='"${XDG_CONFIG_HOME:-$HOME/.config}"/fzf/fzf'
26 prefix_expand=${XDG_CONFIG_HOME:-$HOME/.config}/fzf/fzf
27 ;;
28 *)
29 echo "unknown option: $opt"
30 help
31 exit 1
32 ;;
33 esac
34 done
35
36 cd "$(dirname "${BASH_SOURCE[0]}")"
37 fzf_base=$(pwd)
38
39 ask() {
40 while true; do
41 read -p "$1 ([y]/n) " -r
42 REPLY=${REPLY:-"y"}
43 if [[ $REPLY =~ ^[Yy]$ ]]; then
44 return 0
45 elif [[ $REPLY =~ ^[Nn]$ ]]; then
46 return 1
47 fi
48 done
49 }
50
51 remove() {
52 echo "Remove $1"
53 rm -f "$1"
54 }
55
56 remove_line() {
57 src=$1
58 echo "Remove from $1:"
59
60 shift
61 line_no=1
62 match=0
63 while [ -n "$1" ]; do
64 line=$(sed -n "$line_no,\$p" "$src" | \grep -m1 -nF "$1")
65 if [ $? -ne 0 ]; then
66 shift
67 line_no=1
68 continue
69 fi
70 line_no=$(($(sed 's/:.*//' <<< "$line") + line_no - 1))
71 content=$(sed 's/^[0-9]*://' <<< "$line")
72 match=1
73 echo " - Line #$line_no: $content"
74 [ "$content" = "$1" ] || ask " - Remove?"
75 if [ $? -eq 0 ]; then
76 temp=$(mktemp)
77 awk -v n=$line_no 'NR == n {next} {print}' "$src" > "$temp" &&
78 cat "$temp" > "$src" && rm -f "$temp" || break
79 echo " - Removed"
80 else
81 echo " - Skipped"
82 line_no=$((line_no + 1))
83 fi
84 done
85 [ $match -eq 0 ] && echo " - Nothing found"
86 echo
87 }
88
89 for shell in bash zsh; do
90 shell_config=${prefix_expand}.${shell}
91 remove "${shell_config}"
92 remove_line ~/.${shell}rc \
93 "[ -f ${prefix}.${shell} ] && source ${prefix}.${shell}" \
94 "source ${prefix}.${shell}"
95 done
96
97 bind_file="${fish_dir}/functions/fish_user_key_bindings.fish"
98 if [ -f "$bind_file" ]; then
99 remove_line "$bind_file" "fzf_key_bindings"
100 remove_line "$bind_file" "fzf_completion_setup"
101 remove_line "$bind_file" "fzf --fish | source"
102 fi
103
104 if [ -d "${fish_dir}/functions" ]; then
105 remove "${fish_dir}/functions/fzf.fish"
106 remove "${fish_dir}/functions/fzf_key_bindings.fish"
107 remove_line "$bind_file" "source \"${fzf_base}/shell/completion.fish\""
108 remove_line "$bind_file" "source \"${fzf_base}/shell/key-bindings.fish\""
109
110 if [ -z "$(ls -A "${fish_dir}/functions")" ]; then
111 rmdir "${fish_dir}/functions"
112 else
113 echo "Can't delete non-empty directory: \"${fish_dir}/functions\""
114 fi
115 fi
116
117 if command -v nu > /dev/null; then
118 nushell_autoload_dir=$(nu -c '$nu.user-autoload-dirs | first')
119 else
120 nushell_autoload_dir=${XDG_CONFIG_HOME:-$HOME/.config}/nushell/autoload
121 fi
122 remove "${nushell_autoload_dir}/_fzf_integration.nu"
123
124 config_dir=$(dirname "$prefix_expand")
125 if [[ $xdg == 1 ]] && [[ $config_dir == */fzf ]] && [[ -d $config_dir ]]; then
126 rmdir "$config_dir"
127 fi