#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import subprocess
import sys


# Battery status strings reported by acpi
_STATUS_UNKNOWN     = 'Unknown'
_STATUS_CHARGING    = 'Charging'
_STATUS_DISCHARGING = 'Discharging'

# Font Awesome icons used to display battery status
_ICON_CHARGE   = '' # fa-bolt (0xf0e7)
_ICON_BATTERY0 = '' # fa-battery-empty (0xf244)
_ICON_BATTERY1 = '' # fa-battery-quarter (0xf243)
_ICON_BATTERY2 = '' # fa-battery-half (0xf242)
_ICON_BATTERY3 = '' # fa-battery-three-quarter (0xf241)
_ICON_BATTERY4 = '' # fa-battery-full (0xf240)

# Percentage below which this script will return an urgent flag
_URGENT_THRESHOLD = 5

def _get_xres(resource):
  try:
    return subprocess.check_output(['xgetres', resource], encoding='utf-8')
  except subprocess.CalledProcessError:
    return None

def _get_icon_and_color(status, percent):
  """ Returns an icon and color for the current battery status.
  Args:
    status: An acpi string representing the battery status.
    percent: An integer representing the battery level [0,100].
  Returns:
    A tuple whose first element is a UTF-8 font icon and whose second element
    is a hex color string of the form '#RRGGBB.'
  """

  # ACPI incorrectly reports Unknown status when battery is full.
  if status == _STATUS_CHARGING or status == _STATUS_UNKNOWN:
      return (_ICON_CHARGE, _get_xres('i3blocks.Battery.charging'))
  elif percent < 13:
      return (_ICON_BATTERY0, _get_xres('i3blocks.Battery.warning'))
  elif percent < 38:
      return (_ICON_BATTERY1, None)
  elif percent < 63:
      return (_ICON_BATTERY2, None)
  elif percent < 88:
      return (_ICON_BATTERY3, None)
  else:
      return (_ICON_BATTERY4, None)


def main(argv):
  battery_info = subprocess.check_output(['acpi', '-b'], encoding='utf-8')
  if not battery_info:
    return

  # TODO: Handle more than one battery.
  battery_info = battery_info.split('\n')
  battery_info = battery_info[0]

  match = re.search('Battery [0-9]: ([A-Za-z]*), ([0-9]+)%', battery_info)
  status = match.group(1)
  percent = max(0, min(100, int(match.group(2))))

  icon, color = _get_icon_and_color(status, percent)

  # Font Awesome 5 does not include the space character and Pango seems to have
  # trouble falling back to a different font without putting it in a span.
  display_text = "{}<span fallback='true'> </span>{}%".format(icon, percent)

  print(display_text)
  print(display_text)
  if color: print(color)

  if status != _STATUS_CHARGING and percent < _URGENT_THRESHOLD:
      sys.exit(33)


if __name__ == "__main__":
    main(sys.argv)

1ce3d8529c8dfef423c8b64994e871ef1581daf4
<mask token>


def _get_xres(resource):
    try:
        return subprocess.check_output(['xgetres', resource], encoding='utf-8')
    except subprocess.CalledProcessError:
        return None


def _get_icon_and_color(status, percent):
    """ Returns an icon and color for the current battery status.
  Args:
    status: An acpi string representing the battery status.
    percent: An integer representing the battery level [0,100].
  Returns:
    A tuple whose first element is a UTF-8 font icon and whose second element
    is a hex color string of the form '#RRGGBB.'
  """
    if status == _STATUS_CHARGING or status == _STATUS_UNKNOWN:
        return _ICON_CHARGE, _get_xres('i3blocks.Battery.charging')
    elif percent < 13:
        return _ICON_BATTERY0, _get_xres('i3blocks.Battery.warning')
    elif percent < 38:
        return _ICON_BATTERY1, None
    elif percent < 63:
        return _ICON_BATTERY2, None
    elif percent < 88:
        return _ICON_BATTERY3, None
    else:
        return _ICON_BATTERY4, None


<mask token>

<mask token>


def _get_xres(resource):
    try:
        return subprocess.check_output(['xgetres', resource], encoding='utf-8')
    except subprocess.CalledProcessError:
        return None


