battery2 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2016 James Murphy
  4. # Licensed under the GPL version 2 only
  5. #
  6. # A battery indicator blocklet script for i3blocks
  7. from subprocess import check_output
  8. import os
  9. import re
  10. config = dict(os.environ)
  11. status = check_output(['acpi'], universal_newlines=True)
  12. if not status:
  13. # stands for no battery found
  14. color = config.get("color_10", "red")
  15. fulltext = "<span color='{}'><span font='FontAwesome'>\uf00d \uf240</span></span>".format(color)
  16. percentleft = 100
  17. else:
  18. # if there is more than one battery in one laptop, the percentage left is
  19. # available for each battery separately, although state and remaining
  20. # time for overall block is shown in the status of the first battery
  21. batteries = status.split("\n")
  22. state_batteries=[]
  23. commasplitstatus_batteries=[]
  24. percentleft_batteries=[]
  25. time = ""
  26. for battery in batteries:
  27. if battery!='':
  28. state_batteries.append(battery.split(": ")[1].split(", ")[0])
  29. commasplitstatus = battery.split(", ")
  30. if not time:
  31. time = commasplitstatus[-1].strip()
  32. # check if it matches a time
  33. time = re.match(r"(\d+):(\d+)", time)
  34. if time:
  35. time = ":".join(time.groups())
  36. timeleft = " ({})".format(time)
  37. else:
  38. timeleft = ""
  39. p = int(commasplitstatus[1].rstrip("%\n"))
  40. if p>0:
  41. percentleft_batteries.append(p)
  42. commasplitstatus_batteries.append(commasplitstatus)
  43. state = state_batteries[0]
  44. commasplitstatus = commasplitstatus_batteries[0]
  45. if percentleft_batteries:
  46. percentleft = int(sum(percentleft_batteries)/len(percentleft_batteries))
  47. else:
  48. percentleft = 0
  49. # stands for charging
  50. color = config.get("color_charging", "yellow")
  51. FA_LIGHTNING = "<span color='{}'><span font='FontAwesome'>\uf0e7</span></span>".format(color)
  52. # stands for plugged in
  53. FA_PLUG = "<span font='FontAwesome'>\uf1e6</span>"
  54. # stands for using battery
  55. FA_BATTERY = "<span font='FontAwesome'>\uf240</span>"
  56. # stands for unknown status of battery
  57. FA_QUESTION = "<span font='FontAwesome'>\uf128</span>"
  58. if state == "Discharging":
  59. fulltext = FA_BATTERY + " "
  60. elif state == "Full":
  61. fulltext = FA_PLUG + " "
  62. timeleft = ""
  63. elif state == "Unknown":
  64. fulltext = FA_QUESTION + " " + FA_BATTERY + " "
  65. timeleft = ""
  66. else:
  67. fulltext = FA_LIGHTNING + " " + FA_PLUG + " "
  68. def color(percent):
  69. if percent < 10:
  70. # exit code 33 will turn background red
  71. return config.get("color_10", "#FFFFFF")
  72. if percent < 20:
  73. return config.get("color_20", "#FF3300")
  74. if percent < 30:
  75. return config.get("color_30", "#FF6600")
  76. if percent < 40:
  77. return config.get("color_40", "#FF9900")
  78. if percent < 50:
  79. return config.get("color_50", "#FFCC00")
  80. if percent < 60:
  81. return config.get("color_60", "#FFFF00")
  82. if percent < 70:
  83. return config.get("color_70", "#FFFF33")
  84. if percent < 80:
  85. return config.get("color_80", "#FFFF66")
  86. return config.get("color_full", "#FFFFFF")
  87. form = '<span color="{}">{}%</span>'
  88. fulltext += form.format(color(percentleft), percentleft)
  89. #fulltext += timeleft
  90. print(fulltext)
  91. print(fulltext)
  92. if percentleft < 10:
  93. exit(33)