Remove dependency to color library

This commit is contained in:
Martin Blazik
2022-07-19 13:41:36 +02:00
committed by lachtan
parent d967adac09
commit 8a39c68713

View File

@@ -6,7 +6,6 @@ from sys import stdout
from os import path, environ
from os.path import join, isdir
from collections import namedtuple, OrderedDict
from termcolor import colored
POWER_CLASS = '/sys/class/power_supply'
@@ -14,10 +13,16 @@ BATTERIES = (
environ.get('BATTERY', 'BAT0'),
'cw2015-battery'
)
BATTERY = list(filter(lambda bat: isdir(join(POWER_CLASS, bat)), BATTERIES))[0]
UEVENT = join(POWER_CLASS, BATTERY, 'uevent')
DISCHARNGING = 'Discharging'
RESET = '0'
RED = '0;31'
GREEN = '0;32'
YELLOW = '0;33'
BLUE = '0;34'
MAGENTA = '0;35'
CYAN = '0;36'
BatteryInfo = namedtuple(
'BatteryInfo',
@@ -47,6 +52,22 @@ def print_help():
print(text)
def find_battery_uevent(batteries):
for battery in batteries:
path = join(POWER_CLASS, battery)
if isdir(path):
return join(path, 'uevent')
return None
def start_color(color):
return '\033[' + color + 'm'
def colored(text, color):
return start_color(color) + text + start_color(RESET)
def read_battery(filename):
with open(filename) as file:
lines = file.readlines()
@@ -76,12 +97,12 @@ def terminal_colored(text, color):
def format_status(info):
color = 'red' if info.status == DISCHARNGING else 'green'
color = RED if info.status == DISCHARNGING else GREEN
return terminal_colored(info.status, color)
def format_percent(info):
color = 'green' if info.currrent_percent > 30 else 'red'
color = GREEN if info.currrent_percent > 30 else RED
return terminal_colored(f'{info.currrent_percent}%', color)
@@ -91,7 +112,7 @@ def format_time_to_empty(info):
else:
hours = info.time_to_empty // 60
minutes = info.time_to_empty % 60
return terminal_colored(f"{hours}:{minutes:02d}", 'cyan')
return terminal_colored(f"{hours}:{minutes:02d}", CYAN)
def print_battery_detail(info):
@@ -114,21 +135,25 @@ def print_battery_info(info):
print(state)
def dump_battery_info(battery):
print(f"# {UEVENT}")
def dump_battery_info(uevent, battery):
print(f"# {uevent}")
for key, value in battery.items():
print(f"{key} = {value}")
def main(args):
battery = read_battery(UEVENT)
uevent = find_battery_uevent(BATTERIES)
if not uevent:
print("No battery found")
return
battery = read_battery(uevent)
info = load_battery_info(battery)
if '-h' in args:
print_help()
elif '-d' in args:
dump_battery_info(battery)
elif '-i' in args:
print_battery_info(info)
print_battery_info(uevent, info)
else:
print_battery_detail(info)