Advertisement
wizard10000

polywins.sh.202100806

Aug 6th, 2021
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.89 KB | None | 0 0
  1. #!/bin/sh
  2. # POLYWINS
  3.  
  4. # SETTINGS {{{ ---
  5.  
  6. active_text_color="#363533"
  7. active_bg="#a3a3a3"
  8. active_underline=
  9.  
  10. inactive_text_color=
  11. inactive_text_color=
  12.  
  13. inactive_bg=
  14. inactive_underline=
  15.  
  16. separator="◦"
  17. show="window_class" # options: window_title, window_class, window_classname
  18. forbidden_classes="Polybar Conky Gmrun"
  19. forbidden_names="Audacious Playlist Editor"
  20. empty_desktop_message="desktop"
  21.  
  22. char_limit=20
  23. max_windows=15
  24. char_case="lower"
  25. add_spaces="true"
  26. resize_increment=16
  27. wm_border_width=1 # setting this might be required for accurate resize position
  28.  
  29. # --- }}}
  30.  
  31.  
  32. main() {
  33.     # If no argument passed...
  34.     if [ -z "$2" ]; then
  35.         # ...print new window list every time
  36.         # the active window changes or
  37.         # a window is opened or closed
  38.         xprop -root -spy _NET_CLIENT_LIST _NET_ACTIVE_WINDOW |
  39.             while IFS= read -r _; do
  40.                 generate_window_list
  41.             done
  42.  
  43.     # If arguments are passed, run requested on-click function
  44.     else
  45.         "$@"
  46.     fi
  47. }
  48.  
  49.  
  50.  
  51. # ON-CLICK FUNCTIONS {{{ ---
  52.  
  53. raise_or_minimize() {
  54.     if [ "$(get_active_wid)" = "$1" ]; then
  55.         wmctrl -ir "$1" -b toggle,hidden
  56.     else
  57.         wmctrl -ia "$1"
  58.     fi
  59. }
  60.  
  61. close() {
  62.     wmctrl -ic "$1"
  63. }
  64.  
  65. slop_resize() {
  66.     wmctrl -ia "$1"
  67.     wmctrl -ir "$1" -e "$(slop -f 0,%x,%y,%w,%h)"
  68. }
  69.  
  70. increment_size() {
  71.     while IFS="[ .]" read -r wid ws wx wy ww wh _; do
  72.         test "$wid" != "$1" && continue
  73.         x=$(( wx - wm_border_width * 2 - resize_increment / 2 ))
  74.         y=$(( wy - wm_border_width * 2 - resize_increment / 2 ))
  75.         w=$(( ww + resize_increment ))
  76.         h=$(( wh + resize_increment ))
  77.     done <<-EOF
  78.     $(wmctrl -lG)
  79.     EOF
  80.  
  81.     wmctrl -ir "$1" -e "0,$x,$y,$w,$h"
  82. }
  83.  
  84. decrement_size() {
  85.     while IFS="[ .]" read -r wid ws wx wy ww wh _; do
  86.         test "$wid" != "$1" && continue
  87.         x=$(( wx - wm_border_width * 2 + resize_increment / 2 ))
  88.         y=$(( wy - wm_border_width * 2 + resize_increment / 2 ))
  89.         w=$(( ww - resize_increment ))
  90.         h=$(( wh - resize_increment ))
  91.     done <<-EOF
  92.     $(wmctrl -lG)
  93.     EOF
  94.  
  95.     wmctrl -ir "$1" -e "0,$x,$y,$w,$h"
  96. }
  97.  
  98. # --- }}}
  99.  
  100.  
  101.  
  102. # WINDOW LIST SETUP {{{ ---
  103.  
  104. active_left="%{F$active_text_color}"
  105. active_right="%{F-}"
  106. inactive_left="%{F$inactive_text_color}"
  107. inactive_right="%{F-}"
  108. separator="%{F$inactive_text_color}$separator%{F-}"
  109.  
  110. if [ -n "$active_underline" ]; then
  111.     active_left="${active_left}%{+u}%{u$active_underline}"
  112.     active_right="%{-u}${active_right}"
  113. fi
  114.  
  115. if [ -n "$active_bg" ]; then
  116.     active_left="${active_left}%{B$active_bg}"
  117.     active_right="%{B-}${active_right}"
  118. fi
  119.  
  120. if [ -n "$inactive_underline" ]; then
  121.     inactive_left="${inactive_left}%{+u}%{u$inactive_underline}"
  122.     inactive_right="%{-u}${inactive_right}"
  123. fi
  124.  
  125. if [ -n "$inactive_bg" ]; then
  126.     inactive_left="${inactive_left}%{B$inactive_bg}"
  127.     inactive_right="%{B-}${inactive_right}"
  128. fi
  129.  
  130. get_active_wid() {
  131.     active_wid=$(xprop -root _NET_ACTIVE_WINDOW)
  132.     active_wid="${active_wid#*\# }"
  133.     active_wid="${active_wid%,*}" # Necessary for XFCE
  134.     while [ ${#active_wid} -lt 10 ]; do
  135.         active_wid="0x0${active_wid#*x}"
  136.     done
  137.     echo "$active_wid"
  138. }
  139.  
  140. get_active_workspace() {
  141.     wmctrl -d |
  142.         while IFS="[ .]" read -r number active_status _; do
  143.             test "$active_status" = "*" && echo "$number" && break
  144.         done
  145. }
  146.  
  147. generate_window_list() {
  148.     active_workspace=$(get_active_workspace)
  149.     active_wid=$(get_active_wid)
  150.     window_count=0
  151.     on_click="$0"
  152.  
  153.     # Format each window name one by one
  154.     # Space and . are both used as IFS,
  155.     # because classname and class are separated by '.'
  156.     while IFS="[ .\.]" read -r wid ws cname cls host title; do
  157.         # Don't show the window if on another workspace (-1 = sticky)
  158.         if [ "$ws" != "$active_workspace" ] && [ "$ws" != "-1" ]; then
  159.             continue
  160.         fi
  161.  
  162.         # Don't show the window if its class is forbidden
  163.         case "$forbidden_classes" in
  164.             *$cls*) continue ;;
  165.         esac
  166.  
  167.         # Don't show the window if its name is forbidden
  168.         case "$forbidden_names" in
  169.             *$title*) continue ;;
  170.         esac
  171.  
  172.         # If max number of windows reached, just increment
  173.         # the windows counter
  174.         if [ "$window_count" -ge "$max_windows" ]; then
  175.             window_count=$(( window_count + 1 ))
  176.             continue
  177.         fi
  178.        
  179.         # Show the user-selected window property
  180.         case "$show" in
  181.             "window_class") w_name="$cls" ;;
  182.             "window_classname") w_name="$cname" ;;
  183.             "window_title") w_name="$title" ;;
  184.         esac
  185.        
  186.         # Use user-selected character case
  187.         case "$char_case" in
  188.             "lower") w_name=$(
  189.                 echo "$w_name" | tr '[:upper:]' '[:lower:]'
  190.                 ) ;;
  191.             "upper") w_name=$(
  192.                 echo "$w_name" | tr '[:lower:]' '[:upper:]'
  193.                 ) ;;
  194.         esac
  195.  
  196.         # Truncate displayed name to user-selected limit
  197.         if [ "${#w_name}" -gt "$char_limit" ]; then
  198.             w_name="$(echo "$w_name" | cut -c1-$((char_limit-1)))…"
  199.         fi
  200.  
  201.         # Apply add-spaces setting
  202.         if [ "$add_spaces" = "true" ]; then
  203.             w_name=" $w_name "
  204.         fi
  205.  
  206.         # Add left and right formatting to displayed name
  207.         if [ "$wid" = "$active_wid" ]; then
  208.             w_name="${active_left}${w_name}${active_right}"
  209.         else
  210.             w_name="${inactive_left}${w_name}${inactive_right}"
  211.         fi
  212.  
  213.         # Add separator unless the window is first in list
  214.         if [ "$window_count" != 0 ]; then
  215.             printf "%s" "$separator"
  216.         fi
  217.  
  218.         # Add on-click action Polybar formatting
  219.         printf "%s" "%{A1:$on_click raise_or_minimize $wid:}"
  220.         printf "%s" "%{A2:$on_click close $wid:}"
  221.         printf "%s" "%{A3:$on_click slop_resize $wid:}"
  222.         printf "%s" "%{A4:$on_click increment_size $wid:}"
  223.         printf "%s" "%{A5:$on_click decrement_size $wid:}"
  224.         # Print the final window name
  225.         printf "%s" "$w_name"
  226.         printf "%s" "%{A}%{A}%{A}%{A}%{A}"
  227.  
  228.         window_count=$(( window_count + 1 ))
  229.     done <<-EOF
  230.     $(wmctrl -lx)
  231.     EOF
  232.  
  233.     # After printing all the windows,
  234.     # print number of hidden windows
  235.     if [ "$window_count" -gt "$max_windows" ]; then
  236.         printf "%s" "+$(( window_count - max_windows ))"
  237.     fi
  238.  
  239.     # Print empty desktop message if no windows are open
  240.     if [ "$window_count" = 0 ]; then
  241.         printf "%s" "$empty_desktop_message"
  242.     fi
  243.    
  244.     # Print newline
  245.     echo ""
  246. }
  247.  
  248. # --- }}}
  249.  
  250. main "$@"
  251.  
  252.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement