-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgx
executable file
·48 lines (42 loc) · 1.31 KB
/
gx
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
#!/usr/bin/env sh
# vim: ft=sh ts=4 sw=4 sts=4 et :
# Open a file using $EDITOR or xdg-open
gx() {
# $@: files to open
if ! command -v file >/dev/null 2>&1; then
echo "file executable not found" >&2
return 1
fi
# Create temporary files to store file lists
text_files=$(mktemp)
other_files=$(mktemp)
trap 'rm -f "$text_files" "$other_files"' EXIT
# Categorize files based on their type
for file in "$@"; do
if file -b "$file" | grep -qE 'text|empty'; then
printf '%s\n' "$file" >>"$text_files"
else
printf '%s\n' "$file" >>"$other_files"
fi
done
# Handle non-text files with xdg-open
if command -v xdg-open >/dev/null 2>&1; then
while read -r file; do
xdg-open "$file" >/dev/null 2>&1
done <"$other_files"
elif [ -s "$other_files" ]; then
echo "xdg-open not found, omit opening files:" >&2
cat "$other_files" >&2
fi
# Handle text files with $EDITOR
if [ -s "$text_files" ]; then
if command -v "$EDITOR" >/dev/null 2>&1; then
# Use xargs to handle multiple files
xargs "$EDITOR" <"$text_files"
else
echo "\$EDITOR not found, omit opening files:" >&2
cat "$text_files" >&2
fi
fi
}
gx "$@"