-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ftwind
executable file
·74 lines (60 loc) · 1.69 KB
/
ftwind
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
#!/usr/bin/env bash
# ftwind - switch window (based on @george-b)
GREEN='\033[00;32m'
RESTORE='\033[0m'
# Get information about windows from tmux
windows_str=$(tmux list-windows -F '#I #{pane_current_path} #{window_name} #{window_active}')
# Process windows_str
windows=''
delim=''
actives=()
nums=()
while read -r line; do
line_array=($line)
num=${line_array[0]}
dir_name=$(basename ${line_array[1]})
name=${line_array[2]}
active=${line_array[3]}
# Pad num
[ "$num" -le "9" ] && num="$num "
# Change color and add * if active
if [ "$active" -eq "1" ]
then
actives+=("*")
nums+=("$GREEN$num$RESTORE")
else
actives+=(" ")
nums+=("$num")
fi
# Create output line
out=$(echo -e "$name\t$dir_name")
# Add to windows string
windows=$(printf '%s%s%s' "$windows" "$delim" "$out")
# So that join works
delim=$'\n'
done <<< "$windows_str"
# Align on columns
windows=$(column -s $'\t' -t <<< "$windows")
# Split by newlines
IFS=$'\n' read -rd '' -a window_arr <<<"$windows"
# Add * and number at beginning, as these aren't handled correctly by column
# command
windows=''
delim=''
for (( i=0; i<${#window_arr[*]}; ++i))
do
out=$(echo -e "${actives[$i]} ${nums[$i]} ${window_arr[$i]}")
windows=$(printf '%s%s%s%s' "$windows" "$delim" "$out")
delim=$'\n'
done
# Run fzf and check result
target=$(echo "$windows" | fzf-tmux +m --reverse --ansi)
res=$?
[ "$res" -eq "130" ] && exit 0
[ "$res" -eq "0" ] || exit $res
# Extract window number, handling * correctly
target_window=$(echo "$target" | cut -d' ' -f2)
[ "$target_window" = "" ] && \
target_window=$(echo "$target" | cut -d' ' -f3)
# Select window
tmux select-window -t $target_window