-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathff.fish
More file actions
255 lines (224 loc) · 8.11 KB
/
ff.fish
File metadata and controls
255 lines (224 loc) · 8.11 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# =============================================================================
# ff - Flexible File Finder (Fish Shell Version)
# An interactive file search and navigation tool using fzf.
# Supports dual mode: Find (by filename) and Grep (by content)
# =============================================================================
function ff
# --- Check required dependencies ---
if not command -v fzf >/dev/null 2>&1
echo "❌ Error: fzf is not installed" >&2
echo "" >&2
echo "fzf is required for ff to work. Please install it:" >&2
echo " Ubuntu/Debian: sudo apt install fzf" >&2
echo " macOS: brew install fzf" >&2
echo "" >&2
echo "For more info: https://github.com/junegunn/fzf#installation" >&2
return 1
end
set -l mode "find"
if test (count $argv) -gt 0
set mode $argv[1]
end
set -l out
set -l key
set -l result
set -l file
set -l line
set -l target_dir
set -l preview_cmd
set -l grep_base_cmd
set -l full_reload_cmd
set -l find_cmd
# --- Guide message constants ---
set -l MSG_GREP_GUIDE "Type to search content..."
# --- FZF UI Options ---
set -l base_opts '
--height 60%
--layout=reverse
--border
--info=inline
--prompt=">"
--pointer=">"
--marker="✓"
--ansi
--preview-window=right:60%
'
# --- 1. Dependency tool configuration ---
set -l BAT_CMD "cat"
set -l BAT_OPTS ""
if command -v batcat >/dev/null 2>&1
set BAT_CMD "batcat"
set BAT_OPTS "--style=numbers --color=always"
else if command -v bat >/dev/null 2>&1
set BAT_CMD "bat"
set BAT_OPTS "--style=numbers --color=always"
end
set -l FD_CMD ""
set -l USE_FD 0
if command -v fd >/dev/null 2>&1
set FD_CMD "fd"
set USE_FD 1
else if command -v fdfind >/dev/null 2>&1
set FD_CMD "fdfind"
set USE_FD 1
end
set -l USE_RG 0
if command -v rg >/dev/null 2>&1
set USE_RG 1
end
set -l USE_EZA 0
if command -v eza >/dev/null 2>&1
set USE_EZA 1
end
set -l EDITOR_CMD "$EDITOR"
if test -z "$EDITOR_CMD"
set EDITOR_CMD "vi"
end
set -l IS_VSCODE 0
if command -v code >/dev/null 2>&1
set EDITOR_CMD "code"
set IS_VSCODE 1
end
# Main loop
while true
# --- 2. Execute FZF based on current mode ---
if test "$mode" = "find"
# [FIND MODE]
if test $USE_EZA -eq 1
set preview_cmd "if test -d {}; eza --tree --color=always --level=2 --icons {}; else $BAT_CMD $BAT_OPTS {}; end"
else if command -v tree >/dev/null 2>&1
set preview_cmd "if test -d {}; tree -C -L 2 {}; else $BAT_CMD $BAT_OPTS {}; end"
else
set preview_cmd "if test -d {}; echo '📂 Directory: {}'; else $BAT_CMD $BAT_OPTS {}; end"
end
if test $USE_FD -eq 1
set find_cmd "$FD_CMD . --type f --type d --follow --color=never"
else
set find_cmd "find . \\( -type d -name '.\\*' \\) -prune -o -print"
end
set out (eval $find_cmd 2>/dev/null | \
env FZF_DEFAULT_OPTS=$base_opts fzf --expect=tab,ctrl-o --cycle -i \
--prompt="🔍 FIND > " \
--header='TAB: switch | ENTER: cd | CTRL-O: open' \
--bind "ctrl-u:preview-up,ctrl-d:preview-down" \
--preview "$preview_cmd")
else
# [GREP MODE]
if test $USE_RG -eq 1
set grep_base_cmd "rg --column --line-number --no-heading --color=never --smart-case --null -- {q} | perl -pe 's/\0(\d+):.*/|\1/' || true"
else
set grep_base_cmd "grep -Rni --color=never --null -- {q} . | perl -pe 's/\0(\d+):.*/|\1/' || true"
end
set full_reload_cmd "if test -z '{q}'; echo '$MSG_GREP_GUIDE'; else $grep_base_cmd; end"
set out (echo "$MSG_GREP_GUIDE" | \
env FZF_DEFAULT_OPTS=$base_opts fzf --expect=tab,ctrl-o --delimiter '\\|' --cycle --disabled \
--prompt="📝 GREP > " \
--header='TAB: switch | ENTER: cd | CTRL-O: open' \
--bind "start:reload:$full_reload_cmd" \
--bind "change:reload:sleep 0.1; $full_reload_cmd" \
--bind "ctrl-u:preview-up,ctrl-d:preview-down" \
--preview "if test '{}' = '$MSG_GREP_GUIDE'; echo 'Type to search...'; else
set file (echo {} | cut -d\\| -f1);
set line (echo {} | cut -d\\| -f2);
test -n \"\$file\" && $BAT_CMD $BAT_OPTS --highlight-line \$line \$file;
end" \
--preview-window='+{2}-5')
end
# --- 3. Parse key input and result ---
if test -z "$out"
return
end
set key (echo "$out" | head -n 1)
set result (echo "$out" | sed -n '2p')
if test "$key" = "tab"
if test "$mode" = "find"
set mode "grep"
else
set mode "find"
end
continue
end
if test -z "$result"
return
end
if test "$result" = "$MSG_GREP_GUIDE"
continue
end
# --- 4. Parse result based on mode ---
if test "$mode" = "grep"
set file (echo "$result" | cut -d\| -f1)
set line (echo "$result" | cut -d\| -f2)
if not test -f "$file"
echo "⚠️ File not found: $file" >&2
return
end
else
set file "$result"
set line ""
end
# --- 5. Execute action based on key pressed ---
if test "$key" = "ctrl-o"
if not test -f "$file"
echo "❌ Error: File not found: $file" >&2
return 1
end
if test $IS_VSCODE -eq 1
if test -n "$line"
if code --goto "$file:$line" 2>/dev/null
echo "📄 Opened: $file:$line"
else
echo "❌ Error: Failed to open file in VSCode" >&2
echo " Make sure VSCode is installed and 'code' command is available" >&2
return 1
end
else
if code "$file" 2>/dev/null
echo "📄 Opened: $file"
else
echo "❌ Error: Failed to open file in VSCode" >&2
return 1
end
end
else
if test -n "$line"
if eval $EDITOR_CMD "+$line" "$file"
echo "📄 Opened: $file:$line"
else
echo "❌ Error: Failed to open file with $EDITOR_CMD" >&2
return 1
end
else
if eval $EDITOR_CMD "$file"
echo "📄 Opened: $file"
else
echo "❌ Error: Failed to open file with $EDITOR_CMD" >&2
return 1
end
end
end
return
end
if test -f "$file"
set target_dir (dirname "$file")
else if test -d "$file"
set target_dir "$file"
else
echo "❌ Error: Invalid selection: $file" >&2
echo " Path does not exist or is not accessible" >&2
return 1
end
if test -n "$target_dir"; and test -d "$target_dir"
if cd "$target_dir" 2>/dev/null
echo "📂 Moved to: "(pwd)
else
echo "❌ Error: Failed to change directory to: $target_dir" >&2
echo " Check permissions or if the directory still exists" >&2
return 1
end
else
echo "❌ Error: Target directory not found: $target_dir" >&2
return 1
end
return
end
end