aboutsummaryrefslogtreecommitdiff
path: root/.config/i3blocks/blocks/battery-plus
blob: d097f721d81d44336c84cc68cba9c96e5b51ec1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
#!/usr/bin/env bash
#
#  battery-plus
#
#  An enhanced battery status indicator for i3blocks.
#
#  Requires:
#    awk (POSIX compatible)
#    bc
#    upower
#
#  Optional:
#    fonts-font-awesome
#    notify-send or dunstify -- for notifications
#    zenity -- for dialogs
#
#  Copyright (c) 2018 Beau Hastings. All rights reserved.
#  License: GNU General Public License v2
#
#  Author: Beau Hastings <beausy@gmail.com>
#  URL: https://github.com/hastinbe/i3blocks-battery-plus

_FONT=${_FONT:-"FontAwesome"}
_PERCENT="&#x25;"

# Hide the battery status if fully-charged,
_HIDE_IF_CHARGED=${_HIDE_IF_CHARGED:-false}

# Color the battery symbol using gradient.
_USE_BATT_GRADIENT=${_USE_BATT_GRADIENT:-false}

# Only show symbols.
_SYMBOLS_ONLY=${_SYMBOLS_ONLY:-false}

# Hide the battery percentage
_HIDE_PERCENTAGE=${_HIDE_PERCENTAGE:-false}

# Hide the battery time remaining
_HIDE_TIME_REMAINING=${_HIDE_TIME_REMAINING:-true}

# Hide the time to charge the battery to full
_HIDE_TIME_TO_FULL=${_HIDE_TIME_TO_FULL:-true}

# Show the direction of change for the battery's percentage
_SHOW_CHARGE_DIRECTION=${_SHOW_CHARGE_DIRECTION:-true}

# Show an alert symbol when the battery capacity drops to the given percent (0=disable).
_CAPACITY_ALERT=${_CAPACITY_ALERT:0}

# Action to take when the battery level reaches critical.
_CRITICAL_ACTION=${_CRITICAL_ACTION:-"notify"}

# Action to take when the battery level reaches low.
_LOW_ACTION=${_LOW_ACTION:-"notify"}

# Method to use for notifications
_NOTIFY_PROGRAM=${_NOTIFY_PROGRAM:-"notify-send"}

# The duration, in milliseconds, for the notification to appear on screen.
_NOTIFY_EXPIRES="1500"

# Minimum time in seconds between notifications to prevent spam.
_NOTIFY_THROTTLE=${_NOTIFY_THROTTLE:-120}

# Colors
_COLOR_FULLY_CHARGED=${_COLOR_FULLY_CHARGED:-""}
_COLOR_CHARGING=${_COLOR_CHARGING:-"yellow"}
_COLOR_DISCHARGING=${_COLOR_CHARGING:-"yellow"}
_COLOR_PENDING=${_COLOR_PENDING:-"blue"}
_COLOR_ERROR=${_COLOR_ERROR:-"red"}
_COLOR_BATTERY=${_COLOR_BATTERY:-"white"}
_COLOR_ALERT=${_COLOR_ALERT:-"orange"}
_COLOR_DIRECTIONAL_UP=${_COLOR_DIRECTIONAL_UP:-"green"}
_COLOR_DIRECTIONAL_DOWN=${_COLOR_DIRECTIONAL_DOWN:-"red"}
_COLOR_GRADIENT_START=${_COLOR_GRADIENT_START:-"#FF0000"}
_COLOR_GRADIENT_END=${_COLOR_GRADIENT_END:-"#00FF00"}

