ZSH & Python: python as shell environment variable?

3.6k Views Asked by At

I've been researching setting up ZSH. I've wiped my machine (Mac OS X), installed XCode (Python), ran chsh -s zsh, and installed oh-my-zsh.

The masses seem to rave about Steve Losh's "Prose" theme and his tutorial he wrote in making it. I've been able to incorporate everything relevant to me in it, except for the Battery Info piece. Here's the code:

"I’ve put this script in a file named batcharge.py in my ~/bin/ directory. You can of course name it and place it anything/anywhere you like."

batcharge.py

#!/usr/bin/env python
# coding=UTF-8

import math, subprocess

p = subprocess.Popen(["ioreg", "-rc", "AppleSmartBattery"], stdout=subprocess.PIPE)
output = p.communicate()[0]

o_max = [l for l in output.splitlines() if 'MaxCapacity' in l][0]
o_cur = [l for l in output.splitlines() if 'CurrentCapacity' in l][0]

b_max = float(o_max.rpartition('=')[-1].strip())
b_cur = float(o_cur.rpartition('=')[-1].strip())

charge = b_cur / b_max
charge_threshold = int(math.ceil(10 * charge))

# Output

total_slots, slots = 10, []
filled = int(math.ceil(charge_threshold * (total_slots / 10.0))) * u'▸'
empty = (total_slots - len(filled)) * u'▹'

out = (filled + empty).encode('utf-8')
import sys

color_green = '%{[32m%}'
color_yellow = '%{[1;33m%}'
color_red = '%{[31m%}'
color_reset = '%{[00m%}'
color_out = (
    color_green if len(filled) > 6
    else color_yellow if len(filled) > 4
    else color_red
)

out = color_out + out + color_reset
sys.stdout.write(out)

.zshrc

function battery_charge {
    echo `$BAT_CHARGE` 2>/dev/null
}

later in .zshrc

RPROMPT='$(battery_charge)'

I've done exactly this but the battery icon does not appear in Terminal / iTerm. The gaps in my understanding lie in the $BAT_CHARGE call and in the ~/bin/ placement. Steve specifies "You can of course name it and place it anything/anwhere you like..." If so, how does your shell know what value to assign $BAT_CHARGE if you place the python file in some random location?

Thanks for any advice,

JW

1

There are 1 best solutions below

8
On BEST ANSWER

Because earlier in the file you're supposed to assign the location of the script to the environment variable (or omit the environment variable altogether and use the script location directly).