-
Notifications
You must be signed in to change notification settings - Fork 4
/
sway-marks-switcher
executable file
·81 lines (71 loc) · 2.08 KB
/
sway-marks-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
#!/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="Marks> "
header="Switch to which mark? (!) to delete"
if [[ -n $SMS_FIFO ]]; then
# we are in the forked shell, read from
# the provided FIFO and obtain the marks str
# and pipe to fzf for selection.
str=$(cat $SMS_FIFO)
rm -rf $SMS_FIFO
selection=$(printf $str | fzf --header=$header --prompt="$prompt" $fzf_color --expect="!")
readarray -t op_and_mark <<< "$selection"
op="${op_and_mark[0]}"
mark="${op_and_mark[1]}"
if [[ -z $mark ]]; then
exit
fi
if [[ $op == "d" ]]; then
swaymsg unmark $mark
exit
fi
swaymsg "[con_mark=\b$mark\b]" focus
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=1
lines=0
str=""
marks=($(swaymsg -t get_marks | jq -r '.[]'))
for mark in "${marks[@]}"; do
if [[ ${#mark} -gt $columns ]];
then
columns=${#mark}
fi
lines=$((lines+1))
str="$str$mark\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/sts-$(date +%s)
mkfifo $fifo
SMS_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