-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathi3-focus-on-workspace
executable file
·55 lines (48 loc) · 1.77 KB
/
i3-focus-on-workspace
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
#!/usr/bin/env bash
# See https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -o errexit -o errtrace -o nounset -o pipefail
readonly HELP_HEADING='Select a workspace to focus on'
readonly HELP_CONTENT='You can focus on a new (non existing) workspace by using the format "group:number", for example "work:2"'
readonly HELP_FORMAT='<span alpha="50%%" size="smaller"><b>%s</b>
<i>%s</i></span>'
# shellcheck disable=SC2059
# shellcheck disable=SC2155
readonly HELP_TEXT="$(printf "${HELP_FORMAT}" "${HELP_HEADING}" "${HELP_CONTENT}")"
ROFI_CMD=(rofi -dmenu -p 'Workspace' -theme-str 'window {width: 60ch;}'
-mesg "${HELP_TEXT}")
command_exists() {
type "$1" &> /dev/null
}
get_tool() {
filename="$1"
local dir tool
dir="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
tool="${dir}/${filename}"
command_exists "${tool}" || tool="${filename}"
printf '%s\n' "${tool}"
}
main() {
local tool
tool="$(get_tool "${I3_WORKSPACE_GROUPS_CLI:-i3-workspace-groups}")"
mapfile -t workspaces < <("${tool}" list-workspaces --fields 'global_name')
mapfile -t displayed_workspaces < <("${tool}" list-workspaces \
--fields 'group,local_number,window_icons,static_name' |
column -t -s $'\t' -o ' ')
if ! selected="$(printf '%s\n' "${displayed_workspaces[@]}" |
"${ROFI_CMD[@]}")"; then
exit 1
fi
for i in "${!displayed_workspaces[@]}"; do
line="${displayed_workspaces[${i}]}"
if [[ "${line}" == "${selected}" ]]; then
i3-msg 'workspace "'"${workspaces[${i}]}"'"'
exit
fi
done
# No existing workspace was selected, assume it's a new workspace with a
# format of "group:local_number".
group="${selected%%:*}"
local_number="${selected##*:}"
"${tool}" workspace-number --group-name "${group}" "${local_number}"
}
main "$@"