HokieGeek / dotfiles

My linux config files
0 stars 0 forks source link

Create xmonad conky config on the fly #56

Closed HokieGeek closed 10 years ago

HokieGeek commented 10 years ago

Instead of calling conky with the xmonad.conkyrc config, call a python script that generates the file and then calls conky.

HokieGeek commented 10 years ago

https://github.com/HokieGeek/dotfiles/blob/master/conky/bin/statusbar.py

HokieGeek commented 10 years ago

This gathers the following issues:

HokieGeek commented 10 years ago

Some starting values

colorschemeHex = "9F0AC4"
imagesDir = "$HOME/.conky/imgs"
HokieGeek commented 10 years ago

Battery

f.write("^fg(\\#{})\\\n".format(colorschemeHex))
f.write("${if_match ${battery_percent} < 50}^fg(\\#FFCC00)${endif}\\\n")
f.write("${if_match ${battery_percent} < 20}^fg(\#FF0000)${endif}\\\n")
batterySteps = [100, 94, 88, 82, 75, 69, 63, 56, 50, 44, 38, 31, 25, 19, 12, 6]
for i in batterySteps:
    f.write("${{if_match ${{battery_percent} >= {}}}^i({}/battery_{}.xbm)${{else}}\\\n".format(i, imagesDir, i))
f.write("${{else}}^i({}/battery_0.xbm)\\\n".format(imagesDir))
for i in range(len(batterySteps)):
    f.write("${endif}")
f.write("\\\n")
HokieGeek commented 10 years ago

Starting values (updated)

conkyFile = tempfile.NamedTemporaryFile()
f = open(conkyFile, 'w')
colorschemeHex = "#9F0AC4"
imagesDir = "$HOME/.conky/imgs"
HokieGeek commented 10 years ago

Starting values (updated again)

conkyFile = tempfile.NamedTemporaryFile() f = open(conkyFile, 'w') colorschemeHex = "#9F0AC4" imagesDir = "$HOME/.conky/imgs"

HokieGeek commented 10 years ago

Starting values (updated again)

conkyFile = tempfile.NamedTemporaryFile()
f = open(conkyFile, 'w')
colorschemeHex = "#9F0AC4"
imagesDir = "$HOME/.conky/imgs"
HokieGeek commented 10 years ago

RAM

f.write("^fg(\#737373)^i({}/mem.xbm)^fg(\#FFFFFF) \\\n".format(imagesDir))
f.write("${if_match ${memperc} >= 85}^fg(\#FF0000)${endif}${memperc}%   \\\n")
f.write("^fg({})\\\n".format(colorschemeHex))

Time

f.write("${{time %a}}, ^fg({})${{time %d}}^fg(\#FFFFFF) ^fg(\#737373)${{time %b}}^fg(\#FFFFFF) \\\n".format(colorschemeHex))
f.write("^fg({})${{time %H%M}}^fg(\#FFFFFF) \\\n".format(colorschemeHex))
f.write("^fg(\#3A3A3A)(${uptime})^fg(\#FFFFFF)  \\\n")
HokieGeek commented 10 years ago

Media

f.write("${if_match ${texeci 1 amixer get Master | grep Mono: | grep -c "\[off\]"} >= 1}\\\n")
f.write("^i({}/spkr_02.xbm)\#^fg(\#3A3A3A) \\\n".format(imagesDir))
f.write("${else}\\\n")
f.write("^i({}/spkr_01.xbm)\#^fg(\#FFFFFF) \\\n".format(imagesDir))
f.write("${endif}\\\n")
f.write("${texeci 1 amixer get Master | tail -1 | cut -d'[' -f2 | cut -d']' -f1}  \\\n")
f.write("^fg(\#FFFFFF) \\\n")

TODO: Do I want this to be smarter?

HokieGeek commented 10 years ago

TODO: The last line of RAM needs to be the first line of Media!

HokieGeek commented 10 years ago

Putting it all together

#!/usr/bin/python
import subprocess
import tempfile
import os

## SETTING UP
conkyFile = tempfile.NamedTemporaryFile()
f = open(conkyFile, 'w')
colorschemeHex = "#9F0AC4" # TODO: read this from either a config file or command-line
imagesDir = "$HOME/.conky/imgs"

## NETWORK
# Retrieve interfaces
tempFile = tempfile.NamedTemporaryFile()
os.system("ip link show up | egrep '^[0-9]*:' | awk -F: '$2 !~ /lo/ { sub(\"^ *\", \"\", $2); print $2 }' > %s", tempFile.name)
interfaces = [line.strip() for line in tempFile]
tempFile.close()

# For each interface, generate conky output
for interface in interfaces:
    print("${if_up %s}\\", interface, end="")
    if interface[0] == "w":
        f.write("${{wireless_essid {}}} \\\n".format(interface))
        f.write("^fg({})\\\n".format(colorschemeHex))
        f.write("${{if_match ${{wireless_link_qual_perc {}}} >= 95}^i({}/wifi_100.xbm)${{else}}\\\n".format(interface, imagesDir))
        f.write("${{if_match ${{wireless_link_qual_perc {}}} >= 75}^i({}/wifi_75.xbm)${{else}}\\\n".format(interface, imagesDir))
        f.write("${{if_match ${{wireless_link_qual_perc {}}} >= 50}^i({}/wifi_50.xbm)${{else}}\\\n".format(interface, imagesDir))
        f.write("^i({}/wifi_25.xbm)\\\n".format(imagesDir))
        f.write("${endif}${endif}${endif}\\\n")
        f.write("^fg(\\#FFFFFF) \\\n")
    # elif interface[0] == "e":
        #TODO: f.write("^i({}/ethernet.xbm)\\\n".format(imagesDir))
        #TODO: f.write("^fg(\\#006400)^i(DOWN) ${{downspeed {}}}^fg(\\#FFFFFF) \\\n".format(interface))
        #TODO: f.write("^fg(\\#FF0000)^i(UP) ${{upspeed {}}}^fg(\\#FFFFFF) \\\n".format(interface))
    f.write("^fg(\\#606060)${{addr {}}}^fg(\\#FFFFFF) \\\n".format(interface)
    f.write("${endif}\\\n")

# Lastly, output the external IP
f.write("^fg(\\#606060) (${exec wget -q -O /dev/stdout http://checkip.dyndns.org/ | cut -d : -f 2- | cut -d \\< -f -1 | awk '{ print $1 }'})^fg(\\#FFFFFF)\\\n")

## CPU & RAM
# CPU
#^fg(\#606060)^i($HOME/.conky/imgs/cpu.xbm)^fg(\#FFFFFF) \
numCpus = subprocess.check_output("grep -c 'processor' /proc/cpuinfo", shell=True).strip().decode("utf-8")
for cpu in range(1,int(numCpus)+1):
    f.write("${{if_match ${{cpu cpu{}}} >= 85}}^fg(\\#FF0000)${{endif}}\\\n".format(cpu))
    f.write("${{if_match ${{cpu cpu{}}} < 10}} ${{endif}}${{cpu cpu{}}}%^fg(\\#FFFFFF) \\\n".format(cpu, cpu))
# Ram
f.write("^fg(\#737373)^i({}/mem.xbm)^fg(\#FFFFFF) \\\n".format(imagesDir))
f.write("${if_match ${memperc} >= 85}^fg(\#FF0000)${endif}${memperc}%   \\\n")

## MEDIA
f.write("^fg({})\\\n".format(colorschemeHex))
f.write("${if_match ${texeci 1 amixer get Master | grep Mono: | grep -c "\[off\]"} >= 1}\\\n")
f.write("^i({}/spkr_02.xbm)\#^fg(\#3A3A3A) \\\n".format(imagesDir))
f.write("${else}\\\n")
f.write("^i({}/spkr_01.xbm)\#^fg(\#FFFFFF) \\\n".format(imagesDir))
f.write("${endif}\\\n")
f.write("${texeci 1 amixer get Master | tail -1 | cut -d'[' -f2 | cut -d']' -f1}  \\\n")
f.write("^fg(\#FFFFFF) \\\n")

## TIME
f.write("${{time %a}}, ^fg({})${{time %d}}^fg(\#FFFFFF) ^fg(\#737373)${{time %b}}^fg(\#FFFFFF) \\\n".format(colorschemeHex))
f.write("^fg({})${{time %H%M}}^fg(\#FFFFFF) \\\n".format(colorschemeHex))
f.write("^fg(\#3A3A3A)(${uptime})^fg(\#FFFFFF)  \\\n")

## BATTERY
f.write("^fg({})\\\n".format(colorschemeHex))
f.write("${if_match ${battery_percent} < 50}^fg(\\#FFCC00)${endif}\\\n")
f.write("${if_match ${battery_percent} < 20}^fg(\\#FF0000)${endif}\\\n")
batterySteps = [100, 94, 88, 82, 75, 69, 63, 56, 50, 44, 38, 31, 25, 19, 12, 6]
for i in batterySteps:
    f.write("${{if_match ${{battery_percent} >= {}}}^i({}/battery_{}.xbm)${{else}}\\\n".format(i, imagesDir, i))
f.write("${{else}}^i({}/battery_0.xbm)\\\n".format(imagesDir))
for i in range(len(batterySteps)):
    f.write("${endif}")
f.write("\\\n")

## CALL CONKY
# TODO: cmd = "conky -b -c {}".format(conkyFile)
HokieGeek commented 10 years ago

Changes to xmonad.hs

conkyStatusBar = "~/.conky/bin/statusline.py | dzen2 -y '0' -x '2732' -w '1366' -h '16' -ta 'r' -bg '#1B1D1E' -fg '#FFFFFF' -fn '-*-terminus-medium-r-*-*-12-*-*-*-*-*-*-*'"

On another note, maybe look into replacing

dzenRightBar <- spawnPipe conkyStatusBar

with

dzenRightBar <- spawn conkyStatusBar
HokieGeek commented 10 years ago

Conky call

conky = subprocess.Popen("conky -b -c {}".format(conkyFile), stdout=subprocess.PIPE, shell=True)
for out in conky:
    print(out, end="")
f.close()

TODO: would probably be a good idea to figure out if I setup traps to clean up that file correctly


Fixes

Network

For each interface, generate conky output

for interface in interfaces:

    f.write("${{if_up {}}}\\\n".format(interface))
if interface[0] == "w":
CPU

CPU

f.write("^fg(\\#606060)^i({}/cpu.xbm)^fg(\\#FFFFFF) \\\n".format(imagesDir))

numCpus = subprocess.check_output("grep -c 'processor' /proc/cpuinfo", shell=True).strip().decode("utf-8")

Ram
# Ram
f.write("^fg(\\#737373)^i({}/mem.xbm)^fg(\\#FFFFFF) \\\n".format(imagesDir))
f.write("${if_match ${memperc} >= 85}^fg(\\#FF0000)${endif}${memperc}%   \\\n")
HokieGeek commented 10 years ago

Properly handle cleaning the file

import signal

def cleanup():
    f.close()
    os.remove(conkyFile) # TODO: is this pointless?

def handleSigTERM():
    cleanup()

signal.signal(signal.SIGTERM, handleSigTERM) 

Replace f.close() post conky call above with cleanup()

HokieGeek commented 10 years ago

ANOTHER FREAKING UPDATE

#!/usr/bin/python
import subprocess
import tempfile
import os
import signal

## Setup cleanup
def cleanup():
    f.close()
    os.remove(conkyFile) # TODO: is this pointless?

def handleSigTERM():
    cleanup()

signal.signal(signal.SIGTERM, handleSigTERM) 

## Variables
conkyFile = tempfile.NamedTemporaryFile()
f = open(conkyFile, 'w')
colorschemeHex = "#9F0AC4" # TODO: read this from either a config file or command-line
imagesDir = "$HOME/.conky/imgs"

## NETWORK
# Retrieve interfaces
tempFile = tempfile.NamedTemporaryFile()
os.system("ip link show up | egrep '^[0-9]*:' | awk -F: '$2 !~ /lo/ { sub(\"^ *\", \"\", $2); print $2 }' > %s", tempFile.name)
interfaces = [line.strip() for line in tempFile]
tempFile.close()

# For each interface, generate conky output
for interface in interfaces:
    f.write("${{if_up {}}}\\\n".format(interface))
    if interface[0] == "w":
        f.write("${{wireless_essid {}}} \\\n".format(interface))
        f.write("^fg({})\\\n".format(colorschemeHex))
        f.write("${{if_match ${{wireless_link_qual_perc {}}} >= 95}^i({}/wifi_100.xbm)${{else}}\\\n".format(interface, imagesDir))
        f.write("${{if_match ${{wireless_link_qual_perc {}}} >= 75}^i({}/wifi_75.xbm)${{else}}\\\n".format(interface, imagesDir))
        f.write("${{if_match ${{wireless_link_qual_perc {}}} >= 50}^i({}/wifi_50.xbm)${{else}}\\\n".format(interface, imagesDir))
        f.write("^i({}/wifi_25.xbm)\\\n".format(imagesDir))
        f.write("${endif}${endif}${endif}\\\n")
        f.write("^fg(\\#FFFFFF) \\\n")
    # elif interface[0] == "e":
        #TODO: f.write("^i({}/ethernet.xbm)\\\n".format(imagesDir))
        #TODO: f.write("^fg(\\#006400)^i(DOWN) ${{downspeed {}}}^fg(\\#FFFFFF) \\\n".format(interface))
        #TODO: f.write("^fg(\\#FF0000)^i(UP) ${{upspeed {}}}^fg(\\#FFFFFF) \\\n".format(interface))
    f.write("^fg(\\#606060)${{addr {}}}^fg(\\#FFFFFF) \\\n".format(interface)
    f.write("${endif}\\\n")

# Lastly, output the external IP
f.write("^fg(\\#606060) (${exec wget -q -O /dev/stdout http://checkip.dyndns.org/ | cut -d : -f 2- | cut -d \\< -f -1 | awk '{ print $1 }'})^fg(\\#FFFFFF)\\\n")

## CPU & RAM
# CPU
f.write("^fg(\\#606060)^i({}/cpu.xbm)^fg(\\#FFFFFF) \\\n".format(imagesDir))
numCpus = subprocess.check_output("grep -c 'processor' /proc/cpuinfo", shell=True).strip().decode("utf-8")
for cpu in range(1,int(numCpus)+1):
    f.write("${{if_match ${{cpu cpu{}}} >= 85}}^fg(\\#FF0000)${{endif}}\\\n".format(cpu))
    f.write("${{if_match ${{cpu cpu{}}} < 10}} ${{endif}}${{cpu cpu{}}}%^fg(\\#FFFFFF) \\\n".format(cpu, cpu))
# Ram
f.write("^fg(\\#737373)^i({}/mem.xbm)^fg(\\#FFFFFF) \\\n".format(imagesDir))
f.write("${if_match ${memperc} >= 85}^fg(\\#FF0000)${endif}${memperc}%   \\\n")

## MEDIA
f.write("^fg({})\\\n".format(colorschemeHex))
f.write("${if_match ${texeci 1 amixer get Master | grep Mono: | grep -c "\[off\]"} >= 1}\\\n")
f.write("^i({}/spkr_02.xbm)\#^fg(\#3A3A3A) \\\n".format(imagesDir))
f.write("${else}\\\n")
f.write("^i({}/spkr_01.xbm)\#^fg(\#FFFFFF) \\\n".format(imagesDir))
f.write("${endif}\\\n")
f.write("${texeci 1 amixer get Master | tail -1 | cut -d'[' -f2 | cut -d']' -f1}  \\\n")
f.write("^fg(\#FFFFFF) \\\n")

## TIME
f.write("${{time %a}}, ^fg({})${{time %d}}^fg(\#FFFFFF) ^fg(\#737373)${{time %b}}^fg(\#FFFFFF) \\\n".format(colorschemeHex))
f.write("^fg({})${{time %H%M}}^fg(\#FFFFFF) \\\n".format(colorschemeHex))
f.write("^fg(\#3A3A3A)(${uptime})^fg(\#FFFFFF)  \\\n")

## BATTERY
f.write("^fg({})\\\n".format(colorschemeHex))
f.write("${if_match ${battery_percent} < 50}^fg(\\#FFCC00)${endif}\\\n")
f.write("${if_match ${battery_percent} < 20}^fg(\\#FF0000)${endif}\\\n")
batterySteps = [100, 94, 88, 82, 75, 69, 63, 56, 50, 44, 38, 31, 25, 19, 12, 6]
for i in batterySteps:
    f.write("${{if_match ${{battery_percent} >= {}}}^i({}/battery_{}.xbm)${{else}}\\\n".format(i, imagesDir, i))
f.write("${{else}}^i({}/battery_0.xbm)\\\n".format(imagesDir))
for i in range(len(batterySteps)):
    f.write("${endif}")
f.write("\\\n")

## CALL CONKY
conky = subprocess.Popen("conky -b -c {}".format(conkyFile), stdout=subprocess.PIPE, shell=True)
for out in conky:
    print(out, end="")
cleanup()
HokieGeek commented 10 years ago

Almost forgot the conky options!

## CONKY SETTINGS
f.write("background yes\n")
f.write("out_to_console yes\n")
f.write("out_to_x no\n")
f.write("# Update interval in seconds\n")
f.write("update_interval 1\n")
HokieGeek commented 10 years ago

And this:

f.write("TEXT\n")
HokieGeek commented 10 years ago

Add a space between the interfaces and cpu:

f.write("    \\n")
HokieGeek commented 10 years ago

w00t!