-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathff.sh
More file actions
executable file
·224 lines (197 loc) · 6.84 KB
/
ff.sh
File metadata and controls
executable file
·224 lines (197 loc) · 6.84 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
# =============================================================================
# ff - Flexible File Finder
# An interactive file search and navigation tool using fzf.
# Supports dual mode: Find (by filename) and Grep (by content)
# =============================================================================
ff() {
# --- Check required dependencies ---
if ! command -v fzf >/dev/null 2>&1; then
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
fi
local mode="${1:-find}"
local out key result file line target_dir
local preview_cmd grep_base_cmd full_reload_cmd
local find_cmd_arr
# --- Guide message constants ---
local MSG_GREP_GUIDE="Type to search content..."
# --- FZF UI Options defined LOCALLY ---
local base_opts='
--height 60%
--layout=reverse
--border
--info=inline
--prompt=">"
--pointer=">"
--marker="✓"
--ansi
--preview-window=right:60%
'
# --- 1. Dependency tool configuration ---
local BAT_CMD="cat"
local BAT_OPTS=""
if command -v batcat >/dev/null; then
BAT_CMD="batcat"
BAT_OPTS="--style=numbers --color=always"
elif command -v bat >/dev/null; then
BAT_CMD="bat"
BAT_OPTS="--style=numbers --color=always"
fi
local FD_CMD=""
local USE_FD=0
if command -v fd >/dev/null; then
FD_CMD="fd"
USE_FD=1
elif command -v fdfind >/dev/null; then
FD_CMD="fdfind"
USE_FD=1
fi
local USE_RG=0; command -v rg >/dev/null && USE_RG=1
local USE_EZA=0; command -v eza >/dev/null && USE_EZA=1
local EDITOR_CMD="${EDITOR:-vi}"
local IS_VSCODE=0
if command -v code >/dev/null; then
EDITOR_CMD="code"
IS_VSCODE=1
fi
# Main loop
while :; do
# --- 2. Execute FZF based on current mode ---
if [[ "$mode" == "find" ]]; then
# [FIND MODE]
if [[ "$USE_EZA" -eq 1 ]]; then
preview_cmd="if [[ -d {} ]]; then eza --tree --color=always --level=2 --icons {}; else $BAT_CMD $BAT_OPTS {}; fi"
elif command -v tree >/dev/null; then
preview_cmd="if [[ -d {} ]]; then tree -C -L 2 {}; else $BAT_CMD $BAT_OPTS {}; fi"
else
preview_cmd="if [[ -d {} ]]; then echo '📂 Directory: {}'; else $BAT_CMD $BAT_OPTS {}; fi"
fi
if [[ "$USE_FD" -eq 1 ]]; then
find_cmd_arr=($FD_CMD . --type f --type d --follow --color=never)
else
find_cmd_arr=(find . \( -type d -name '.\*' \) -prune -o -print)
fi
# Apply options strictly to this command execution
out=$("${find_cmd_arr[@]}" 2>/dev/null | \
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 [[ "$USE_RG" -eq 1 ]]; then
grep_base_cmd="rg --column --line-number --no-heading --color=never --smart-case --null -- {q} | perl -pe 's/\0(\d+):.*/|\1/' || true"
else
grep_base_cmd="grep -Rni --color=never --null -- {q} . | perl -pe 's/\0(\d+):.*/|\1/' || true"
fi
full_reload_cmd="if [[ -z {q} ]]; then echo '$MSG_GREP_GUIDE'; else $grep_base_cmd; fi"
# Apply options strictly to this command execution
out=$(echo "$MSG_GREP_GUIDE" | \
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 [[ {} == '$MSG_GREP_GUIDE' ]]; then echo 'Type to search...'; else
file=\$(echo {} | cut -d\| -f1);
line=\$(echo {} | cut -d\| -f2);
[ -n \"\$file\" ] && $BAT_CMD $BAT_OPTS --highlight-line \$line \$file;
fi" \
--preview-window=+{2}-5)
fi
# --- 3. Parse key input and result ---
[[ -z "$out" ]] && return
key=$(head -n 1 <<< "$out")
result=$(sed -n '2p' <<< "$out")
if [[ "$key" == "tab" ]]; then
[[ "$mode" == "find" ]] && mode="grep" || mode="find"
continue
fi
[[ -z "$result" ]] && return
[[ "$result" == "$MSG_GREP_GUIDE" ]] && continue
# --- 4. Parse result based on mode ---
if [[ "$mode" == "grep" ]]; then
file=$(echo "$result" | cut -d\| -f1)
line=$(echo "$result" | cut -d\| -f2)
if [[ ! -f "$file" ]]; then
echo "⚠️ File not found: $file" >&2
return
fi
else
file="$result"
line=""
fi
# --- 5. Execute action based on key pressed ---
if [[ "$key" == "ctrl-o" ]]; then
if [[ ! -f "$file" ]]; then
echo "❌ Error: File not found: $file" >&2
return 1
fi
if [[ "$IS_VSCODE" -eq 1 ]]; then
if [[ -n "$line" ]]; then
if code --goto "$file:$line" 2>/dev/null; then
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
fi
else
if code "$file" 2>/dev/null; then
echo "📄 Opened: $file"
else
echo "❌ Error: Failed to open file in VSCode" >&2
return 1
fi
fi
else
if [[ -n "$line" ]]; then
if $EDITOR_CMD "+$line" "$file"; then
echo "📄 Opened: $file:$line"
else
echo "❌ Error: Failed to open file with $EDITOR_CMD" >&2
return 1
fi
else
if $EDITOR_CMD "$file"; then
echo "📄 Opened: $file"
else
echo "❌ Error: Failed to open file with $EDITOR_CMD" >&2
return 1
fi
fi
fi
return
fi
if [[ -f "$file" ]]; then
target_dir="$(dirname "$file")"
elif [[ -d "$file" ]]; then
target_dir="$file"
else
echo "❌ Error: Invalid selection: $file" >&2
echo " Path does not exist or is not accessible" >&2
return 1
fi
if [[ -n "$target_dir" && -d "$target_dir" ]]; then
if cd "$target_dir" 2>/dev/null; then
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
fi
else
echo "❌ Error: Target directory not found: $target_dir" >&2
return 1
fi
return
done
}