ref: 74cf75831439442d8b85a1408b1ab409b104faa9
i3/i3status.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
#!/usr/bin/env python # -*- coding: utf-8 -*- import json import os import subprocess import sys RED = '#dc322f' GREEN = '#2aa198' NULL = open(os.devnull, 'w') def get_memory(): try: import psutil val = psutil.virtual_memory().percent if val > 80: color = '#FF0000' else: color = '#FFFFFF' text = "MEM: {0}%".format(val) except ImportError: text = "Failed to load psutil" color = '#FF0000' return { 'full_text': str(text), 'name': 'vpn', 'color': color, } def get_vpn(): try: subprocess.check_call( ["/home/honza/dotfiles/bin/is-vpn-connected"], stdout=NULL ) vpn_connected = True except Exception: vpn_connected = False return { 'full_text': "VPN", 'name': 'vpn', 'color': GREEN if vpn_connected else RED } def get_battery(): battery_path = '/sys/class/power_supply/BAT0/uevent' battery_level = 0 battery_status = '' battery_critical = False f = open(battery_path) battery_data = f.readlines() f.close() for line in battery_data: label, value = line.split('=') value = value.replace('\n', '') if label == 'POWER_SUPPLY_CAPACITY': battery_level = int(value) if label == 'POWER_SUPPLY_STATUS': battery_status = value if battery_level < 15: battery_critical = True if battery_status == 'Discharging': battery_status = '(bat)' else: battery_status = '(ac)' text = 'B: {}% {}'.format(battery_level, battery_status) return { 'full_text': text, 'name': 'bat', 'color': '#FF0000' if battery_critical else '#FFFFFF', } def print_line(message): sys.stdout.write(message + '\n') sys.stdout.flush() def read_line(): try: line = sys.stdin.readline().strip() if not line: sys.exit(3) return line except KeyboardInterrupt: sys.exit() def main(): # The first line is just a Header, with the version print_line(read_line()) # Then the second line is just "[" as it starts the infinate array. wat print_line(read_line()) while True: line, prefix = read_line(), '' if line.startswith(','): line, prefix = line[1:], ',' j = json.loads(line) j.insert(0, get_vpn()) j.insert(5, get_memory()) j.insert(5, get_battery()) print_line(prefix+json.dumps(j)) if __name__ == '__main__': main() |