def _get_icon_and_color(status, percent):
    """ Returns an icon and color for the current battery status.
  Args:
    status: An acpi string representing the battery status.
    percent: An integer representing the battery level [0,100].
  Returns:
    A tuple whose first element is a UTF-8 font icon and whose second element
    is a hex color string of the form '#RRGGBB.'
  """
    if status == _STATUS_CHARGING or status == _STATUS_UNKNOWN:
        return _ICON_CHARGE, _get_xres('i3blocks.Battery.charging')
    elif percent < 13:
        return _ICON_BATTERY0, _get_xres('i3blocks.Battery.warning')
    elif percent < 38:
        return _ICON_BATTERY1, None
    elif percent < 63:
        return _ICON_BATTERY2, None
    elif percent < 88:
        return _ICON_BATTERY3, None
    else:
        return _ICON_BATTERY4, None


def main(argv):
    battery_info = subprocess.check_output(['acpi', '-b'], encoding='utf-8')
    if not battery_info:
        return
    battery_info = battery_info.split('\n')
    battery_info = battery_info[0]
    match = re.search('Battery [0-9]: ([A-Za-z]*), ([0-9]+)%', battery_info)
    status = match.group(1)
    percent = max(0, min(100, int(match.group(2))))
    icon, color = _get_icon_and_color(status, percent)
    display_text = "{}<span fallback='true'> </span>{}%".format(icon, percent)
    print(display_text)
    print(display_text)
    if color:
        print(color)
    if status != _STATUS_CHARGING and percent < _URGENT_THRESHOLD:
        sys.exit(33)


if __name__ == '__main__':
    main(sys.argv)

<mask token>
_STATUS_UNKNOWN = 'Unknown'
_STATUS_CHARGING = 'Charging'
_STATUS_DISCHARGING = 'Discharging'
_ICON_CHARGE = '\uf0e7'
_ICON_BATTERY0 = '\uf244'
_ICON_BATTERY1 = '\uf243'
_ICON_BATTERY2 = '\uf242'
_ICON_BATTERY3 = '\uf241'
_ICON_BATTERY4 = '\uf240'
_URGENT_THRESHOLD = 5


def _get_xres(resource):
    try:
        return subprocess.check_output(['xgetres', resource], encoding='utf-8')
    except subprocess.CalledProcessError:
        return None


def _get_icon_and_color(status, percent):
    """ Returns an icon and color for the current battery status.
  Args:
    status: An acpi string representing the battery status.
    percent: An integer representing the battery level [0,100].
  Returns:
    A tuple whose first element is a UTF-8 font icon and whose second element
    is a hex color string of the form '#RRGGBB.'
  """
    if status == _STATUS_CHARGING or status == _STATUS_UNKNOWN:
        return _ICON_CHARGE, _get_xres('i3blocks.Battery.charging')
    elif percent < 13:
        return _ICON_BATTERY0, _get_xres('i3blocks.Battery.warning')
    elif percent < 38:
        return _ICON_BATTERY1, None
    elif percent < 63:
        return _ICON_BATTERY2, None
    elif percent < 88:
        return _ICON_BATTERY3, None
    else:
        return _ICON_BATTERY4, None


def main(argv):
    battery_info = subprocess.check_output(['acpi', '-b'], encoding='utf-8')
    if not battery_info:
        return
    battery_info = battery_info.split('\n')
    battery_info = battery_info[0]
    match = re.search('Battery [0-9]: ([A-Za-z]*), ([0-9]+)%', battery_info)
    status = match.group(1)
    percent = max(0, min(100, int(match.group(2))))
    icon, color = _get_icon_and_color(status, percent)
    display_text = "{}<span fallback='true'> </span>{}%".format(icon, percent)
    print(display_text)
    print(display_text)
    if color:
        print(color)
    if status != _STATUS_CHARGING and percent < _URGENT_THRESHOLD:
        sys.exit(33)


if __name__ == '__main__':
    main(sys.argv)

import re
import subprocess
import sys
_STATUS_UNKNOWN = 'Unknown'
_STATUS_CHARGING = 'Charging'
_STATUS_DISCHARGING = 'Discharging'
_ICON_CHARGE = '\uf0e7'
_ICON_BATTERY0 = '\uf244'
_ICON_BATTERY1 = '\uf243'
_ICON_BATTERY2 = '\uf242'
_ICON_BATTERY3 = '\uf241'
_ICON_BATTERY4 = '\uf240'
_URGENT_THRESHOLD = 5


def _get_xres(resource):
    try:
        return subprocess.check_output(['xgetres', resource], encoding='utf-8')
    except subprocess.CalledProcessError:
        return None


