Browse Source

Added sway-screens function

Blallo 4 years ago
parent
commit
8512bb3ecb
1 changed files with 79 additions and 0 deletions
  1. 79 0
      .zshrc

+ 79 - 0
.zshrc

@@ -144,6 +144,85 @@ function reload_work_gpg {
   kill -9 $(ps aux|egrep "gpg-agent.*work"|grep -v grep|awk '{print $2}')
 }
 
+function sway-screens {
+  local screens=$(swaymsg -t get_outputs -r)
+  local show_active
+  local show_inactive
+  local var_style="plain"
+  local active_screens=()
+  local inactive_screens=()
+
+  while [ $# -ne 0 ]; do
+    case $1 in
+      -a|--active)
+        show_active=1
+        ;;
+      -n|--inactive)
+        show_inactive=1
+        ;;
+      -s|--style)
+        case $2 in
+          json)
+            var_style="json"
+            ;;
+          plain)
+            ;;
+          *)
+            echo "unknown style ${2}"
+            return 1
+            ;;
+        esac
+        shift
+        ;;
+    esac
+    shift
+  done
+
+  if [ "z${show_active}" != "z" ]; then
+    for s in $(echo $screens|jq -r '.[] | select(.active) | .name'); do
+      active_screens+=($s)
+    done
+  fi
+
+  if [ "z${show_inactive}" != "z" ]; then
+    for s in $(echo $screens|jq -r '.[] | select(.active|not) | .name'); do
+      inactive_screens+=($s)
+    done
+  fi
+
+  if [ "${var_style}" = "plain" ]; then
+    if [ ${#active_screens[@]} -gt 0 ]; then
+      echo "ACTIVE=${active_screens[@]}"
+    fi
+    if [ ${#inactive_screens[@]} -gt 0 ]; then
+      echo "INACTIVE=${inactive_screens[@]}"
+    fi
+  else
+    local active=""
+    for s in $active_screens; do
+      active+="\"$s\","
+    done
+    active=${active%,}
+
+    local inactive=""
+    for s in $inactive_screens; do
+      inactive+="\"$s\","
+    done
+    inactive=${inactive%,}
+
+    local content=""
+    if [ "z${show_active}" != "z" ]; then
+      content+="\"active\": [${active}],"
+    fi
+    if [ "z${show_inactive}" != "z" ]; then
+      content+="\"inactive\": [${inactive}],"
+    fi
+    content=${content%,}
+
+    echo "{${content}}" | jq
+  fi
+}
+
 ## Custom completion
 # certo
 eval "$(_CERTO_COMPLETE=source_zsh certo)"