Guest User

Untitled

a guest
Aug 4th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. def divide_chunks(l, n):
  2.     for i in range(0, len(l), n):
  3.         yield l[i:i + n]
  4.  
  5. def run(args):
  6.     print("Running: " + args)
  7.     return subprocess.run(args, shell=True, capture_output=True).stdout.decode("ascii")
  8.  
  9. desktops = [1,2,3,4,5,6,7,8,9,0]
  10. # desktops = ['I', 'II', 'III', "IV", "V", "VI", "VII", "VIII", "IX", "X"]
  11.  
  12. import subprocess
  13. monitors = run("bspc query -M --names").split()
  14. print("Connected monitors are: " + " ".join(monitors))
  15.  
  16. #Reorders the list so that the primary monitor gets the first chunk of the desktops
  17. if len(monitors) > 1:
  18.     primary_display = run('xrandr --query | grep " primary" | cut -d" " -f1').rstrip()
  19.     print("Primary display is: " + primary_display)
  20.     try:
  21.         monitors.remove(primary_display)
  22.         monitors.insert(0, primary_display)
  23.     except:
  24.         print(f"Failed to remove primary display: {primary_display}")
  25.  
  26.     #remove disconnected display from bspwm
  27.     disconncted_monitors = run('xrandr --query | grep " disconnected" | cut -d" " -f1').split()
  28.     for monitor in monitors:
  29.         for disconnected in disconncted_monitors:
  30.             if monitor == disconnected:
  31.                 run(f"bspc monitor {monitor} --remove")
  32.  
  33. print("Monitor order is: " + " ".join(monitors))
  34.  
  35. #Useful when we change our monitor setup.
  36. #I.e When we disconnect a monitor we need to take all of the windows from that monitor's workspaces to our currently connected monitors
  37. run("bspc wm --adopt-orphans")
  38.  
  39. import math
  40. chunk_size = math.floor(len(desktops) / len(monitors))
  41. print("Each monitor gets " + str(chunk_size) + " amount of desktops")
  42.  
  43. monitor_desktop_chunks = list(divide_chunks(desktops, chunk_size))
  44. print("There are " + str(len(monitor_desktop_chunks)) + " monitor desktops chunks")
  45.  
  46. #if we dont get an evenly sized chunks, take the leftover and add it to the last monitor
  47. if (len(desktops) % len(monitors)) != 0:
  48.     last_index = len(monitor_desktop_chunks)-1
  49.     overflow = monitor_desktop_chunks[len(monitor_desktop_chunks)-1]
  50.     last_index = len(monitor_desktop_chunks)-2
  51.     print("There chunks are not evenly sized. Addding " + str(overflow) + " desktops to the last monitor")
  52.     monitor_desktop_chunks[last_index] = monitor_desktop_chunks[last_index] + overflow
  53.  
  54. chunk_used_counter = 0
  55. for monitor_name in monitors:
  56.     monitor_assigned_chunk = ' '.join(str(e) for e in monitor_desktop_chunks[chunk_used_counter])
  57.     run(f"bspc monitor {monitor_name} -d {monitor_assigned_chunk}")
  58.     chunk_used_counter = chunk_used_counter + 1
  59.  
Advertisement
Add Comment
Please, Sign In to add comment