Advertisement
Guest User

Lemonbar.py

a guest
May 25th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. import threading
  4. import time
  5. import sys
  6. import string
  7. import os
  8.  
  9. status_cmd = 'herbstclient tag_status'
  10. idle_cmd = 'herbstclient --idle'
  11.  
  12. def parse_tags(output):
  13. #Takes herbstclient tag_status and parses it
  14. #returns a nested list of tags.
  15. tags = output.split()
  16. taglist = ''
  17. for i in range(len(tags)):
  18. if tags[i][0] == '#':
  19. taglist += '%{B' + colors[11] + '} ' + tags[i][1:] + ' '
  20. elif tags[i][0] == ':':
  21. taglist += '%{B' + colors[17] + '} ' + tags[i][1:] + ' '
  22. elif tags[i][0] == '.' or '!':
  23. taglist += '%{B' + colors[1] + '} ' + tags[i][1:] + ' '
  24. return taglist
  25.  
  26. def parse_xres():
  27. os.chdir('/home/damian/')
  28. fin = open('.Xresources')
  29. colors = []
  30. for line in fin:
  31. color = line.strip()
  32. if 'color' in color:
  33. rgb = color[color.find('#'):]
  34. colors.append(rgb)
  35. elif 'background' in color:
  36. colors.append(color[color.find('#'):])
  37. elif 'foreground' in color:
  38. colors.append(color[color.find('#'):])
  39. return(colors)
  40.  
  41. def print_colors(colors):
  42. res = ''
  43. for i in range(len(colors)):
  44. res +='%{B' + colors[i] + '}' + str(i)
  45. return res
  46.  
  47. def get_date():
  48. return time.strftime('%a %d %b %Y %R')
  49.  
  50. def get_battery():
  51. try:
  52. fin = open('/sys/class/power_supply/BAT1/uevent')
  53. full = 0
  54. now = 0
  55. for line in fin.readlines():
  56. if line[13:25] == 'CHARGE_FULL=':
  57. full = int(line[25:])
  58. elif line[13:23] == 'CHARGE_NOW':
  59. now = int(line[24:])
  60. res = str(now / full * 100) + '%'
  61. return res
  62. except:
  63. return 'No'
  64.  
  65. def get_event():
  66. #rename, clean up
  67. proc = os.popen(status_cmd)
  68. output = proc.readline()
  69. taglist = parse_tags(output)
  70. print(taglist, '%{B' + colors[1] + '} ', get_date(), ' ', '%{B' + colors[1] + '}Bat:', get_battery()) #, print_colors(colors) ///also, print time here?
  71. proc.close()
  72.  
  73. def event_thread():
  74. get_event()
  75. while True:
  76. line = idlein.readline()
  77. if 'tag' in line:
  78. get_event()
  79. elif 'reload' in line:
  80. idlein.close()
  81. os._exit(0) #Extremely hacky, the interpreter doesn't free up res's
  82.  
  83. def timer_thread():
  84. while True:
  85. get_event()
  86. time.sleep(1)
  87. return o
  88.  
  89. def main_thread():
  90. """docstring for main_thread"""
  91. t1 = threading.Thread(target=event_thread)
  92. t2 = threading.Thread(target=timer_thread)
  93. t1.start()
  94. t2.start()
  95.  
  96. colors = parse_xres()
  97. single_line_out = ''
  98. idlein = os.popen(idle_cmd)
  99.  
  100. if __name__ == '__main__':
  101. main_thread()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement