xresources_palette.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. # Basend on the implementation in Qtile's documentation, modified to be more compliant with the Xresources specifications
  2. # See https://qutebrowser.org/doc/help/configuring.html under "Reading colors from Xresources"
  3. import subprocess, re
  4. def read_xresources():
  5. props = {}
  6. x = subprocess.run(['xrdb', '-query'], capture_output=True, check=True, text=True)
  7. lines = x.stdout.split('\n')
  8. for line in lines:
  9. prop, _, value = line.partition(':\t')
  10. props[prop] = value
  11. return props
  12. xresources=read_xresources()
  13. def query_xresources(prefix, name, default):
  14. prefix=prefix + "."
  15. try:
  16. return xresources[prefix+name]
  17. except:
  18. while len(prefix)>0 :
  19. try:
  20. return xresources["*"+prefix+name]
  21. except:
  22. prefix=prefix[1:]
  23. try:
  24. return xresources["*"+name]
  25. except:
  26. return default
  27. def xresources_get_color(prefix, name, default):
  28. return re.sub("/","",re.sub("rgb:","#",query_xresources(prefix, name, default)))