cleaned up some code and reduced calls to upower
This commit is contained in:
		
							parent
							
								
									06434a4935
								
							
						
					
					
						commit
						e75250ac9f
					
				@ -22,7 +22,7 @@ MOUSE_ICON=$(echo -e '\Uf098b') # bt mouse
 | 
			
		||||
HEADSET_ICON=$(echo -e '\Uf0970')
 | 
			
		||||
 | 
			
		||||
battery_icon() {
 | 
			
		||||
    # grabs corresponding battery level icon
 | 
			
		||||
    # grabs corresponding battery level icon; state in 2, level in 1
 | 
			
		||||
    if [ "$2" == "charging" ] ; then
 | 
			
		||||
        # mouse is charging, battery icon represents
 | 
			
		||||
        if [ $1 -gt 95 ] ; then
 | 
			
		||||
@ -87,46 +87,20 @@ identifier() {
 | 
			
		||||
    # returns device name or icon based on options; dev in $1
 | 
			
		||||
    
 | 
			
		||||
    if [ -n "$DEV_ICON" ] ; then
 | 
			
		||||
        if [ -n "$(upower -i "$1" | grep 'mouse')" ] ; then
 | 
			
		||||
            printf '%s ' "$MOUSE_ICON"
 | 
			
		||||
        fi
 | 
			
		||||
 | 
			
		||||
        if [ -n "$(upower -i "$1" | grep 'headset')" ] ; then
 | 
			
		||||
            printf '%s ' "$HEADSET_ICON"
 | 
			
		||||
        fi
 | 
			
		||||
        [[ "$1" =~ [Mm]ouse ]] && echo "$MOUSE_ICON"
 | 
			
		||||
        [[ "$1" =~ [Hh]eadset ]] && echo "$HEADSET_ICON"
 | 
			
		||||
    else
 | 
			
		||||
        printf '%s' "$(upower -i "$1" | grep 'model:' | awk '{print $2}')"
 | 
			
		||||
        echo "$1" | grep 'model:' | awk '{print $2}'
 | 
			
		||||
    fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
battery() {
 | 
			
		||||
    # gets device battery level; dev in $1
 | 
			
		||||
 | 
			
		||||
    level=$(upower -i "$1" | grep 'percentage:' | awk '{print $2}' | tr -d '%')
 | 
			
		||||
    state=$(upower -i "$1" | grep 'state:' | awk '{print $2}')
 | 
			
		||||
 | 
			
		||||
    if [ "$state" == "unknown" ] ; then
 | 
			
		||||
        # override to always show if device asleep
 | 
			
		||||
        echo -e '\Uf04b2 ' && exit 0
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    color "$state" $level
 | 
			
		||||
 | 
			
		||||
    if [ -n "$BATTERY_ICON" ] ; then
 | 
			
		||||
        battery_icon $level "$state"
 | 
			
		||||
    else
 | 
			
		||||
        printf '%d%%' $level
 | 
			
		||||
    fi
 | 
			
		||||
}    
 | 
			
		||||
 | 
			
		||||
color() {
 | 
			
		||||
    # sets battery color based on passed params
 | 
			
		||||
    if [ -n "$COLOR" ] ; then
 | 
			
		||||
        if [ "$1" == "charging" ] ; then
 | 
			
		||||
            echo "$CHARGING_COLOR"
 | 
			
		||||
        elif [ $2 -le $LOW_BATTERY_THRESHOLD ] ; then
 | 
			
		||||
            echo "$LOW_BATTERY_COLOR"
 | 
			
		||||
        fi
 | 
			
		||||
    # sets battery color; level in $1, state in $2
 | 
			
		||||
    if [ "$2" == "charging" ] ; then
 | 
			
		||||
        echo "$CHARGING_COLOR"
 | 
			
		||||
    elif [ $1 -le $LOW_BATTERY_THRESHOLD ] ; then
 | 
			
		||||
        echo "$LOW_BATTERY_COLOR"
 | 
			
		||||
    fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -173,14 +147,36 @@ while getopts "hcidb" opt ; do
 | 
			
		||||
    esac
 | 
			
		||||
done
 | 
			
		||||
 | 
			
		||||
# go through devs
 | 
			
		||||
devs=$(upower -e | grep -v "DisplayDevice") # don't use the monitor
 | 
			
		||||
 | 
			
		||||
for dev in $devs; do
 | 
			
		||||
for dev in `upower -e | grep -v 'DisplayDevice'`; do
 | 
			
		||||
    # allows proper spacing
 | 
			
		||||
    devs+=1
 | 
			
		||||
    # getting device info
 | 
			
		||||
    dev_id=$(identifier "$dev")
 | 
			
		||||
    dev_battery=$(battery "$dev")
 | 
			
		||||
    info=$(upower -i "$dev")
 | 
			
		||||
    state=$(echo "$info" | awk '{if($1 == "state:"){print $2}}')
 | 
			
		||||
 | 
			
		||||
    printf ' %s %s' "$dev_id" "$dev_battery"
 | 
			
		||||
    dev_id=$(identifier "$info")
 | 
			
		||||
    dev_battery=`echo "$info" | awk '{if($1 == "percentage:"){print $2}}'`
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    if [ -n "$COLOR" ] ; then
 | 
			
		||||
        clr=$(color `echo $dev_battery | tr -d '%'` "$state")
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    if [ -n "$BATTERY_ICON" ] ; then
 | 
			
		||||
        dev_battery=$(battery_icon `echo $dev_battery | tr -d '%'` "$state")
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    if [ "$state" == "unknown" ] ; then
 | 
			
		||||
        # override to always show if device asleep
 | 
			
		||||
        if [ -n "$DEV_ICON" ] ; then
 | 
			
		||||
            printf '%s %s' "$dev_id" `echo -e '\Uf04b2 '` && continue
 | 
			
		||||
        else
 | 
			
		||||
            printf '%s %s' "$dev_id" '?' && continue
 | 
			
		||||
        fi
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    # seperator
 | 
			
		||||
    [ $devs -gt 1 ] & printf ' '
 | 
			
		||||
 | 
			
		||||
    printf '%s %s%s' "$dev_id" "$clr" "$dev_battery"
 | 
			
		||||
done
 | 
			
		||||
echo
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user