File: uninstall

package info (click to toggle)
fzf 0.24.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 936 kB
  • sloc: ruby: 1,973; sh: 998; makefile: 113
file content (117 lines) | stat: -rwxr-xr-x 2,530 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env bash

xdg=0
prefix='~/.fzf'
prefix_expand=~/.fzf
fish_dir=${XDG_CONFIG_HOME:-$HOME/.config}/fish

help() {
  cat << EOF
usage: $0 [OPTIONS]

    --help               Show this message
    --xdg                Remove files generated under \$XDG_CONFIG_HOME/fzf
EOF
}

for opt in "$@"; do
  case $opt in
    --help)
      help
      exit 0
      ;;
    --xdg)
      xdg=1
      prefix='"${XDG_CONFIG_HOME:-$HOME/.config}"/fzf/fzf'
      prefix_expand=${XDG_CONFIG_HOME:-$HOME/.config}/fzf/fzf
      ;;
    *)
      echo "unknown option: $opt"
      help
      exit 1
      ;;
  esac
done

ask() {
  while true; do
    read -p "$1 ([y]/n) " -r
    REPLY=${REPLY:-"y"}
    if [[ $REPLY =~ ^[Yy]$ ]]; then
      return 0
    elif [[ $REPLY =~ ^[Nn]$ ]]; then
      return 1
    fi
  done
}

remove() {
  echo "Remove $1"
  rm -f "$1"
}

remove_line() {
  src=$(readlink "$1")
  if [ $? -eq 0 ]; then
    echo "Remove from $1 ($src):"
  else
    src=$1
    echo "Remove from $1:"
  fi

  shift
  line_no=1
  match=0
  while [ -n "$1" ]; do
    line=$(sed -n "$line_no,\$p" "$src" | \grep -m1 -nF "$1")
    if [ $? -ne 0 ]; then
      shift
      line_no=1
      continue
    fi
    line_no=$(( $(sed 's/:.*//' <<< "$line") + line_no - 1 ))
    content=$(sed 's/^[0-9]*://' <<< "$line")
    match=1
    echo    "  - Line #$line_no: $content"
    [ "$content" = "$1" ] || ask "    - Remove?"
    if [ $? -eq 0 ]; then
      awk -v n=$line_no 'NR == n {next} {print}' "$src" > "$src.bak" &&
        mv "$src.bak" "$src" || break
      echo  "      - Removed"
    else
      echo  "      - Skipped"
      line_no=$(( line_no + 1 ))
    fi
  done
  [ $match -eq 0 ] && echo "  - Nothing found"
  echo
}

for shell in bash zsh; do
  shell_config=${prefix_expand}.${shell}
  remove "${shell_config}"
  remove_line ~/.${shell}rc \
    "[ -f ${prefix}.${shell} ] && source ${prefix}.${shell}" \
    "source ${prefix}.${shell}"
done

bind_file="${fish_dir}/functions/fish_user_key_bindings.fish"
if [ -f "$bind_file" ]; then
  remove_line "$bind_file" "fzf_key_bindings"
fi

if [ -d "${fish_dir}/functions" ]; then
  remove "${fish_dir}/functions/fzf.fish"
  remove "${fish_dir}/functions/fzf_key_bindings.fish"

  if [ "$(ls -A "${fish_dir}/functions")" ]; then
    echo "Can't delete non-empty directory: \"${fish_dir}/functions\""
  else
    rmdir "${fish_dir}/functions"
  fi
fi

config_dir=$(dirname "$prefix_expand")
if [[ "$xdg" = 1 ]] && [[ "$config_dir" = */fzf ]] && [[ -d "$config_dir" ]]; then
  rmdir "$config_dir"
fi