如何在Zsh提示符下显示电池状态

gre*_*uan 6 zsh command-prompt

我认为答案是非常自我解释的.

我一直在寻找已经做到这一点的软件,但我没有运气.它不是在Zsh中完成的,也不是在另一个应用程序中完成的,例如tmux.点是我无法找到它.

所以我的问题是,是否已经有一个预制的脚本,有人这样做了吗?如果有,请你分享一个链接.

如果没有,我应该考虑制作这个脚本?我是Zsh脚本的新手所以记住这一点.

这个想法是它输出一些东西67%.你明白了.;)

sim*_*ont 8

其他的答案无法在Mac OS X(没有工作apci).

在我的提示中,我已经采取了一些Steve Losh的zsh提示符.这并不完全是你所追求的 - 箭头??????????显示当前的电池状态,并在电池电量不足时改变颜色.此方法使用Python脚本,为了完整性,我将在下面添加.这是我的提示操作的屏幕截图:

我的zsh提示

OS X的另一种方法是在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)}'
Run Code Online (Sandbox Code Playgroud)

假设此脚本保存在~/bin/battery.sh:

function battery {
    ~/bin/battery.sh
}
setopt promptsubst
PROMPT='$(battery) $'
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

基于Reddit的电池充电.


要使用Steve Losh的脚本,请将脚本保存在某处,并在battery()上面的函数中使用它.

OS X的电池充电Python脚本

#!/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)
Run Code Online (Sandbox Code Playgroud)


uzs*_*olt 5

一个非常简单的解决方案:

setopt promptsubst
PROMPT='$(acpi | grep -o "[0-9]*%)% '
Run Code Online (Sandbox Code Playgroud)

  • 这不起作用:它将设置一次提示并完成.使用单引号和`setopt promptsubst`. (2认同)

小智 5

虽然这个帖子已经很老了,但我还是想在这里发布我的版本:

我使用了steve losh的电池提示的基本概念,但首先改变它不使用python而是shell,这更快,并且还改变了我的arch linux发行版的电池路径.另外,如果我的笔记本电脑正在充电,我会在末尾添加一个绿色的粗体'+': 这是我的提示与电池状态的关系

我的电池功能如下:

function battery_charge {
  b_now=$(cat /sys/class/power_supply/BAT1/energy_now)
  b_full=$(cat /sys/class/power_supply/BAT1/energy_full)
  b_status=$(cat /sys/class/power_supply/BAT1/status)
  # I am displaying 10 chars -> charge is in {0..9}
  charge=$(expr $(expr $b_now \* 10) / $b_full)

  # choose the color according the charge or if we are charging then always green
  if [[ charge -gt 5 || "Charging" == $b_status ]]; then
    echo -n "%{$fg[green]%}"
  elif [[ charge -gt 2 ]]; then
    echo -n "%{$fg[yellow]%}"
  else
    echo -n "%{$fg[red]%}"
  fi

  # display charge * '?' and (10 - charge) * '?'
  i=0;
  while [[ i -lt $charge ]]
  do
    i=$(expr $i + 1)
    echo -n "?"
  done
  while [[ i -lt 10 ]]
  do
    i=$(expr $i + 1)
    echo -n "?"
  done

  # display a plus if we are charging
  if [[ "Charging" == $b_status ]]; then
    echo -n "%{$fg_bold[green]%} +"
  fi
  # and reset the color
  echo -n "%{$reset_color%} "
}
Run Code Online (Sandbox Code Playgroud)