Skip to content

Instantly share code, notes, and snippets.

@lukas2511
Created June 15, 2013 17:45
Show Gist options
  • Save lukas2511/5788910 to your computer and use it in GitHub Desktop.
Save lukas2511/5788910 to your computer and use it in GitHub Desktop.
Just a simple script to be used with i3bar
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from mpd import (MPDClient, CommandError)
import sys
import os
import pytz
import socket
import fcntl
import struct
import time
from datetime import datetime
def rprint(text):
sys.stdout.write(text)
def battery_state():
now=float(open("/sys/bus/acpi/drivers/battery/PNP0C0A:00/power_supply/BAT0/energy_now").read().rstrip())
full=float(open("/sys/bus/acpi/drivers/battery/PNP0C0A:00/power_supply/BAT0/energy_full").read().rstrip())
percent=now/full*100.0
if percent < 20:
color='#FF0000'
elif percent < 50:
color='#FFA500'
else:
color='#A0A0A0'
battery_bars = ""
for state in (10, 20, 30, 40, 50, 60, 70, 80, 90, 100):
if percent >= state:
battery_bars = "%s■" % battery_bars
else:
battery_bars = "%s□" % battery_bars
rprint("""{\n\t\t"name": "battery",\n\t\t"color": "%s",\n\t\t"full_text": "%s (%.0f%%)"\n\t}""" % (color,battery_bars,percent))
def disk_info():
color='#A0A0A0'
s = os.statvfs('/home')
ges = s.f_bfree*s.f_bsize/1073741824.0
free = s.f_bavail*s.f_bsize/1073741824.0
percent = free/ges*100.0
rprint("""{\n\t\t"name": "disk_info",\n\t\t"color": "%s",\n\t\t"full_text": "%.1f GB"\n\t}""" % (color,free))
def current_time():
now = datetime.now(pytz.timezone('Europe/Berlin'))
rprint("""{\n\t\t"name": "tztime",\n\t\t"color": "#A0A0A0",\n\t\t"full_text": "%s"\n\t}""" % now.strftime("%d.%m.%Y %H:%M:%S"))
def load_avg():
loadavg = os.getloadavg()[1]
if loadavg < 2:
color = '#A0A0A0'
elif loadavg < 5:
color = '#FFA500'
else:
color = '#FF0000'
rprint("""{\n\t\t"name": "load",\n\t\t"color": "%s",\n\t\t"full_text": "%.2f"\n\t}""" % (color,loadavg))
def cpu_temperature():
temp = int(open("/sys/devices/virtual/thermal/thermal_zone0/temp").read().rstrip())/1000
if temp < 50:
color = '#A0A0A0'
elif temp < 70:
color = '#FFA500'
else:
color = '#FF0000'
rprint("""{\n\t\t"name": "cpu_temperature",\n\t\t"color": "%s",\n\t\t"full_text": "T: %d °C"\n\t}""" % (color,temp))
def mpd_status():
global client
currentsong = client.currentsong()
title = currentsong['title']
artist = currentsong['artist']
status = client.status()['time'].split(':')
length = time.strftime('%M:%S', time.gmtime(int(status[1])))
elapsed = time.strftime('%M:%S', time.gmtime(int(status[0])))
rprint("""{\n\t\t"name": "mpd",\n\t\t"color": "#A0A0A0",\n\t\t"full_text": "MPD: %s - %s (%s / %s)"\n\t}""" % (artist,title,elapsed,length))
client = MPDClient()
client.connect(host='127.0.0.1', port=6600)
rprint('{"version":1}\n')
rprint('[\n')
def status_bar():
rprint("\t[\n\t")
if client.status()['state'] == 'play':
mpd_status();
rprint(",\n\t")
disk_info();
rprint(",\n\t")
load_avg()
rprint(",\n\t")
cpu_temperature()
rprint(",\n\t")
battery_state()
rprint(",\n\t")
current_time()
rprint("\n\t]")
sys.stdout.flush()
status_bar()
while True:
time.sleep(1)
rprint(",\n")
status_bar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment