holman / dotfiles

@holman does dotfiles
http://zachholman.com/2010/08/dotfiles-are-meant-to-be-forked/
MIT License
7.35k stars 3.36k forks source link

Resource Improvement and Increased Portability (suggestion) #292

Closed ghost closed 7 years ago

ghost commented 7 years ago

Hi,

Hope this helps. It's regarding:

https://github.com/holman/dotfiles/blob/master/bin/battery-status

I don't have a Mac, so I can't actually properly test this, but I've looked around online for example output using that command, so this is the best I can do with it. This saves using extra processes when bash is more than capable of doing it itself.

Because I'm using a while read array, the "(no" and "not" testing will probably be broken, but will be easy to sort; I just can't test for that condition, sadly.

This also compresses it and makes it more concise. Feel free to go with the more stretched out styling of this, but thought I would show my approach. :)

while read -a X; do
        time_left="${X[9]}"
done <<< `pmset -g batt`

[[ "$(pmset -g ac)" == *"No adapter attached."* ]] && emoji='🔋' || emoji='🔌'

case "$time_left" in
        \(no|not) time_left='⌛️ ' ;;
        0:00) time_left='⚡️ ' ;;
esac

printf "\033[1;92m$emoji  $time_left \033[0m"
holman commented 7 years ago

Appreciate it, but I'll always opt for clarity over optimizations, particularly in bash. :)

ghost commented 7 years ago

That's fine. I actually have only jsut realised I messed it up. Forgot to remove that if statement. It's even clearer now.