support autodetection of current workspace; convert spaces to tabs

This commit is contained in:
ch1p 2021-03-04 21:14:08 +03:00
parent 339f942f00
commit f21e07d674
2 changed files with 50 additions and 35 deletions

View File

@ -1,6 +1,6 @@
BSD 2-Clause License BSD 2-Clause License
Copyright (c) 2017, 2020, Evgeny Zinoviev Copyright (c) 2017, 2020, 2021 Evgeny Zinoviev
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without

View File

@ -1,71 +1,86 @@
#!/bin/bash #!/bin/bash
current_workspace() {
wmctrl -d | awk '{print $1" "$2}' | grep "*" | awk '{print $1}'
}
workspace_exists() { workspace_exists() {
wmctrl -d | awk '{print $1}' | grep -w -- "${1}" wmctrl -d | awk '{print $1}' | grep -w -- "${1}"
} }
workspace_windows() { workspace_windows() {
wmctrl -l | { wmctrl -l | {
while IFS= read -r line; do while IFS= read -r line; do
local parts=($line) local parts=($line)
local id="${parts[@]:0:1}" local id="${parts[@]:0:1}"
local workspace=${parts[@]:1:1} local workspace=${parts[@]:1:1}
if [[ "$workspace" != "$1" ]]; then if [[ "$workspace" != "$1" ]]; then
continue continue
fi fi
echo "$id" echo "$id"
done done
} }
} }
move_window() { move_window() {
wmctrl -i -r "$1" -t "$2" wmctrl -i -r "$1" -t "$2"
} }
USAGE="A program to move all windows from workspace <1> to workspace <2> and usage() {
vice versa. cat <<-_EOF
A program to move all windows from workspace <1> to workspace <2> and
vice versa.
This is useful when you want to reorder workspaces but your DE doesn't have such If the first workspace number is not specified, current workspace is assumed.
feature (XFCE as an example). Workspace numbering starts with 0.
Usage: This is useful when you want to reorder workspaces but your DE doesn't have such
$(basename "$0") <1> <2> feature.
Dependencies: Usage:
wmctrl" $(basename "$0") [<1>] <2>
Dependencies:
wmctrl
_EOF
}
FROM=$1 FROM=$1
TO=$2 TO=$2
if [ ! -x "$(command -v wmctrl)" ]; then if [ ! -x "$(command -v wmctrl)" ]; then
echo "Please make sure that wmctrl is installed." echo "Please make sure that wmctrl is installed."
exit 1 exit 1
fi fi
if [ -z "$FROM" ] || [ -z "$TO" ]; then if [ -z "$TO" ]; then
echo "$USAGE" if [ -z "$FROM" ]; then
exit 1 echo "$USAGE"
exit 1
fi
TO="$FROM"
FROM=$(current_workspace)
fi fi
if [ "$FROM" == "$TO" ]; then if [ "$FROM" == "$TO" ]; then
exit exit
fi fi
if [[ ! $(workspace_exists "$FROM") ]]; then if [[ ! $(workspace_exists "$FROM") ]]; then
echo "workspace $FROM not found" echo "workspace $FROM not found"
exit 1 exit 1
elif [[ ! $(workspace_exists "$TO") ]]; then elif [[ ! $(workspace_exists "$TO") ]]; then
echo "workspace $TO not found" echo "workspace $TO not found"
exit 1 exit 1
fi fi
WINDOWS_FROM=($(workspace_windows "$FROM")) WINDOWS_FROM=($(workspace_windows "$FROM"))
WINDOWS_TO=($(workspace_windows "$TO")) WINDOWS_TO=($(workspace_windows "$TO"))
for id in "${WINDOWS_FROM[@]}"; do for id in "${WINDOWS_FROM[@]}"; do
move_window "$id" "$TO" move_window "$id" "$TO"
done done
for id in "${WINDOWS_TO[@]}"; do for id in "${WINDOWS_TO[@]}"; do
move_window "$id" "$FROM" move_window "$id" "$FROM"
done done