summaryrefslogtreecommitdiff
path: root/ar/.local/bin/fzffiles
blob: b98eee946f22e91f497974b07d6f2d54e77fadb1 (plain)
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
118
119
120
#!/bin/sh

# The set -e option instructs sh to immediately exit if any command has a non-zero exit status
set -e

# Set new line and tab for word splitting
IFS='
'

# Get the list of selected files with key bindings for specific paths
files=$(fzf-tmux \
  --header "^a pwd ^b public ^d .dotfiles ^f configs ^g git ^h home ^k desktop ^r scripts ^s suckless ^u staged files ^v private ^/ help" \
  --preview "selection={};
    clean=\"\$selection\";
    for icon in 📄 ✏️ ✅ ❌ 🔀 ❓; do
      case \"\$clean\" in
        \"\$icon \"*)
          clean=\${clean#\"\$icon \"}
          break
          ;;
      esac
    done;
    [ -z \"\$clean\" ] && { echo 'No selection'; exit 0; }
    target=\$(readlink -f \"\$clean\" 2>/dev/null || printf '%s' \"\$clean\");
    if [ -z \"\$target\" ]; then
      echo 'Could not resolve path';
      exit 0;
    fi
    if [ -f \"\$target\" ]; then
      dir=\$(dirname \"\$target\");
      if git_root=\$(git -C \"\$dir\" rev-parse --show-toplevel 2>/dev/null); then
        rel=\${target#\"\$git_root\"/};
        diff_output=\$(git -C \"\$git_root\" diff --color -- \"\$rel\");
        if [ -n \"\$diff_output\" ]; then
          printf '%s\n' \"\$diff_output\"
          exit 0
        fi
        diff_output=\$(git -C \"\$git_root\" diff --color --cached -- \"\$rel\");
        if [ -n \"\$diff_output\" ]; then
          printf '%s\n' \"\$diff_output\"
          exit 0
        fi
      fi
    fi
    if [ -d \"\$target\" ]; then
      exa --color=always --long --all --header --icons --git \"\$target\"
    elif [ -f \"\$target\" ]; then
      bat --color=always --style=header,grid --line-range=:500 \"\$target\"
    else
      file -h \"\$target\"
    fi" \
  --reverse \
  --query="$1" \
  --multi \
  --exit-0 \
  --prompt " 💡 " \
  --bind "ctrl-a:change-prompt( ⚡ )+reload(fd -H -L -t f -E .Trash -E .git -E .cache . $PWD)" \
  --bind "ctrl-b:change-prompt( 🌎 )+reload(fd -H -L -t f -E .Trash -E .git -E .cache . ${XDG_PUBLICSHARE_DIR:-${HOME}/Public})" \
  --bind "ctrl-d:change-prompt( ⚙️ )+reload(fd -H -L -t f -E .Trash -E .git -E .cache . ${XDG_DOTFILES_DIR:-${HOME}/.dotfiles})" \
  --bind "ctrl-f:change-prompt( 🗂️ )+reload(fd -H -L -t f -E .Trash -E .git -E .cache . ${XDG_CONFIG_HOME:-${HOME}/.config})" \
  --bind "ctrl-g:change-prompt(   )+reload(fd -H -L -t f -E .Trash -E .git -E .cache . $HOME/Private/repos $HOME/Public/repos)" \
  --bind "ctrl-h:change-prompt( 🏠 )+reload(fd -H -L -t f -E .Trash -E .git -E .cache . $HOME)" \
  --bind "ctrl-k:change-prompt( 🖥️ )+reload(fd -H -L -t f -E .Trash -E .git -E .cache . ${XDG_DESKTOP_DIR:-${HOME}/Desktop})" \
  --bind "ctrl-r:change-prompt( 👟 )+reload(fd -H -L -t f -E .Trash -E .git -E .cache -E zsh . ${XDG_BIN_HOME:-${HOME}/.local/bin})" \
  --bind "ctrl-s:change-prompt( 🛠  )+reload(find ${XDG_SOURCES_HOME:-${HOME}/.local/src}/suckless -maxdepth 2 -type f -not -path '*/.git/*')" \
  --bind "ctrl-u:change-prompt( 📝 )+reload(if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then top=\$(git rev-parse --show-toplevel 2>/dev/null); git -C \"\$top\" status --porcelain | awk -v root=\"\$top\" '{
    staged=substr(\$0,1,1);
    unstaged=substr(\$0,2,1);
    file=substr(\$0,4);
    gsub(/^ +/, \"\", file);
    if (file == \"\") next;
    if (staged == \"?\" && unstaged == \"?\") icon=\"📄\";
    else if (staged == \"!\" && unstaged == \"!\") icon=\"❌\";
    else if (staged != \" \" && staged != \"?\" && unstaged != \" \" && unstaged != \"?\") icon=\"🔀\";
    else if (staged != \" \" && staged != \"?\") icon=\"✅\";
    else if (unstaged != \" \") icon=\"✏️\";
    else icon=\"❓\";
    print icon \" \" root \"/\" file
      }'; else echo 'This is not a git repository.'; fi)" \
  --bind "ctrl-v:change-prompt( 🔒 )+reload(fd -H -L -t f -E .Trash -E .git -E .cache . $HOME/Private)" \
  --bind 'ctrl-/:change-prompt( ❓ )+reload(echo "^a all
^b public
^c configs
^d .dotfiles
^g git
^k desktop
^r scripts
^s suckless
^u staged files
^v private
^/ help")')

# Check if any files were selected, and exit if not
[ -z "$files" ] && exit 0

cleaned_files=$(printf '%s\n' "$files" | while IFS= read -r line; do
  stripped="$line"
  for icon in 📄 ✏️ ✅ ❌ 🔀 ❓; do
    case "$stripped" in
    "$icon "*)
      stripped=${stripped#"$icon "}
      break
      ;;
    esac
  done
  printf '%s\n' "$stripped"
done)

files="$cleaned_files"

if [ -d "$files" ]; then
  absolute_files=$(realpath $files)
  if echo "$absolute_files" | while read -r file; do file --mime-type "$file" | grep -q 'video/'; done; then
    mpv --quiet --loop $absolute_files
  else
    openfiles "$absolute_files"
  fi
else
  openfiles "$files"
fi