Advertisement
Guest User

fibonacciTiling.py

a guest
May 24th, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import getopt
  4. import sys
  5. import os
  6. from i3ipc import Connection, Event
  7.  
  8.  
  9. def find_parent(i3, window_id):
  10.     """
  11.        Find the parent of a given window id
  12.    """
  13.  
  14.     def finder(con, parent):
  15.         if con.id == window_id:
  16.             return parent
  17.         for node in con.nodes:
  18.             res = finder(node, con)
  19.             if res:
  20.                 return res
  21.         return None
  22.  
  23.     return finder(i3.get_tree(), None)
  24.  
  25.  
  26. def set_layout(i3, e):
  27.     """
  28.        Set the layout/split for the currently
  29.        focused window to either vertical or
  30.        horizontal, depending on its width/height
  31.    """
  32.     win = e.container
  33.     parent = find_parent(i3, win.id)
  34.  
  35.     if (parent and parent.layout != 'tabbed'
  36.             and parent.layout != 'stacked'):
  37.  
  38.         if win.rect.height > win.rect.width:
  39.             i3.command('split v')
  40.         else:
  41.             i3.command('split h')
  42.  
  43.  
  44. def print_help():
  45.     print("Usage: " + sys.argv[0] + " [-p path/to/pid.file]")
  46.     print("")
  47.     print("Options:")
  48.     print("    -p path/to/pid.file   Saves the PID for this program in the filename specified")
  49.     print("")
  50.  
  51.  
  52. def main():
  53.     """
  54.    Main function - listen for window focus
  55.        changes and call set_layout when focus
  56.        changes
  57.    """
  58.     opt_list, _ = getopt.getopt(sys.argv[1:], 'hp:')
  59.     pid_file = None
  60.     for opt in opt_list:
  61.         if opt[0] == "-h":
  62.             print_help()
  63.             sys.exit()
  64.         if opt[0] == "-p":
  65.             pid_file = opt[1]
  66.  
  67.     if pid_file:
  68.         with open(pid_file, 'w') as f:
  69.             f.write(str(os.getpid()))
  70.  
  71.     i3 = Connection()
  72.     i3.on(Event.WINDOW_FOCUS, set_layout)
  73.     i3.main()
  74.  
  75.  
  76. if __name__ == "__main__":
  77.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement