其他答案 在Mac OS X上无法运行(缺少apci
)。
我已经在我的提示符中使用了Steve Losh的zsh提示符的部分内容。 它不完全是您想要的 - 箭头▸▸▸▸▸▸▸▸▸▸
显示当前电池状态,并在电池电量低时更改颜色。 这种方法使用Python脚本,为了完整起见,我将在下面添加。 这是我提示符运行的屏幕截图:
![My zsh prompt](https://istack.dev59.com/KyhOl.webp)
在 OS X 上,另一种方法是使用另一个简短的脚本on Reddit:
ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%.2f%%"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}'
ioreg -n AppleSmartBattery -r | awk '$1~/ExternalConnected/{gsub("Yes", "+");gsub("No", "%"); print substr($0, length, 1)}'
假设这个脚本保存在
~/bin/battery.sh
中:
function battery {
~/bin/battery.sh
}
setopt promptsubst
PROMPT='$(battery) $'
看起来像这样:
![Reddit-based battery charge.](https://istack.dev59.com/cdLl4.webp)
要使用Steve Losh的脚本,请先保存脚本,然后在上面的
battery()
函数中使用。
供OS X使用的电池充电Python脚本
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))
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)