-
Notifications
You must be signed in to change notification settings - Fork 4
/
sway-tree-switcher
executable file
·82 lines (71 loc) · 2.15 KB
/
sway-tree-switcher
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
#!/bin/bash
SCRIPTNAME=$(basename $0)
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
IFS=$'\n'
if [[ -n $THEME ]]; then
fzf_color="--color=$THEME"
fi
# $THEME env variable set via sway config.
# check for env variable, if exists we are the
# forked shell, get the waiting string from the fifo
# and call fzf and sway from there.
prompt="Windows> "
header="Switch to which window?"
if [[ -n $fifo ]]; then
str=$(cat $fifo)
rm -rf $fifo
selection=$(printf $str | fzf --header=$header --prompt="$prompt" $fzf_color)
id=$(echo $selection | cut -d ":" -f1)
if [[ -z $id ]]; then
exit
fi
swaymsg "[con_id=$id]" focus
exit
fi
windows=($(swaymsg -t get_tree| jq -r 'recurse(.nodes[]?) | recurse(.floating_nodes[]?) | select(.type=="con"), select(.type=="floating_con") | select((.app_id != null) or .name != null) | {id, app_id, name} | .id, .app_id, .name'))
# build the selection string we will ultimate
# pipe to fzf, take note of largest string to set
# columns and number of lines to set lines for alacritty.
str=""
columns=0
lines=0
for ((i=0; i<"${#windows[@]}"; i=i+3,lines++)); do
id="${windows[i]}"
app_id="${windows[i+1]}"
name="${windows[i+2]}"
building_string="$id:"
if [[ $app_id != "null" ]];
then
building_string="$building_string $app_id"
fi
if [[ $name != "null" ]];
then
building_string="$building_string : $name"
fi
if [[ ${#building_string} -gt $columns ]];
then
str_largest="$building_string"
columns=${#building_string}
fi
str="$str$building_string\n"
done
# add some padding to the terminal for
# lines and columns, for columns make sure
# we take the prompt into the padding consideration
lines=$((lines+3))
columns=$((columns+"${#header}"+5))
if [[ columns -gt 100 ]];
then
columns=100
fi
fifo=/tmp/sts-$(date +%s)
mkfifo $fifo
fifo=$fifo $SHELL -c "alacritty \
-o window.dimensions.columns=$columns \
-o window.dimensions.lines=$lines \
-o font.size=16.0 \
-o window.padding.x=20 \
-o window.padding.y=20 \
--title "fzf-switcher" \
-e $SCRIPTPATH/$SCRIPTNAME"&
echo -n $str > $fifo