我一直在研究设置ZSH。我擦了我的机器(Mac OS X),安装了XCode(Python),运行了chsh -s zsh,并安装了oh-my-zsh。
群众似乎对史蒂夫·洛什的“散文”主题以及他撰写的这篇教程赞不绝口。除了电池信息之外,我已经能够将与我相关的所有内容合并到其中。这是代码:
“我已将此脚本放在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/
展示位置。史蒂夫指出“你当然可以命名它,把它放在任何你喜欢的地方......”如果是这样的话,如果你将python文件放在一个随机位置,你的shell如何知道分配$ BAT_CHARGE的值是什么?
感谢您的任何建议,
JW
答案 0 :(得分:1)
因为在文件的前面你应该将脚本的位置分配给环境变量(或者完全省略环境变量并直接使用脚本位置)。