From f21e07d6745ae1bd5b1a1dce1ac0e8d110652268 Mon Sep 17 00:00:00 2001 From: ch1p Date: Thu, 4 Mar 2021 21:14:08 +0300 Subject: support autodetection of current workspace; convert spaces to tabs --- LICENSE | 2 +- swap_workspaces | 83 ++++++++++++++++++++++++++++++++++----------------------- 2 files changed, 50 insertions(+), 35 deletions(-) diff --git a/LICENSE b/LICENSE index df2a2da..70f4222 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ BSD 2-Clause License -Copyright (c) 2017, 2020, Evgeny Zinoviev +Copyright (c) 2017, 2020, 2021 Evgeny Zinoviev All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/swap_workspaces b/swap_workspaces index f7c8185..a4e4745 100755 --- a/swap_workspaces +++ b/swap_workspaces @@ -1,71 +1,86 @@ #!/bin/bash +current_workspace() { + wmctrl -d | awk '{print $1" "$2}' | grep "*" | awk '{print $1}' +} + workspace_exists() { - wmctrl -d | awk '{print $1}' | grep -w -- "${1}" + wmctrl -d | awk '{print $1}' | grep -w -- "${1}" } workspace_windows() { - wmctrl -l | { - while IFS= read -r line; do - local parts=($line) - local id="${parts[@]:0:1}" - local workspace=${parts[@]:1:1} - if [[ "$workspace" != "$1" ]]; then - continue - fi - echo "$id" - done - } + wmctrl -l | { + while IFS= read -r line; do + local parts=($line) + local id="${parts[@]:0:1}" + local workspace=${parts[@]:1:1} + if [[ "$workspace" != "$1" ]]; then + continue + fi + echo "$id" + done + } } 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 -vice versa. +usage() { + cat <<-_EOF + A program to move all windows from workspace <1> to workspace <2> and + vice versa. + + If the first workspace number is not specified, current workspace is assumed. + Workspace numbering starts with 0. -This is useful when you want to reorder workspaces but your DE doesn't have such -feature (XFCE as an example). + This is useful when you want to reorder workspaces but your DE doesn't have such + feature. -Usage: - $(basename "$0") <1> <2> - -Dependencies: - wmctrl" + Usage: + $(basename "$0") [<1>] <2> + + Dependencies: + wmctrl + _EOF +} FROM=$1 TO=$2 if [ ! -x "$(command -v wmctrl)" ]; then - echo "Please make sure that wmctrl is installed." - exit 1 + echo "Please make sure that wmctrl is installed." + exit 1 fi -if [ -z "$FROM" ] || [ -z "$TO" ]; then - echo "$USAGE" - exit 1 +if [ -z "$TO" ]; then + if [ -z "$FROM" ]; then + echo "$USAGE" + exit 1 + fi + TO="$FROM" + FROM=$(current_workspace) fi if [ "$FROM" == "$TO" ]; then - exit + exit fi if [[ ! $(workspace_exists "$FROM") ]]; then - echo "workspace $FROM not found" - exit 1 + echo "workspace $FROM not found" + exit 1 elif [[ ! $(workspace_exists "$TO") ]]; then - echo "workspace $TO not found" - exit 1 + echo "workspace $TO not found" + exit 1 fi WINDOWS_FROM=($(workspace_windows "$FROM")) WINDOWS_TO=($(workspace_windows "$TO")) for id in "${WINDOWS_FROM[@]}"; do - move_window "$id" "$TO" + move_window "$id" "$TO" done for id in "${WINDOWS_TO[@]}"; do - move_window "$id" "$FROM" + move_window "$id" "$FROM" done -- cgit v1.2.3