# Symbols
_SYMBOL_FULLY_CHARGED=${_SYMBOL_FULLY_CHARGED:-""}
_SYMBOL_CHARGING=${_SYMBOL_CHARGING:-"&#xf0e7;"}
_SYMBOL_DISCHARGING=${_SYMBOL_DISCHARGING:-""}
_SYMBOL_UNKNOWN=${_SYMBOL_UNKNOWN:-"&#xf128;"}
_SYMBOL_PENDING=${_SYMBOL_PENDING:-"&#xf254;"}
_SYMBOL_ERROR=${_SYMBOL_ERROR:-"&#xf00d;"}
_SYMBOL_ALERT=${_SYMBOL_ALERT:-"&#xf071;"}
_SYMBOL_BATT_100=${_SYMBOL_BATT_100:-"&#xf240;"}
_SYMBOL_BATT_75=${_SYMBOL_BATT_75:-"&#xf241;"}
_SYMBOL_BATT_50=${_SYMBOL_BATT_50:-"&#xf242;"}
_SYMBOL_BATT_25=${_SYMBOL_BATT_25:-"&#xf243;"}
_SYMBOL_BATT_0=${_SYMBOL_BATT_0:-"&#xf244;"}
_SYMBOL_DIRECTION_UP=${_SYMBOL_DIRECTION_UP:-"&#8593;"}
_SYMBOL_DIRECTION_DOWN=${_SYMBOL_DIRECTION_DOWN:-"&#8595;"}

# Get a the system's temporary files directory.
#
# Notes:
#   Executes a dry-run so we don't create a file.
#   Linux doesn't require the template option, but MacOSX does.
#
# Returns:
#   The path to the temporary directory.
get_temp_dir() {
    echo $(dirname $(mktemp -ut "battery-plus.XXX"))
}

# Output text wrapped in a span element
#
# Options:
#   -c <color>  To specify a text color
#
# Arguments:
#   $1 or $3 - String to encapsulate within a span
span() {
    local -A attribs
    local text="$*"

    if [ -n "$FONT" ]; then attribs[font]="$FONT"; fi

    if [ "$1" = "-c" ]; then
        attribs[color]="$2"
        text="${*:3}"
    fi

    echo "<span$(build_attribs attribs)>$text</span>"
}

# Builds html element attributes
#
# Arguments:
#   $1 - An associative array of attribute-value pairs
build_attribs() {
    local -n array=$1
    for k in "${!array[@]}"; do
        echo -n " $k='${array[$k]}'"
    done
    echo
}

