aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/hyprland-switch-tag
blob: 06840c9b41b12e11a2ffbe86047516867a5a373b (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env bash

# Pascal Jaeger <pascal.jaeger@leimstift.de>

# utils
green="\033[0;32m"
red="\033[0;31m"
blue="\033[0;34m"
nocolor="\033[0m"

#util functions
check() {
	command -v "$1" 1>/dev/null
}

ok() {
	echo -e "[$green  $nocolor] $*"
}

err() {
	echo -e "[$red  $nocolor] $*"
}
optional() {
	echo -e "[$blue  $nocolor] $*"
}

notify() {
	# shellcheck disable=SC2015
	check notify-send && {
		notify-send "$@"
	} || {
		echo "$@"
	}
}

checkUtils() {
	# shellcheck disable=SC2015
	check grep && ok "grep" || err "grep"
	# shellcheck disable=SC2015
	check grep && ok "cut" || err "cut"
	# shellcheck disable=SC2015
	check notify-send && ok "notify-send (Optional)" || optional "notify-send (Optional)"
	exit
}

basicChecks() {
	check hyprctl || {
		notify "Seriously mate!!" "Start Hyprland before this script"
		exit 1
	}
	pgrep -x Hyprland &>/dev/null || {
		notify "Make Sure Hyprland Session is running."
		exit 1
	}
}

help() {
	cat <<EOF
  This is a bash script to move arbitrary workspace to arbritrary monitor and to swap workspaces between
  monitors if the desired workspace is already active on a monitor for Hyprland using hyprctl.

  flags:
    -h: Displays This help menu
    -c: Checks for all dependencies

  Usage: try_swap_workspace [WORKSPACE]
    bind = ALT,1,exec, /path/to/try_swap_workspace/binary 1
    (where the last 1 is the workspace that should be shown on the currently active monitor)

EOF
}

getArgs() {
	while [ "$#" -gt 0 ]; do
		case "$1" in
		-h | --help)
			help
			exit 0
			;;
		-c)
			checkUtils
			;;
    (*[!0-9]*)
      # contains non-numbers
      help
      echo ""
      echo "Wrong argument given"
      exit 1
      ;;
    *)
      # only nubers left, so good
      switch_or_swap "$1"
      ;;
    esac
    shift
  done
}


#variables
mon_wrkspcs=()

get_active_mon() {
  echo $(($(hyprctl monitors | grep 'focused' | grep -n 'yes' | cut -c1)-1))
}

get_workspaces_array() {
  local workspaces
  workspaces=$(hyprctl monitors | grep 'active workspace' | cut -f3 -d' ')
  SAVEIFS=$IFS
  IFS=$'\n'
  mon_wrkspcs=($workspaces)
  IFS=$SAVEIFS
}

# first argument: workspace to switch to
# second argument: monitor to switch workspace on
switch_workspace() {
  local target_wrkspc=$1
  local target_mon=$2
  hyprctl dispatch moveworkspacetomonitor "$target_wrkspc" "$target_mon"
  hyprctl dispatch workspace "$target_wrkspc"
}
# first argument: monitor the workspace should go to
# second argument: monitor the workspace is currently displayed on
swap_workspace() {
  local target_mon=$1
  local source_mon=$2
  hyprctl dispatch swapactiveworkspaces "$target_mon" "$source_mon"
}

# first argument: workspace to switch to active monitor
switch_or_swap() {
  target_mon=$(get_active_mon)
  target_wrkspc=$1
  get_workspaces_array
  # check if the workspace is currently displayed on another monitor
  local currently_active_on_mon=-1
  for (( i=0; i<${#mon_wrkspcs[@]}; i++ ))
  do
    if [[ "$target_wrkspc" == "${mon_wrkspcs[$i]}" ]]; then
      currently_active_on_mon=$i
    fi
  done
  if [[ $currently_active_on_mon -lt 0 ]]; then
    # workspace is not active on any monitor, do a normal switch
    ok "switching workspace $target_wrkspc to monitor $target_mon"
    switch_workspace "$target_wrkspc" "$target_mon"
  else
    # workspace is already active on other monitor, swap workspaces between monitor
    ok "swapping workspaces between $target_mon to monitor $currently_active_on_mon"
    swap_workspace "$target_mon" "$currently_active_on_mon"
  fi
}

main() {
  basicChecks
  getArgs "$@"
}

main "$@"