def _get_icon_and_color(status, percent):
    """ Returns an icon and color for the current battery status.
  Args:
    status: An acpi string representing the battery status.
    percent: An integer representing the battery level [0,100].
  Returns:
    A tuple whose first element is a UTF-8 font icon and whose second element
    is a hex color string of the form '#RRGGBB.'
  """
    if status == _STATUS_CHARGING or status == _STATUS_UNKNOWN:
        return _ICON_CHARGE, _get_xres('i3blocks.Battery.charging')
    elif percent < 13:
        return _ICON_BATTERY0, _get_xres('i3blocks.Battery.warning')
    elif percent < 38:
        return _ICON_BATTERY1, None
    elif percent < 63:
        return _ICON_BATTERY2, None
    elif percent < 88:
        return _ICON_BATTERY3, None
    else:
        return _ICON_BATTERY4, None


def main(argv):
    battery_info = subprocess.check_output(['acpi', '-b'], encoding='utf-8')
    if not battery_info:
        return
    battery_info = battery_info.split('\n')
    battery_info = battery_info[0]
    match = re.search('Battery [0-9]: ([A-Za-z]*), ([0-9]+)%', battery_info)
    status = match.group(1)
    percent = max(0, min(100, int(match.group(2))))
    icon, color = _get_icon_and_color(status, percent)
    display_text = "{}<span fallback='true'> </span>{}%".format(icon, percent)
    print(display_text)
    print(display_text)
    if color:
        print(color)
    if status != _STATUS_CHARGING and percent < _URGENT_THRESHOLD:
        sys.exit(33)


if __name__ == '__main__':
    main(sys.argv)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import subprocess
import sys


# Battery status strings reported by acpi
_STATUS_UNKNOWN     = 'Unknown'
_STATUS_CHARGING    = 'Charging'
_STATUS_DISCHARGING = 'Discharging'

# Font Awesome icons used to display battery status
_ICON_CHARGE   = '' # fa-bolt (0xf0e7)
_ICON_BATTERY0 = '' # fa-battery-empty (0xf244)
_ICON_BATTERY1 = '' # fa-battery-quarter (0xf243)
_ICON_BATTERY2 = '' # fa-battery-half (0xf242)
_ICON_BATTERY3 = '' # fa-battery-three-quarter (0xf241)
_ICON_BATTERY4 = '' # fa-battery-full (0xf240)

# Percentage below which this script will return an urgent flag
_URGENT_THRESHOLD = 5

def _get_xres(resource):
  try:
    return subprocess.check_output(['xgetres', resource], encoding='utf-8')
  except subprocess.CalledProcessError:
    return None

def _get_icon_and_color(status, percent):
  """ Returns an icon and color for the current battery status.
  Args:
    status: An acpi string representing the battery status.
    percent: An integer representing the battery level [0,100].
  Returns:
    A tuple whose first element is a UTF-8 font icon and whose second element
    is a hex color string of the form '#RRGGBB.'
  """

  # ACPI incorrectly reports Unknown status when battery is full.
  if status == _STATUS_CHARGING or status == _STATUS_UNKNOWN:
      return (_ICON_CHARGE, _get_xres('i3blocks.Battery.charging'))
  elif percent < 13:
      return (_ICON_BATTERY0, _get_xres('i3blocks.Battery.warning'))
  elif percent < 38:
      return (_ICON_BATTERY1, None)
  elif percent < 63:
      return (_ICON_BATTERY2, None)
  elif percent < 88:
      return (_ICON_BATTERY3, None)
  else:
      return (_ICON_BATTERY4, None)


def main(argv):
  battery_info = subprocess.check_output(['acpi', '-b'], encoding='utf-8')
  if not battery_info:
    return

  # TODO: Handle more than one battery.
  battery_info = battery_info.split('\n')
  battery_info = battery_info[0]

  match = re.search('Battery [0-9]: ([A-Za-z]*), ([0-9]+)%', battery_info)
  status = match.group(1)
  percent = max(0, min(100, int(match.group(2))))

  icon, color = _get_icon_and_color(status, percent)

  # Font Awesome 5 does not include the space character and Pango seems to have
  # trouble falling back to a different font without putting it in a span.
  display_text = "{}<span fallback='true'> </span>{}%".format(icon, percent)

  print(display_text)
  print(display_text)
  if color: print(color)

  if status != _STATUS_CHARGING and percent < _URGENT_THRESHOLD:
      sys.exit(33)


if __name__ == "__main__":
    main(sys.argv)