# Interpolate between 2 RGB colors
#
# Arguments:
#   $1 - Color to interpolate from, as a hex triplet prefixed with #
#   $2 - Color to interpolate to, as a hex triplet prefixed with #
#   $3 - Amount of steps needed to get from start to end color
interpolate_rgb() {
    local -i steps=$3
    local -a colors
    local color1=$1
    local color2=$2
    local reversed=false

    if (( 0x${color1:1:5} > 0x${color2:1:5} )); then
        color1=$2
        color2=$1
        reversed=true
    fi

    printf -v startR "%d" 0x${color1:1:2}
    printf -v startG "%d" 0x${color1:3:2}
    printf -v startB "%d" 0x${color1:5:2}
    printf -v endR "%d" 0x${color2:1:2}
    printf -v endG "%d" 0x${color2:3:2}
    printf -v endB "%d" 0x${color2:5:2}

    stepR=$(( ($endR - $startR) / $steps ))
    stepG=$(( ($endG - $startG) / $steps ))
    stepB=$(( ($endB - $startB) / $steps ))

    for i in $(seq 0 $steps); do
        local -i R=$(( $startR + ($stepR * $i) ))
        local -i G=$(( $startG + ($stepG * $i) ))
        local -i B=$(( $startB + ($stepB * $i) ))

        colors+=( "$(printf "#%02x%02x%02x\n" $R $G $B)" )
    done
    colors+=( "$(printf "#%02x%02x%02x\n" $endR $endB $endG)" )

    if $reversed; then
        reverse colors colors_reversed
        echo "${colors_reversed[@]}"
    else
        echo "${colors[@]}"
    fi
}

# Reverse an array
#
# Arguments:
#   $1 - Array to reverse
#   $2 - Destination array
reverse() {
    local -n arr="$1" rev="$2"
    for i in "${arr[@]}"
    do
        rev=("$i" "${rev[@]}")
    done
}

min() {
    echo "if ($1 < $2) $1 else $2" | bc
}

max() {
    echo "if ($1 > $2) $1 else $2" | bc
}

clamp() {
    echo $(min $(max $1 $2) $3)
}

# Retrieve an item from the provided lookup table based on the percentage of a number
#
# Arguments:
#   $1 - A number
#   $2 - Number to get a percentage of
#   $3 - An array of potential values
plookup() {
    if [ -z "$2" -o -z "$3" ]; then
        return
    fi

    local table=( ${@:3} )
    local number2=${2%.*}
    local number1=$(min ${1%.*} $number2)
    local percent_max=$(( $number1 * $number2 / 100 ))
    local index=$(( $percent_max * ${#table[@]} / 100 ))

    index=$(clamp $index 0 $(( ${#table[@]} - 1 )) )

    echo ${table[$index]}
}

# Get battery status.
#
# Returns:
#   The battery's status or state.
get_battery_status() {
    echo "$__UPOWER_INFO" | awk -W posix '$1 == "state:" {print $2}'
}

# Get battery warning level.
#
# Returns:
#   The battery's warning level.
get_battery_warning_level() {
    echo "$__UPOWER_INFO" | awk -W posix '$1 == "warning-level:" {print $2}'
}

# Get battery percentage.
#
# Returns:
#   The battery's percentage, without the %.
get_battery_percent() {
    echo "$__UPOWER_INFO" | awk -W posix '$1 == "percentage:" { gsub("%","",$2); print $2}'
}

# Get battery capacity.
#
# Returns:
#   The battery's capcity, without the %.
get_battery_capacity() {
    echo "$__UPOWER_INFO" | awk -W posix '$1 == "capacity:" { gsub("%","",$2); print $2}'
}

# Get battery time left.
#
# Returns:
#   The battery's time left.
get_time_to_empty() {
    echo "$__UPOWER_INFO" | awk -W posix -F':' '/time to empty:/{print $2}' | xargs
}

# Get the time remaining until the battery is fully charged.
#
# Returns:
#   The time remaining until battery is fully charged.
get_time_to_full() {
    echo "$__UPOWER_INFO" | awk -W posix -F':' '/time to full:/{print $2}' | xargs
}

# Get symbol for the given battery percentage.
#
# Arguments:
#   $percent  (int)  Battery percentage.
#
# Returns:
#  The battery symbol.
get_battery_charge_symbol() {
    local percent="$1"
    local symbol

    if [ "$battery_percentage" -ge 90 ]; then symbol="$_SYMBOL_BATT_100"
    elif [ "$battery_percentage" -ge 70 ]; then symbol="$_SYMBOL_BATT_75"
    elif [ "$battery_percentage" -ge 40 ]; then symbol="$_SYMBOL_BATT_50"
    elif [ "$battery_percentage" -ge 20 ]; then symbol="$_SYMBOL_BATT_25"
    else symbol="$_SYMBOL_BATT_0"
    fi

    echo "$symbol"
}

# Get battery status symbol.
#
# Returns:
#   An symbol name, following the Symbol Naming Specification
get_battery_status_symbol() {
    echo "$__UPOWER_INFO" | awk -W posix '$1 == "symbol-name:" {print $2}'
}

# Get color for the given battery percentage.
#
# Arguments:
#   $percent  (int)  Battery percentage.
#
# Returns:
#   The color to use for the given battery percentage.
get_percentage_color() {
    local colors=( $(interpolate_rgb "$_COLOR_GRADIENT_START" "$_COLOR_GRADIENT_END" 8) )
    echo $(plookup $1 100 ${colors[@]})
}

# Determines whether or not we can send a notification.
#
# Returns:
#   0 on false, 1 on true.
can_notify() {
    local -i now=$(date +"%s")
    local -i last=$(get_last_notify_time)

    if [ -z "$last" ]; then return 0; fi

    local -i diff=$(($now - $last))

    [ $diff -gt $_NOTIFY_THROTTLE ]
}

# Get the last time we sent a notification.
#
# Returns:
#   Time in seconds since the Epoch.
get_last_notify_time() {
    if [ -f "$TMP_NOTIFY" ]; then
        echo $(stat -c '%Y' "$TMP_NOTIFY")
    fi
}

# Save the last time we sent a notification.
save_last_notify_time() {
    touch -m "$TMP_NOTIFY"
}

# Display the charging indicator.
#
# Returns:
#   The charging indicator.
display_batt_charging() {
    if [ -n "$_SYMBOL_CHARGING" ]; then
        echo -n "$(span -c "${_COLOR_CHARGING}" "$_SYMBOL_CHARGING") "
    fi

    echo "$colored_battery_symbol"
}

# Display the discharging indicator.
#
# Returns:
#   The discharging indicator.
display_batt_discharging() {
    if [ -n "$_SYMBOL_DISCHARGING" ]; then
        echo -n "$(span -c "${_COLOR_DISCHARGING}" "$_SYMBOL_DISCHARGING") "
    fi

    echo "$battery_charge_symbol"
}

# Display the pending charge indicator.
#
# Returns:
#   The pending charge indicator.
display_batt_pending_charge() {
    if [ -n "$_SYMBOL_PENDING" ]; then
       echo -n "$(span -c "${_COLOR_PENDING}" "$_SYMBOL_PENDING") "
    fi

    if [ -n "$_SYMBOL_CHARGING" ]; then
        echo -n "$(span -c "${_COLOR_CHARGING}" "$_SYMBOL_CHARGING") "
    fi

    echo "${battery_charge_symbol}"
}

# Display the pending discharge indicator.
#
# Returns:
#   The pending discharge indicator.
display_batt_pending_discharge() {
    if [ -n "$_SYMBOL_PENDING" ]; then
        echo -n "$(span -c "${_COLOR_PENDING}" "$_SYMBOL_PENDING") "
    fi

    if [ -n "$_SYMBOL_DISCHARGING" ]; then
        echo -n "$(span -c "${_COLOR_DISCHARGING}" "$_SYMBOL_DISCHARGING") "
    fi

    echo "$colored_battery_symbol"
}

# Display the empty battery indicator.
#
# Returns:
#   The empty battery indicator.
display_batt_empty() {
    echo "$battery_charge_symbol"
}

# Display the fully charged indicator.
#
# Returns:
#   The fully-charged indicator.
display_batt_fully_charged() {
   if [ -n "$_SYMBOL_FULLY_CHARGED" ]; then
        echo -n "$(span -c "${_COLOR_FULLY_CHARGED}" "$_SYMBOL_FULLY_CHARGED") "
    fi

    echo "$battery_charge_symbol"
}

# Display the unknown battery indicator.
#
# Returns:
#   The unknonw battery indicator.
display_batt_unknown() {
    if [ -n "$_SYMBOL_UNKNOWN" ]; then
        echo -n "$(span "$_SYMBOL_UNKNOWN") "
    fi

    echo "$(get_battery_charge_symbol 0)"
}

# Display the battery error indicator.
#
# Returns:
#   The battery error indicator.
display_batt_error() {
    echo "$(span -c "${_COLOR_ERROR}" "$_SYMBOL_ERROR" "$_SYMBOL_BATT_0")"
}

# Display the battery percentage.
#
# Returns:
#   The battery percentage, or nothing if disabled.
display_percentage() {
    if ! [ $_SYMBOLS_ONLY = true -o $_HIDE_PERCENTAGE = true ]; then
        echo -n " $(span -c "${percentage_color}" "${battery_percentage:---}${_PERCENT}")"

        if [ "$status" = "charging" ] && $_SHOW_CHARGE_DIRECTION; then
            echo -n "$(span -c "${_COLOR_DIRECTIONAL_UP}" "${_SYMBOL_DIRECTION_UP}")"
        elif [ "$status" = "discharging" ] && $_SHOW_CHARGE_DIRECTION; then
            echo -n "$(span -c "${_COLOR_DIRECTIONAL_DOWN}" "${_SYMBOL_DIRECTION_DOWN}")"
        fi

        echo
    fi
}

# Display the battery time remaining.
#
# Arguments:
#   $force  (bool) Force display.
#
# Returns:
#   The time remaining, or nothing if disabled.
display_time_remaining() {
    local force=${1:-false}

    if [ $force = true ] || ! [ $_SYMBOLS_ONLY = true -o $_HIDE_TIME_REMAINING = true ]; then
        time_to_empty=$(get_time_to_empty "$battery_info")
        if [ -n "$time_to_empty" ]; then
            echo "$time_to_empty"
        fi
    fi
}

# Display the battery time to fully charge.
#
# Arguments:
#   $force  (bool) Force display.
#
# Returns:
#   The time to fully charge, or nothing if disabled.
display_time_to_full() {
    local force=${1:-false}

    if [ $force = true ] || ! [ $_SYMBOLS_ONLY = true -o $_HIDE_TIME_TO_FULL = true ]; then
        time_to_full=$(get_time_to_full "$battery_info")
        if [ -n "$time_to_full" ]; then
            echo "$time_to_full"
        fi
    fi
}

# Displays an array of segments.
#
# Arguments:
#   $segments  (array)  The an array of segments.
#
# Returns:
#   Each segment after another, followed by a newline.
display_segments() {
    local -a segments="$@"

    for segment in "${segments[@]}"; do
        if [ -n "$segment" ]; then
            echo -n "$segment"
        fi
    done

    echo
}

# Colors text using either the first or second color
#
# Arguments:
#   $1 - Text
#   $2 - Color to use if toggle is false
#   $3 - Color to use if toggle is true
#   $4 - A boolean used to toggle between colors
#
# Returns:
#   The colored text
multicolor() {
    local color="$2"
    local toggle=$4

    if $toggle && [ -n "$3" ]; then
        color="$3"
    fi

    if [ -n "$color" ]; then
        echo "$(span -c "$color" "$1")"
    else
        echo "$(span "$1")"
    fi
}


# Display a notification.
#
# Arguments:
#   $text  (string)  The notification text.
#   $symbol  (string)  Name of an symbol.
#   $type  (string)  The type of notification.
#   $force (string)  Force the notification, ignoring any throttle.
#   $rest  (string)  Any additional options to pass to the command.
notify() {
    local text="$1"
    local symbol="$2"
    local type="$3"
    local force="$4"
    local rest="${@:5}"
    local command

    if [ -z "$text" ] || ! $force && ! $(can_notify); then return; fi

    if [ "$_NOTIFY_PROGRAM" = "dunstify" ]; then
        command="dunstify -r 1001"
    elif [ "$_NOTIFY_PROGRAM" = "notify-send" ]; then
        command="notify-send"
    fi

    if [ -n "$_NOTIFY_EXPIRES" ]; then command="$command -t $_NOTIFY_EXPIRES"; fi
    if [ -n "$symbol" ]; then command="$command -i $symbol"; fi
    if [ -n "$type" ]; then command="$command -u $type"; fi

    command="$command $rest \"$text\""
    if eval $command; then
        save_last_notify_time
    fi
}

# Display a dialog.
#
# Arguments:
#   $text  (string)  The dialog text.
#   $symbol  (string)  Name of an symbol.
#   $type  (string)  The type of dialog.
#   $rest  (string)  Any additional options to pass to the command.
dialog() {
    local text="$1"
    local symbol="$2"
    local type="$3"
    local rest="${@:4}"

    if [ -z "$text" ]; then
        return
    fi

    command="zenity"

    if [ -n "$symbol" ]; then command="$command --symbol-name=\"$symbol\""; fi
    if [ -n "$type" ]; then command="$command --${type}"; fi

    command="$command --text=\"$text\" $rest"
    eval $command
}

# Display program usage.
usage() {
    echo -e "Usage: $0 [options]
Display the battery status using upower for i3blocks.\n
Options:
  -a <none|notify>\taction to take when battery is low
  -A <color>\t\tcolor of the alert symbol
  -B <color>\t\tcolor of the battery symbol
  -c <capacity>\t\tset battery capacity alert percentage (0=disable)
  -C <color>\t\tcolor of the charging symbol
  -d\t\t\tshow the direction of change for the battery's percentage
  -D <color>\t\tcolor of the discharging symbol
  -e <millseconds>\texpiration time of notifications (Ubuntu's Notify OSD and GNOME Shell both ignore this parameter.)
  -E <color>\t\tcolor of the battery error symbol
  -f <font>\t\tfont for text and symbols
  -F <color>\t\tcolor of the fully-charged symbol
  -G\t\t\tcolor the battery symbol according to battery percentage
  -h\t\t\tdisplay this help and exit
  -H\t\t\tsuppress displaying if battery is fully-charged
  -i <color>\t\tcolor gradient start color
  -I\t\t\tonly display symbols
  -j <color>\t\tcolor gradient end color
  -J <symbol>\t\tsymbol for a battery 100% charged
  -k\t\t\thide the battery percentage
  -K <symbol>\t\tsymbol for a battery 75% charged
  -l <none|notify>\taction to take when the battery level reaches critical
  -L <symbol>\t\tsymbol to indicate there is an alert
  -M <symbol>\t\tsymbol for a battery 50% charged
  -n <program>\t\ta libnotify compatible notification program
  -N <color>\t\tcolor of the battery charge decreasing indicator
  -O <symbol>\t\tsymbol for a battery with no/low charge
  -p <symbol>\t\tsymbol to use for the percent sign
  -P <color>\t\tcolor of the pending charge symbol
  -Q <symbol>\t\tsymbol for a battery 25% charged
  -r\t\t\thide the battery time remaining
  -R <symbol>\t\tsymbol to indicate the battery state is undefined
  -S <symbol>\t\tsymbol to indicate battery is fully charged
  -t <seconds>\t\tminimum time in seconds between notifications to prevent spam
  -T <symbol>\t\tsymbol to indicate the battery is charging
  -u\t\t\thide the time to charge the battery to full
  -U <color>\t\tcolor of the battery charge increasing indicator
  -V <symbol>\t\tsymbol to indicate the battery state is unknown
  -W <symbol>\t\tsymbol to indicate battery charge is increasing
  -X <symbol>\t\tsymbol to indicate the battery state is pending
  -Y <symbol>\t\tsymbol to indicate the battery is discharging
  -Z <symbol>\t\tsymbol to indicate battery charge is decreasing
" 1>&2
    exit 1
}

###############################################################################

declare -a long_segments
declare -a short_segments

__UPOWER_INFO=$(upower --show-info "/org/freedesktop/UPower/devices/battery_${BLOCK_INSTANCE:-BAT0}")
TMP_NOTIFY="$(get_temp_dir)/battery-plus.last_notify"

while getopts "a:A:B:c:C:dD:e:E:f:F:GhHi:Ij:J:kK:l:L:M:n:N:O:p:P:Q:rR:S:t:T:uU:V:W:X:Y:Z:" o; do
    case "$o" in
        a) _LOW_ACTION="${OPTARG}" ;;
        A) _COLOR_ALERT="${OPTARG}" ;;
        B) _COLOR_BATTERY="${OPTARG}" ;;
        c) _CAPACITY_ALERT="${OPTARG}" ;;
        C) _COLOR_CHARGING="${OPTARG}" ;;
        d) _SHOW_CHARGE_DIRECTION=true ;;
        D) _COLOR_DISCHARGING="${OPTARG}" ;;
        e) _NOTIFY_EXPIRES="${OPTARG}" ;;
        E) _COLOR_ERROR="${OPTARG}" ;;
        f) _FONT="${OPTARG}" ;;
        F) _COLOR_FULLY_CHARGED="${OPTARG}" ;;
        G) _USE_BATT_GRADIENT=true ;;
        h | *) usage ;;
        H) _HIDE_IF_CHARGED=true ;;
        i) _COLOR_GRADIENT_START="${OPTARG}" ;;
        I) _SYMBOLS_ONLY=true ;;
        j) _COLOR_GRADIENT_END="${OPTARG}" ;;
        J) _SYMBOL_BATT_100="${OPTARG}" ;;
        k) _HIDE_PERCENTAGE=true ;;
        K) _SYMBOL_BATT_75="${OPTARG}" ;;
        l) _CRITICAL_ACTION="${OPTARG}" ;;
        L) _SYMBOL_ALERT="${OPTARG}" ;;
        M) _SYMBOL_BATT_50="${OPTARG}" ;;
        n) _NOTIFY_PROGRAM="${OPTARG}" ;;
        N) _COLOR_DIRECTIONAL_DOWN="${OPTARG}" ;;
        O) _SYMBOL_BATT_0="${OPTARG}" ;;
        p) _PERCENT="${OPTARG}" ;;
        P) _COLOR_PENDING="${OPTARG}" ;;
        Q) _SYMBOL_BATT_25="${OPTARG}" ;;
        r) _HIDE_TIME_REMAINING=true ;;
        R) _SYMBOL_ERROR="${OPTARG}" ;;
        S) _SYMBOL_FULLY_CHARGED="${OPTARG}" ;;
        t) _NOTIFY_THROTTLE="${OPTARG}" ;;
        T) _SYMBOL_CHARGING="${OPTARG}" ;;
        u) _HIDE_TIME_TO_FULL=true ;;
        U) _COLOR_DIRECTIONAL_UP="${OPTARG}" ;;
        V) _SYMBOL_UNKNOWN="${OPTARG}" ;;
        W) _SYMBOL_DIRECTION_UP="${OPTARG}" ;;
        X) _SYMBOL_PENDING="${OPTARG}" ;;
        Y) _SYMBOL_DISCHARGING="${OPTARG}" ;;
        Z) _SYMBOL_DIRECTION_DOWN="${OPTARG}" ;;
    esac
