-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfzf_previewer
executable file
·58 lines (51 loc) · 1.51 KB
/
fzf_previewer
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
#!/bin/bash
# use tree for dir
# else use bat
# used for fzf preview
# Function to find the index of the last space within a substring
find_last_space() {
local str="$1"
local substring_length="$2"
local last_space_index=-1
for (( i=$substring_length; i>=0; i-- )); do
if [ "${str:i:1}" = " " ]; then
last_space_index=$i
break
fi
done
echo "$last_space_index"
}
# Function to format input string into multiple lines with up to $max_length characters each
wrapped_echo() {
local input="$1"
local length=${#input}
local start=0
local end=0
local terminal_width=$(tput cols)
local max_length=$((terminal_width - 5)) # Adjust as needed
while [ $((end + max_length)) -lt $length ]; do
end=$((start + max_length))
# Find the last space within the substring
last_space_index=$(find_last_space "${input:$start:$max_length}" $((max_length - 1)))
if [ $last_space_index -eq -1 ]; then
last_space_index=$max_length
fi
echo "${input:$start:$last_space_index}"
start=$((start + last_space_index + 1))
done
echo "${input:$start}"
}
if [ -z "$1" ]; then
echo [empty]
elif [ ! -e "$1" ]; then
wrapped_echo "$@"
elif [ -d "$1" ]; then
echo "Directory: $1" | bat --style=grid --color=always
tree -C "$1" | head -200
elif file -b "$1" | grep -q "ELF"; then
readelf -WCa "$1"
elif file -b "$1" | grep -q "text"; then
bat --color=always "$1"
else
file -b "$1"
fi