Browse Source

Modularized and added ROFI/DMENU completion for workspace layouts

Samuel W. Flint 8 years ago
parent
commit
1024ac684e
1 changed files with 43 additions and 15 deletions
  1. 43 15
      i3-layout

+ 43 - 15
i3-layout

@@ -1,34 +1,62 @@
 #!/bin/zsh -f
 
 if [[ $# -eq 0 ]] ; then
-    echo "i3-layout [ name workspace | list | define workspace name | edit name ]"
+    echo "i3-layout [ name workspace | list | define workspace name | edit name | dmenu ]"
     exit
 fi
 
 LAYOUT=$1
 WORKSPACE=$2
 
+function define-workspace {
+    i3-save-tree --workspace ${WORKSPACE} > ~/.config/i3/layouts/${NAME}.json
+    emacsclient --alternate-editor="" -n ~/.config/i3/layouts/${NAME}.json
+}
+
+function edit-workspace {
+    emacsclient --alternate-editor="" -n ~/.config/i3/layouts/${WORKSPACE}.json
+}
+
+function list-workspaces {
+    for layout in ~/.config/i3/layouts/*.json ;
+    do
+        echo "$(basename -- ${layout} .json)"
+    done
+}
+
+function apply-workspace {
+    if [[ -e ~/.config/i3/layouts/${LAYOUT}.json ]] ; then
+        i3-msg "append_layout ~/.config/i3/layouts/${LAYOUT}.json"
+        [[ -e ~/.config/i3/layouts/${LAYOUT}.after ]] && sh ~/.config/i3/layouts/${LAYOUT}.after
+    else
+        i3-nagbar -m "${LAYOUT} not available" -t error
+    fi
+}
+
+function with-external-completion {
+    if [[ -e `which rofi` ]] ; then
+        LAYOUT=`list-workspaces | rofi -dmenu -p "workspace layout:"`
+
+    else
+        LAYOUT=`list-workspaces | dmenu -p "workspace layout:"`
+        [[ $LAYOUT != "" ]] && apply-workspace
+    fi
+}
+
 case ${LAYOUT} in
     define)
         NAME=$3
-        i3-save-tree --workspace ${WORKSPACE} > ~/.config/i3/layouts/${NAME}.json
-        emacsclient --alternate-editor="" -n ~/.config/i3/layouts/${NAME}.json
+        define-workspace
         ;;
     edit)
-        NAME=$3
-        emacsclient --alternate-editor="" -n ~/.config/i3/layouts/${NAME}.json
+        edit-workspace
         ;;
     list)
-        for layout in ~/.config/i3/layouts/*.json ;
-        do
-            echo " - $(basename -- ${layout} .json)"
-        done
+        list-workspaces
+        ;;
+    dmenu)
+        with-external-completion
         ;;
     *)
-        if [[ -e ~/.config/i3/layouts/${LAYOUT}.json ]] ; then
-            i3-msg "workspace ${WORKSPACE}; append_layout ~/.config/i3/layouts/${LAYOUT}.json"
-            [[ -e ~/.config/i3/layouts/${LAYOUT}.after ]] && sh ~/.config/i3/layouts/${LAYOUT}.after
-        else
-            i3-nagbar -m "${LAYOUT} not available" -t error
-        fi
+        apply-workspace
 esac