done
shift $((OPTIND-1)) # Shift off options and optional --

battery_percentage=$(get_battery_percent)
percentage_color=$(get_percentage_color "$battery_percentage")
capacity=$(get_battery_capacity)
battery_charge_symbol=$(get_battery_charge_symbol "$battery_percentage")
battery_status_symbol=$(get_battery_status_symbol)
colored_battery_symbol=$(multicolor "$battery_charge_symbol" "$_COLOR_BATTERY" "$percentage_color" $_USE_BATT_GRADIENT)
warning_level=$(get_battery_warning_level)

# Handle battery warning levels
case "$warning_level" in
    critical)
        case "$_CRITICAL_ACTION" in
            notify) $(notify "$(span "Your battery level is ${warning_level}!")" "$battery_status_symbol" "critical" false "-c device") ;;
        esac
        ;;
    low)
        case "$_LOW_ACTION" in
            notify) $(notify "$(span "Your battery level is ${warning_level}")" "$battery_status_symbol" "normal" false "-c device") ;;
        esac
        ;;
esac

# Displayable alerts
if [ "$_CAPACITY_ALERT" -gt 0 ] && [ "${capacity%.*}" -le $_CAPACITY_ALERT ]; then
    CAPACITY_TRIGGERED=true
    long_segments+=( "$(span -c "$_COLOR_ALERT" "$_SYMBOL_ALERT") " )
