-
Notifications
You must be signed in to change notification settings - Fork 4
/
sway-window-move
executable file
·87 lines (78 loc) · 2.28 KB
/
sway-window-move
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
#!/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="Workspace> "
header="Move window to which workspace?"
if [[ -n $SWM_FIFO ]]; then
str=$(cat $SWM_FIFO)
# rm the fifo, we are done with it
rm -rf $SWM_FIFO
selection=$(printf $str | fzf --header=$header --print-query --prompt="$prompt" $fzf_color)
readarray -t query_and_workspace <<< "$selection"
query="${query_and_workspace[0]}"
workspace="${query_and_workspace[1]}"
if [[ -n $workspace ]];
then
selection=$workspace
elif [[ -n $query ]];
then
if [[ ${query##*:} == "new" ]];
then
selection=${query%:*}
else
selection=$query
fi
else
exit
fi
swaymsg focus tiling
swaymsg move window to workspace $selection
swaymsg workspace $selection
exit
fi
# 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.
columns=0
lines=0
str=""
workspaces=($(swaymsg -t get_workspaces -r | jq -r -c '.[] | .name'))
for workspace in "${workspaces[@]}"; do
if [[ ${#workspace} -gt $columns ]];
then
columns=${#workspace}
fi
lines=$((lines+1))
str="$str$workspace\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
# create fifo and launch a terminal with the title "fzf-switcher"
# run the script in the new terminal which will see the env vars
# and execute the first if block in this script.
fifo=/tmp/sws-$(date +%s)
mkfifo $fifo
SWM_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