#!/usr/bin/env python3 # # Copyright (C) 2016 James Murphy # Licensed under the GPL version 2 only # # A battery indicator blocklet script for i3blocks from subprocess import check_output import os import re config = dict(os.environ) status = check_output(['acpi'], universal_newlines=True) if not status: # stands for no battery found fulltext = "\uf00d \uf240".format(color) percentleft = 100 else: # if there is more than one battery in one laptop, the percentage left is # available for each battery separately, although state and remaining # time for overall block is shown in the status of the first battery batteries = status.split("\n") state_batteries=[] commasplitstatus_batteries=[] percentleft_batteries=[] for battery in batteries: if battery!='': state_batteries.append(battery.split(": ")[1].split(", ")[0]) commasplitstatus = battery.split(", ") p = int(commasplitstatus[1].rstrip("%\n")) if p>0: percentleft_batteries.append(p) commasplitstatus_batteries.append(commasplitstatus) state = state_batteries[0] commasplitstatus = commasplitstatus_batteries[0] if percentleft_batteries: percentleft = int(sum(percentleft_batteries)/len(percentleft_batteries)) else: percentleft = 0 def color(percent): if percent < 35: return colors.get("low") if percent < 75 and percent > 35: return colors.get("mid") if percent > 50: return colors.get("high") return "#FFFFFF" B_TEMPLATE = "{}" B_FULL = B_TEMPLATE.format("\uf240") B_MID = B_TEMPLATE.format("\uf242") B_DISCHARING = B_TEMPLATE.format("\uf241") B_CHARGING = B_TEMPLATE.format("\uf1e6") B_DOWN = B_TEMPLATE.format("\uf06a") B_UNKNOWN = B_TEMPLATE.format("\uf128") colors = { 'low': '#fb4934', 'mid': '#fabd2f', 'high': '#b8bb26' } display = "{}{}%" battery = "" color = color(percentleft) if state == "Discharging": battery = B_DISCHARING + " " elif state == "Full": battery = B_FULL + " " elif state == "Unknown": battery = B_UNKNOWN + " " + B_FULL + " " else: battery = B_CHARGING + " " + B_MID + " " display = display.format(color, battery, percentleft) print(display) if percentleft < 10: exit(33)