How to show battery status in Zsh request

I think the answer is quite explanatory.

I was looking for software that already does this, but I'm out of luck. This is either not done in Zsh, or for another application, for example tmux. Point: I could not find her.

So my question is: is there a ready-made script that someone did this? If so, could you share a link to it.

If this does not happen, what should I consider to make this a script? I am new to Zsh scripts, so keep that in mind.

The idea is that it outputs something along the lines 67%. You understand.;)

+5
source share
4 answers

Very, very simple solution:

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

Mac OS X ( apci).

Steve Losh zsh. , - ▸▸▸▸▸▸▸▸▸▸ , . Python script, . :

My zsh prompt

OS X Reddit, script:

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

, script ~/bin/battery.sh:

function battery {
    ~/bin/battery.sh
}
setopt promptsubst
PROMPT='$(battery) $'

:

Reddit-based battery charge.


Steve Losh script, script - battery() .

Python script OS X

#!/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)
+8

, , , :

steve losh, , python, shell, arch linux. , "+" , : This is what my battery status app looks like

:

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%} "
}
+5

ZSH.

.zshrc

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

- batprompt.sh script 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

We will warn that ^ [you see the Escape symbol in the color definitions. If cut-paste does not work properly, you will need to convert the ^ [character to Escape character. In VI / Vim, you can remove characters and then insert the -v control with the Escape key.

+2
source

All Articles