fi

# Battery statuses
status=$(get_battery_status)
case "$status" in
    charging)           long_segments+=( "$(display_batt_charging)" ) ;;
    discharging)        long_segments+=( "$(display_batt_discharging)" ) ;;
    empty)              long_segments+=( "$(display_batt_empty)" ) ;;
    fully-charged)
        if $_HIDE_IF_CHARGED; then
            exit 0
        fi
        long_segments+=( "$(display_batt_fully_charged)" )
        ;;
    pending-charge)     long_segments+=( "$(display_batt_pending_charge)" ) ;;
    pending-discharge)  long_segments+=( "$(display_batt_pending_discharge)" ) ;;
    unknown)            long_segments+=( "$(display_batt_unknown)" ) ;;
    *)                  long_segments+=( "$(display_batt_error)" ) ;;
esac

# Since long_segments contains no text at this point, lets use it for the short_text
short_segments=( "${long_segments[@]}" )

# Append all text segments
long_segments+=( "$(display_percentage)" )
if [ -n "$(display_time_remaining)" ]; then
    long_segments+=( "($(display_time_remaining))" )
fi
if [ -n "$(display_time_to_full)" ]; then
    long_segments+=( "($(display_time_to_full))" )
fi

# Display the block long_text
display_segments ${long_segments[@]}

# Display the block short_text
display_segments ${short_segments[@]}

# Handle click events
case $BLOCK_BUTTON in
    1)
        if [ -n "$CAPACITY_TRIGGERED" ]; then
            $(dialog "$(span "Your battery capacity has reduced to ${capacity%.*}${_PERCENT}!")" "battery-caution" "warning" "--no-wrap")
        else
            if [ "$status" = "discharging" ]; then
                $(notify "$(span "$(display_time_remaining true) remaining")" "alarm-symbolic" "normal" true "-c device")
            elif [ "$status" = "charging" ]; then
                $(notify "$(span "$(display_time_to_full true) until fully charged")" "alarm-symbolic" "normal" true "-c device")
            fi
        fi
        ;;
esac

if [ -n "$battery_percentage" ] && [ "$battery_percentage" -le 5 ]; then
    exit 33
fi
nihil fit ex nihilo