【问题标题】:How to show battery status in Zsh prompt如何在 Zsh 提示符中显示电池状态
【发布时间】:2013-04-14 01:54:08
【问题描述】:

我认为答案是不言自明的。

我一直在寻找已经可以做到这一点的软件,但我没有任何运气。它要么不是在 Zsh 中完成,要么是用于另一个应用程序,例如 tmux。重点是我没找到。

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

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

这个想法是它输出类似于67% 的内容。你明白了。 ;)

【问题讨论】:

    标签: command-prompt zsh


    【解决方案1】:

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

    我使用了steve losh's 电池提示的基本概念,但首先将其更改为不使用 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%} "
        }
    

    【讨论】:

      【解决方案2】:

      自 2011 年以来,Zsh 有一个名为 battery 的内置插件,可以显示电池状态。要激活插件,请在您喜欢的文本编辑器中打开 ~/.zshrc 并将 battery 添加到插件设置中:

      plugins=(git battery)
      

      保存您的更改并重新加载您的 Zsh 会话:

      source ~/.zshrc
      

      列出的插件现在处于活动状态。

      【讨论】:

        【解决方案3】:

        这对我的 Mac 有帮助,

        pmset -g batt | grep -Eo "\d+%" | cut -d% -f1
        

        【讨论】:

          【解决方案4】:

          这是一个带有颜色并用 ZSH 编写的版本。

          在您的 .zshrc 文件中

          function battery {
              batprompt.sh
          }
          setopt promptsubst
          PROMPT='$(battery) >'
          

          这里有一个 batprompt.sh 的 linux 脚本。您可以适应其他读取电池数据的方式或适应早期帖子中的 acpi 版本:

          #!/bin/zsh
          # BATDIR is the folder with your battery characteristics
          BATDIR="/sys/class/power_supply/BAT0"
          max=`cat $BATDIR/charge_full`
          current=`cat $BATDIR/charge_now`
          percent=$(( 100 * $current / $max ))
          
          color_green="%{^[[32m%}"
          color_yellow="%{^[[34m%}"
          color_red="%{^[[31m%}"
          color_reset="%{^[[00m%}"
          
          if [ $percent -ge 80 ] ; then
              color=$color_green;
          elif [ $percent -ge 40 ] ; then
              color=$color_yellow;
          else
              color=$color_red;
          fi
          echo $color$percent$color_reset
          

          请注意,您在颜色定义中看到的 ^[ 是转义字符。如果剪切和粘贴不能正常工作,您需要将 ^[ 转换为转义字符。在 VI/Vim 中,您可以删除字符,然后插入 control-v,然后按 Escape 键。

          【讨论】:

          • 在 Korora (Fedora) 上它不起作用。它没有找到/sys/class/power_supply/BAT0/charge_full/sys/class/power_supply/BAT0/charge_now
          • 好吧...它在/sys/class/power_supply/BAT1/energy_full/sys/class/power_supply/BAT1/energy_now 中。谢谢!
          【解决方案5】:

          other answer 不适用于 Mac OS X(没有 apci)。

          我在提示符中使用了一些Steve Losh's zsh prompt。这并不完全符合您的要求 - 箭头 ▸▸▸▸▸▸▸▸▸▸ 显示当前电池状态,并在电池电量不足时更改颜色。此方法使用 Python 脚本,为了完整起见,我将在下面添加。这是我的提示的屏幕截图:

          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) $'
          

          看起来像这样:


          要使用 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)
          

          【讨论】:

          • 谢谢!虽然这不算数,因为我在 Linux 上并且这是针对 Mac 的,但这是一个经过深思熟虑并做出的答案。投票有用。 :)
          【解决方案6】:

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

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

          【讨论】:

          • 这不起作用:它会设置一次提示并完成。使用单引号和setopt promptsubst
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-20
          • 1970-01-01
          • 1970-01-01
          • 2014-08-04
          • 1970-01-01
          相关资源
          最近更新 更多