ZSH& Python:python作为shell环境变量?
我一直在研究设置 ZSH。我已经擦除了我的机器(Mac OS X),安装了 XCode(Python),运行了 chsh -s zsh,并安装了 oh-my-zsh。
大众似乎对史蒂夫·洛什的“散文”主题和他在创作过程中编写的教程赞不绝口。除了电池信息部分之外,我已经能够将与我相关的所有内容合并到其中。代码如下:
“我已将此脚本放入 ~/bin/
目录中名为 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
function battery_charge {
echo `$BAT_CHARGE` 2>/dev/null
}
稍后在 .zshrc
RPROMPT='$(battery_charge)'
我已经完成了这一点,但电池图标没有出现在终端/ iTerm 中。我理解的差距在于 $BAT_CHARGE
调用和 ~/bin/
放置。 Steve 指定“您当然可以命名它并将其放置在任何您喜欢的位置...”如果是这样,如果您将 python 文件放置在某个随机位置,您的 shell 如何知道为 $BAT_CHARGE 分配什么值?
感谢您的任何建议,
JW
I've been researching setting up ZSH. I've wiped my machine (Mac OS X), installed XCode (Python), ran chsh -s zsh, and installed oh-my-zsh.
The masses seem to rave about Steve Losh's "Prose" theme and his tutorial he wrote in making it. I've been able to incorporate everything relevant to me in it, except for the Battery Info piece. Here's the code:
"I’ve put this script in a file named batcharge.py
in my ~/bin/
directory. You can of course name it and place it anything/anywhere you like."
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
}
later in .zshrc
RPROMPT='$(battery_charge)'
I've done exactly this but the battery icon does not appear in Terminal / iTerm. The gaps in my understanding lie in the $BAT_CHARGE
call and in the ~/bin/
placement. Steve specifies "You can of course name it and place it anything/anwhere you like..." If so, how does your shell know what value to assign $BAT_CHARGE if you place the python file in some random location?
Thanks for any advice,
JW
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为在文件的前面,您应该将脚本的位置分配给环境变量(或者完全省略环境变量并直接使用脚本位置)。
Because earlier in the file you're supposed to assign the location of the script to the environment variable (or omit the environment variable altogether and use the script location directly).