bat shit

Submitted scripts and programs
Forum rules
Your own work only.
User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

bat shit

Unread post by wuxmedia » Tue Feb 03, 2015 8:34 pm

So -
  • Bash was annoying me, time to fuck it up a bit.
  • Wanted to see if bat was charging or not as well as capacity.
  • Bored.

Code: Select all

#!/bin/bash

# bat.sh
# Queries /sys/class/power_supply/BATn/ things
# and makes it a pretty ascii output.

if [ -d /sys/class/power_supply/BAT0/ ]
then 
DIR=/sys/class/power_supply/BAT0/
else
DIR=/sys/class/power_supply/BAT1
fi

CAP=$(cat $DIR/capacity)
STATE=$(cat $DIR/status)
# Charging or What??
if [[ $STATE  == D* ]]
then ST="-"
else ST="+"
fi

if [[ $CAP == 100 ]]
then ST=""
fi
	echo {$CAP$ST]
- Of course the formatting is in this last line.
very handy piped to i3 bar thing.
Which - for my ref, looks like this:

Code: Select all

#!/bin/sh
# shell script to prepend i3status with more stuff

i3status | while :
 do
  read line
  echo "$(mocp -Q %title) | $( ~/bin/./bat.sh) | $line" || exit 1
 done
outputs something like:

Code: Select all

{98+]
and well, when it's 100% it's full, so shows neither charge or discharge. Which while absolutely floating my boat (as the char width remains the same, how sad am I?) User - as always can season as they wish. prolly pipe it to conky if wished ;)
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: bat shit

Unread post by dkeg » Tue Feb 03, 2015 9:10 pm

Nice. I have the same in my bar script. Very cool that you and I have the same output with a slightly different script. You were very complete by adding the checks. Nice job. I do a double ++ when full. My rendition is sourced in my latest scrot. Here's the relevant piece

Code: Select all

batt() {
     level=$(cat /sys/class/power_supply/BAT0/capacity 2>/dev/null)
     if [ $level -lt '25' ]; then
         echo %{+u}$red$level%{-u}
     else
         echo $green$level
     fi
       }

battstate() {
    state=$(cat /sys/class/power_supply/BAT0/status)
    case $state in 
        "Charging")
            echo $green2+ ;; 
        "Discharging")
            echo $red- ;;
        "Full")
            echo $green2++$none ;;
    esac
        }

Work hard; Complain less

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: bat shit

Unread post by wuxmedia » Tue Feb 03, 2015 9:19 pm

ugh, /me is a Johnny come lately uh-gain :(

I like the case, much less obfuscated than the "D*" test.
I think that was the tldp.org "test" page when I googled that test idea.
Err. where do you put your functions?

Nice idea with the colours, not sure if i3bar will let me.
Are you still rocking your "plugin or die fuckker" dunst script? was thinking of adapting that to go with this.
It is rare that I miss the fact it's charging or not.

Hell, we'll end up with that xfce powerman icons soon, or maybe not :)
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: bat shit

Unread post by dkeg » Tue Feb 03, 2015 9:49 pm

Its part of my status bar script. Yeah still have a separate notification script for that. Use bar instead of dunst.

Work hard; Complain less

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: bat shit

Unread post by wuxmedia » Tue Feb 03, 2015 10:02 pm

gotcha.
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: bat shit

Unread post by rhowaldt » Tue Feb 03, 2015 10:09 pm

no matter whether 2 men invent the wheel separately; there's still a wheel.
cool script Wux, nice and simple. Drew's looks good as well, but i personally prefer the text-only approach. seeing how Drew is our colourguy, it makes total sense for him to use colours :)

thanks for sharing!
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: bat shit

Unread post by wuxmedia » Tue Feb 03, 2015 10:32 pm

Only because you mention it and I've been listening to THHG2G all weekend, or anytime in the car:
Not any kind of comment, just thought it's funny, and if you haven't seen the whole series, you must:
http://youtu.be/zBmdkhDGZ8A?t=25m33s
shared from appropriate time index :)
Share and Enjoy :)
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: bat shit

Unread post by rhowaldt » Tue Feb 03, 2015 11:09 pm

^ cool, i needed that :) thanks!
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: bat shit

Unread post by stark » Thu Feb 05, 2015 9:12 am

Thanks wux and dkeg! Boy I am stupid for using the plain version with pidsley's color-echo method.
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: bat shit

