Remove dependency to color library
This commit is contained in:
45
bin/battery
45
bin/battery
@@ -6,7 +6,6 @@ from sys import stdout
|
|||||||
from os import path, environ
|
from os import path, environ
|
||||||
from os.path import join, isdir
|
from os.path import join, isdir
|
||||||
from collections import namedtuple, OrderedDict
|
from collections import namedtuple, OrderedDict
|
||||||
from termcolor import colored
|
|
||||||
|
|
||||||
|
|
||||||
POWER_CLASS = '/sys/class/power_supply'
|
POWER_CLASS = '/sys/class/power_supply'
|
||||||
@@ -14,10 +13,16 @@ BATTERIES = (
|
|||||||
environ.get('BATTERY', 'BAT0'),
|
environ.get('BATTERY', 'BAT0'),
|
||||||
'cw2015-battery'
|
'cw2015-battery'
|
||||||
)
|
)
|
||||||
BATTERY = list(filter(lambda bat: isdir(join(POWER_CLASS, bat)), BATTERIES))[0]
|
|
||||||
UEVENT = join(POWER_CLASS, BATTERY, 'uevent')
|
|
||||||
DISCHARNGING = 'Discharging'
|
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 = namedtuple(
|
||||||
'BatteryInfo',
|
'BatteryInfo',
|
||||||
@@ -47,6 +52,22 @@ def print_help():
|
|||||||
print(text)
|
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):
|
def read_battery(filename):
|
||||||
with open(filename) as file:
|
with open(filename) as file:
|
||||||
lines = file.readlines()
|
lines = file.readlines()
|
||||||
@@ -76,12 +97,12 @@ def terminal_colored(text, color):
|
|||||||
|
|
||||||
|
|
||||||
def format_status(info):
|
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)
|
return terminal_colored(info.status, color)
|
||||||
|
|
||||||
|
|
||||||
def format_percent(info):
|
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)
|
return terminal_colored(f'{info.currrent_percent}%', color)
|
||||||
|
|
||||||
|
|
||||||
@@ -91,7 +112,7 @@ def format_time_to_empty(info):
|
|||||||
else:
|
else:
|
||||||
hours = info.time_to_empty // 60
|
hours = info.time_to_empty // 60
|
||||||
minutes = 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):
|
def print_battery_detail(info):
|
||||||
@@ -114,21 +135,25 @@ def print_battery_info(info):
|
|||||||
print(state)
|
print(state)
|
||||||
|
|
||||||
|
|
||||||
def dump_battery_info(battery):
|
def dump_battery_info(uevent, battery):
|
||||||
print(f"# {UEVENT}")
|
print(f"# {uevent}")
|
||||||
for key, value in battery.items():
|
for key, value in battery.items():
|
||||||
print(f"{key} = {value}")
|
print(f"{key} = {value}")
|
||||||
|
|
||||||
|
|
||||||
def main(args):
|
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)
|
info = load_battery_info(battery)
|
||||||
if '-h' in args:
|
if '-h' in args:
|
||||||
print_help()
|
print_help()
|
||||||
elif '-d' in args:
|
elif '-d' in args:
|
||||||
dump_battery_info(battery)
|
dump_battery_info(battery)
|
||||||
elif '-i' in args:
|
elif '-i' in args:
|
||||||
print_battery_info(info)
|
print_battery_info(uevent, info)
|
||||||
else:
|
else:
|
||||||
print_battery_detail(info)
|
print_battery_detail(info)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user