【发布时间】:2019-12-18 06:43:03
【问题描述】:
您好,我正在尝试使用 python 脚本在我的 zsh 提示符上显示我的笔记本电脑的电池状态。 我正在关注this。
我在~/bin/ 中创建了batcharge.py 脚本,并在逗号行中创建了chmod 755 batcharge.py。
这是 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 脚本。
BAT_CHARGE=~/bin/batcharge.py
function battery_charge {
echo `$BAT_CHARGE` 2>/dev/null
}
set_prompt(){
NEWLINE=$'\n'
PROMPT="%F{202}%n%f at %F{136}%m%f in %F{green}%1~%f${NEWLINE}%# "
RPROMPT='$(battery_charge)'
}
autoload add-zsh-hook
add-zsh-hook precmd set_prompt
然后我重新启动了我的 shell,但是它一直显示 '% $(battery_charge)
我哪里做错了?
【问题讨论】:
-
如果你手动运行
~/bin/batcharge.py,这行得通吗? -
谢谢你的评论!