Skip to content

Commit

Permalink
Dynamic Icon Set Update.
Browse files Browse the repository at this point in the history
Added in some easy code to dynamically pick the icon set based on a command-line argument. 

(AWP made some automated changes to the files, so committing them as is).
  • Loading branch information
adwaraki committed May 6, 2022
1 parent 954adba commit 3b8a228
Show file tree
Hide file tree
Showing 15 changed files with 6,259 additions and 20 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Battery

> For Apple M1 chip users: https://github.com/BenziAhamed/alfred-battery/issues/11#issue-812562822
![Logo](info.png)

An Alfred workflow to display battery information of your Apple laptop.

Is activated by the keyword "battery". The new gradient icon set is the default, but if
you like the old set better, just use the optional keyword "reg", like thus: "battery reg"
and it will revert to the old set.

Gradient Icons:
Thanks to their respective authors on FlatIcon.

Expand All @@ -21,3 +23,4 @@ Clock icons created by redempticon
Mouse icons created by Ilham Fitrotul Hayat
Panel icons created by Pixel perfect
Computer peripheral icons created by lakonicon
Macbook icons created by bqlqn
42 changes: 24 additions & 18 deletions src/battery.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#1/usr/bin/env bash
#!/bin/bash

ioreg -l -n AppleSmartBattery -r > info.txt

if [ "$1" = "reg" ]; then
ICON_SET="icons"
else
ICON_SET="gradient-icons"
fi

RAW_CURRENT_CAPACITY=$(cat info.txt | grep -e \"AppleRawCurrentCapacity\" | awk '{printf ("%i", $3)}')
RAW_MAX_CAPACITY=$(cat info.txt | grep -e \"AppleRawMaxCapacity\" | awk '{printf ("%i", $3)}')
# MAX_CAPACITY=$(cat info.txt | grep -e \"MaxCapacity\" | | awk -F',' '{printf ("%s", $45)}' | awk -F'=' '{printf ("%i", $2)}')
Expand All @@ -28,34 +34,34 @@ fi

TIME_INFO=n
STATUS_INFO=Draining
BATT_ICON="gradient-icons/draining.png"
BATT_ICON="$ICON_SET/draining.png"

if [ $CHARGING == Yes ]; then
TIME_FULL=$(cat info.txt | grep -e \"AvgTimeToFull\" | tr '\n' ' | ' | awk '{printf("%i:%.2i", $3/60, $3%60)}')
TIME_INFO=$(echo $TIME_FULL until full)
STATUS_INFO=Charging
BATT_ICON="gradient-icons/charging.png"
BATT_ICON="$ICON_SET/charging.png"
else
FULLY_CHARGED=$(cat info.txt | grep -e \"FullyCharged\" | awk '{printf("%s",$3)}')
EXTERNAL=$(cat info.txt | grep -e \"ExternalConnected\" | awk '{printf("%s",$3)}')
if [ $FULLY_CHARGED == Yes ]; then
if [ $EXTERNAL == Yes ]; then
TIME_INFO=$(echo On AC power)
STATUS_INFO=$(echo Fully Charged)
BATT_ICON="gradient-icons/power.png"
BATT_ICON="$ICON_SET/power.png"
else
TIME_INFO=$(echo $TIME_LEFT)
BATT_ICON="gradient-icons/full.png"
BATT_ICON="$ICON_SET/full.png"
fi
else
TIME_INFO=$(echo $TIME_LEFT)
BATT_ICON="gradient-icons/critical.png"
BATT_ICON="$ICON_SET/critical.png"
if [ $CHARGE -gt 80 ]; then
BATT_ICON="gradient-icons/full.png"
BATT_ICON="$ICON_SET/full.png"
elif [ $CHARGE -gt 50 ]; then
BATT_ICON="gradient-icons/medium.png"
BATT_ICON="$ICON_SET/medium.png"
elif [ $CHARGE -gt 10 ]; then
BATT_ICON="gradient-icons/low.png"
BATT_ICON="$ICON_SET/low.png"
fi
fi
fi
Expand All @@ -69,7 +75,7 @@ let "day=(MANUFACTURE_DATE >> 32) & 0xFFFF"
AGE=$(python3 -c "from datetime import date as D; d1=D.today(); d2=D($year, $month, $day); print ( (d1.year - d2.year)*12 + d1.month - d2.month )")
TRACKPAD_ICON="gradient-icons/trackpad.png"
TRACKPAD_ICON="$ICON_SET/trackpad.png"
# trackpad
TrackpadPercent=`ioreg -c AppleDeviceManagementHIDEventService | grep -se \"Magic Trackpad\" -A8 | grep -se \"BatteryPercent\" | sed 's/[a-z,A-Z, ,|,\",=]//g' | tail -1 | awk '{print $1}'`
if [ ${#TrackpadPercent} = 0 ]
Expand All @@ -80,7 +86,7 @@ else
TrackpadTitle="$TrackpadPercent% $TrackpadSlug"
fi
MOUSE_ICON="gradient-icons/mouse.png"
MOUSE_ICON="$ICON_SET/mouse.png"
# mouse
MousePercent=`ioreg -c AppleDeviceManagementHIDEventService | grep -se \"Magic Mouse\" -A8 | grep -se \"BatteryPercent\" | sed 's/[a-z,A-Z, ,|,\",=]//g' | tail -1 | awk '{print $1}'`
if [ ${#MousePercent} = 0 ]
Expand All @@ -91,7 +97,7 @@ else
MouseTitle="$MousePercent% $MouseSlug"
fi
KEYBOARD_ICON="gradient-icons/keyboard.png"
KEYBOARD_ICON="$ICON_SET/keyboard.png"
# keyboard
KeyboardPercent=`ioreg -c AppleDeviceManagementHIDEventService | grep -se \"Magic Keyboard\" -A8 | grep -se \"BatteryPercent\" | sed 's/[a-z,A-Z, ,|,\",=]//g' | tail -1 | awk '{print $1}'`
if [ ${#KeyboardPercent} = 0 ]
Expand All @@ -113,22 +119,22 @@ cat << EOB
<item>
<title>$TIME_INFO</title>
<subtitle>Time Left</subtitle>
<icon>gradient-icons/clock.png</icon>
<icon>$ICON_SET/clock.png</icon>
</item>
<item>
<title>${TEMPERATURE} °C</title>
<subtitle>Temperature</subtitle>
<icon>gradient-icons/temp.png</icon>
<icon>$ICON_SET/temp.png</icon>
</item>
<item>
<title>$CYCLE_COUNT</title>
<subtitle>Charge Cycles Completed</subtitle>
<icon>gradient-icons/cycles.png</icon>
<icon>$ICON_SET/cycles.png</icon>
</item>
<item>
<title>$HEALTH%</title>
<subtitle>Health</subtitle>
<icon>gradient-icons/health.png</icon>
<icon>$ICON_SET/health.png</icon>
</item>
<item>
<title>$MouseTitle</title>
Expand All @@ -148,12 +154,12 @@ cat << EOB
<item>
<title>$SERIAL</title>
<subtitle>Serial</subtitle>
<icon>gradient-icons/serial.png</icon>
<icon>$ICON_SET/serial.png</icon>
</item>
<item>
<title>$AGE months</title>
<subtitle>Age</subtitle>
<icon>gradient-icons/age.png</icon>
<icon>$ICON_SET/age.png</icon>
</item>
</items>
EOB
Binary file added src/icons/keyboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/mouse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/trackpad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added workflow/Notify.tgz
Binary file not shown.
108 changes: 108 additions & 0 deletions workflow/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2014 Dean Jackson <[email protected]>
#
# MIT Licence. See http://opensource.org/licenses/MIT
#
# Created on 2014-02-15
#

"""A helper library for `Alfred <http://www.alfredapp.com/>`_ workflows."""

import os

# Workflow objects
from .workflow import Workflow, manager
from .workflow3 import Variables, Workflow3

# Exceptions
from .workflow import PasswordNotFound, KeychainError

# Icons
from .workflow import (
ICON_ACCOUNT,
ICON_BURN,
ICON_CLOCK,
ICON_COLOR,
ICON_COLOUR,
ICON_EJECT,
ICON_ERROR,
ICON_FAVORITE,
ICON_FAVOURITE,
ICON_GROUP,
ICON_HELP,
ICON_HOME,
ICON_INFO,
ICON_NETWORK,
ICON_NOTE,
ICON_SETTINGS,
ICON_SWIRL,
ICON_SWITCH,
ICON_SYNC,
ICON_TRASH,
ICON_USER,
ICON_WARNING,
ICON_WEB,
)

# Filter matching rules
from .workflow import (
MATCH_ALL,
MATCH_ALLCHARS,
MATCH_ATOM,
MATCH_CAPITALS,
MATCH_INITIALS,
MATCH_INITIALS_CONTAIN,
MATCH_INITIALS_STARTSWITH,
MATCH_STARTSWITH,
MATCH_SUBSTRING,
)


__title__ = 'Alfred-Workflow'
__version__ = open(os.path.join(os.path.dirname(__file__), 'version')).read()
__author__ = 'Dean Jackson'
__licence__ = 'MIT'
__copyright__ = 'Copyright 2014-2019 Dean Jackson'

__all__ = [
'Variables',
'Workflow',
'Workflow3',
'manager',
'PasswordNotFound',
'KeychainError',
'ICON_ACCOUNT',
'ICON_BURN',
'ICON_CLOCK',
'ICON_COLOR',
'ICON_COLOUR',
'ICON_EJECT',
'ICON_ERROR',
'ICON_FAVORITE',
'ICON_FAVOURITE',
'ICON_GROUP',
'ICON_HELP',
'ICON_HOME',
'ICON_INFO',
'ICON_NETWORK',
'ICON_NOTE',
'ICON_SETTINGS',
'ICON_SWIRL',
'ICON_SWITCH',
'ICON_SYNC',
'ICON_TRASH',
'ICON_USER',
'ICON_WARNING',
'ICON_WEB',
'MATCH_ALL',
'MATCH_ALLCHARS',
'MATCH_ATOM',
'MATCH_CAPITALS',
'MATCH_INITIALS',
'MATCH_INITIALS_CONTAIN',
'MATCH_INITIALS_STARTSWITH',
'MATCH_STARTSWITH',
'MATCH_SUBSTRING',
]
Loading

0 comments on commit 3b8a228

Please sign in to comment.