0

我一直在研究设置 ZSH。我已经擦除了我的机器(Mac OS X),安装了 XCode(Python),运行了 chsh -s zsh,并安装了 oh-my-zsh。

大众似乎对 Steve Losh 的“散文”主题以及他在制作过程中编写的教程赞不绝口。我已经能够在其中包含与我相关的所有内容,除了电池信息部分。这是代码:

“我已将此脚本放在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)'

我已经做到了这一点,但终端/iTerm 中没有出现电池图标。我理解的差距在于$BAT_CHARGE电话和~/bin/位置。史蒂夫指定“你当然可以命名它并将它放在任何你喜欢的地方/任何地方......”如果是这样,如果你将python文件放在某个随机位置,你的shell如何知道分配什么值 $BAT_CHARGE ?

感谢您的任何建议,

JW

4

1 回答 1

1

因为在文件的前面,您应该将脚本的位置分配给环境变量(或完全省略环境变量并直接使用脚本位置)。

于 2011-02-08T10:04:58.180 回答