ZSH与Python:如何将Python作为环境变量?
我最近在研究如何设置 ZSH。我的电脑(Mac OS X)已经重装了系统,安装了 XCode(Python),运行了 chsh -s zsh 命令,并且安装了 oh-my-zsh。
大家似乎都很喜欢 Steve Losh 的 "Prose" 主题,还有他写的教程。我已经把与我相关的内容都整合进去了,除了电池信息的部分。以下是代码:
“我把这个脚本放在了一个名为 batcharge.py
的文件里,放在我的 ~/bin/
目录下。当然,你可以随意命名和放置它。”
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
}
在 .zshrc
文件的后面部分
RPROMPT='$(battery_charge)'
我确实按照这个方法做了,但电池图标在 Terminal / iTerm 中并没有出现。我不太明白的地方在于 $BAT_CHARGE
的调用和 ~/bin/
的位置。Steve 指出“你当然可以随意命名和放置它……”那么,如果你把这个 Python 文件放在一个随机的位置,终端是怎么知道给 $BAT_CHARGE
赋什么值的呢?
谢谢你的建议,
JW
1 个回答
1
因为在文件的前面,你应该把脚本的位置赋值给环境变量(或者干脆不使用环境变量,直接用脚本的位置)。