Unread post by dkeg » Thu Feb 05, 2015 12:25 pm

well, displaying info; its just a matter of
1. locate
2. capture
2. filter [sed,awk,cut]
4. dispaly [bar,conky,osd,tmux,etc]
5. bloat it up (optional)

Work hard; Complain less

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: bat shit

Unread post by rhowaldt » Thu Feb 05, 2015 1:38 pm

^ that seems completely rational to me. some people might call this "obvious", but i always like it when somebody analyses obvious things, lest we forget how they work and take them for granted. thanks!
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: bat shit

Unread post by dkeg » Thu Feb 05, 2015 2:02 pm

Cool thanks. And +1 for using 'lest' .

Work hard; Complain less

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: bat shit

Unread post by wuxmedia » Thu Feb 05, 2015 2:52 pm

before, I was using acpi -b (manipulate) then I remembered it's all in /sys/blah/
all sorts of fun things in files linux.

Code: Select all

cat /proc/loadavg 
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: bat shit

Unread post by stark » Thu Feb 05, 2015 4:46 pm

^ whats the case for bsdland ?
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: bat shit

Unread post by wuxmedia » Thu Feb 05, 2015 5:29 pm

Too insecure for BSD :p
Bones might know, if google doesn't?
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: bat shit

Unread post by stark » Thu Feb 05, 2015 6:03 pm

I have the temptation to install OpenBSD in the *cough* #! *cough* partition as I don't boot into it that much but I have no idea if the Bsd bootloaders are multi-boot friendly and can I mentally handle the constant switch between GNU and BSD userland ?
Last edited by stark on Thu Feb 05, 2015 6:10 pm, edited 1 time in total.
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: bat shit

Unread post by pidsley » Thu Feb 05, 2015 6:07 pm

stark wrote:I'm have the temptation to install OpenBSD in the *cough* #! *cough* partition as I don't boot into it that much but I have no idea if the Bsd bootloaders are multi-bootfriendly and can I mentally handle the constant switch between GNU and BSD userland ?
Please don't try this. BSDs are not friendly about sharing a disk with other operating systems. I'm sure it can be done, but I don't think it's easy.

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: bat shit

Unread post by stark » Thu Feb 05, 2015 6:13 pm

^ Thanks pidsley! Seriously Noted.
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: bat shit

Unread post by pidsley » Thu Feb 05, 2015 6:21 pm

^ bones may have a different opinion, but every time I use a BSD I install it on a test machine and give it the entire disk. It just makes my life easier.

And yes, at least for me, the switch between Linux userland and BSD can be a bit challenging. I'm sure I could get used to BSD if I used it all the time (and I have considered it) but it's confusing at first.

User avatar
bones
Clooney
Posts: 2385
Joined: Fri Jun 28, 2013 11:47 pm
Location: Cascadia

Re: bat shit

Unread post by bones » Thu Feb 05, 2015 6:41 pm

^ Agreed, I only ever give it the entire disk. Haven't even attempted otherwise.

As for battery monitoring, there's options. I often go the lazy route and just use xfce-battery (NOT xfce-powermanager). acpi works, too. And there's stuff like this, that I haven't tried:

http://www.nongnu.org/gap/batmon/

EDIT: Oh yes, forgot I wrote a tutorial for myself on this:

Battery monitoring in OpenBSD:

Get a list of sensors:

Code: Select all

user@openbsd:~$ sysctl hw.sensors

Code: Select all

hw.sensors.acpibat0.watthour0=4.35 Wh (last full capacity)
hw.sensors.acpibat0.watthour1=0.43 Wh (warning capacity)
hw.sensors.acpibat0.watthour2=0.00 Wh (low capacity)
hw.sensors.acpibat0.watthour3=3.24 Wh (remaining capacity), OK
hw.sensors.acpibat0.raw0=1 (battery discharging), OK
hw.sensors.acpibat0.raw1=1957 (rate)
Configure /etc/sensorsd.conf similar to:

Code: Select all

# alert if remaining capacity < warning capacity
hw.sensors.acpibat0.watthour3:low=0.43Wh:command=/usr/X11R6/bin/xmessage "battery is low"
Start/restart sensorsd via sudo /usr/sbin/sensorsd or sudo pkill -HUP sensorsd

Code: Select all

Set sensorsd_flags="" in /etc/rc.conf.local to stick around on boot.
If remaining capacity goes below the warning capacity, a xmessage pop-up will interrupt you.

Post Reply