i3: Using better script for battery in bar

This commit is contained in:
Julian Mutter 2021-07-22 16:21:55 +02:00
parent 723c7ae6c1
commit f0129a370f
5 changed files with 100 additions and 1295 deletions

98
i3/.config/i3blocks/battery Executable file
View File

@ -0,0 +1,98 @@
#!/usr/bin/env python3
#
# Copyright (C) 2016 James Murphy
# Licensed under the GPL version 2 only
#
# A battery indicator blocklet script for i3blocks
import re
from subprocess import check_output
status = check_output(['acpi'], universal_newlines=True)
if not status:
# stands for no battery found
fulltext = "<span color='red'><span font='FontAwesome'>\uf00d \uf240</span></span>"
percentleft = 100
else:
# if there is more than one battery in one laptop, the percentage left is
# available for each battery separately, although state and remaining
# time for overall block is shown in the status of the first battery
batteries = status.split("\n")
state_batteries=[]
commasplitstatus_batteries=[]
percentleft_batteries=[]
time = ""
for battery in batteries:
if battery!='':
state_batteries.append(battery.split(": ")[1].split(", ")[0])
commasplitstatus = battery.split(", ")
if not time:
time = commasplitstatus[-1].strip()
# check if it matches a time
time = re.match(r"(\d+):(\d+)", time)
if time:
time = ":".join(time.groups())
timeleft = " ({})".format(time)
p = int(commasplitstatus[1].rstrip("%\n"))
if p>0:
percentleft_batteries.append(p)
commasplitstatus_batteries.append(commasplitstatus)
state = state_batteries[0]
commasplitstatus = commasplitstatus_batteries[0]
if percentleft_batteries:
percentleft = int(sum(percentleft_batteries)/len(percentleft_batteries))
else:
percentleft = 0
# stands for charging
FA_LIGHTNING = "<span color='yellow'><span font='FontAwesome'>\uf0e7</span></span>"
# stands for plugged in
FA_PLUG = "<span font='FontAwesome'>\uf1e6</span>"
# stands for using battery
FA_BATTERY = "<span font='FontAwesome'>\uf240</span>"
# stands for unknown status of battery
FA_QUESTION = "<span font='FontAwesome'>\uf128</span>"
if state == "Discharging":
fulltext = FA_BATTERY + " "
elif state == "Full":
fulltext = FA_PLUG + " "
timeleft = ""
elif state == "Unknown":
fulltext = FA_QUESTION + " " + FA_BATTERY + " "
timeleft = ""
else:
fulltext = FA_LIGHTNING + " " + FA_PLUG + " "
def color(percent):
if percent < 10:
# exit code 33 will turn background red
return "#FFFFFF"
if percent < 20:
return "#FF3300"
if percent < 30:
return "#FF6600"
if percent < 50:
return "#FFCC00"
if percent < 60:
return "#FFFF00"
if percent < 70:
return "#FFFF33"
if percent < 80:
return "#FFFF66"
return "#FFFFFF"
form = '<span color="{}">{}%</span>'
fulltext += form.format(color(percentleft), percentleft)
fulltext += timeleft
print(fulltext)
print(fulltext)
if percentleft < 10:
exit(33)

View File

@ -1,793 +0,0 @@
#!/usr/bin/env bash
#
# battery-plus
#
# An enhanced battery status indicator for i3blocks.
#
# Requires:
# awk (POSIX compatible)
# bc
# upower
#
# Recommended:
# fonts-font-awesome or fonts-hack
#
# Optional:
# 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 <beau@saweet.net>
# URL: https://github.com/hastinbe/i3blocks-battery-plus
_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:-75}
# 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 $endG $endB)" )
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: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) _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

View File

@ -1,35 +0,0 @@
#!/bin/bash
DATEFMT=${DATEFMT:-"+%a %d.%m.%Y %H:%M:%S"}
SHORTFMT=${SHORTFMT:-"+%H:%M:%S"}
OPTIND=1
while getopts ":f:" opt; do
case $opt in
f) DATEFMT="$OPTARG" ;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
case "$BLOCK_BUTTON" in
1|2|3)
# Try to focus Thunderbird
i3-msg -q [class="Thunderbird" instance="Mail"] focus
if [ $? == 2 ]
then
# Open Thunderbird if it was not open
i3-msg -q "exec thunderbird -mail -calendar"
else
# Open calendar
xdotool key --clearmodifiers --delay 5 ctrl+shift+c
fi
esac
echo "$LABEL$(date "$DATEFMT")"
echo "$LABEL$(date "$SHORTFMT")"

View File

@ -5,10 +5,6 @@
#
# https://vivien.github.io/i3blocks
# Sources:
# https://github.com/hastinbe/i3blocks-battery-plus
# https://github.com/zakariaGatter/i3blocks-gate
# Global properties
separator=true
separator_block_width=15
@ -31,7 +27,8 @@ interval=10
signal=1
[battery]
command=./i3b-gate 6
command=./battery
markup=pango
interval=120
[calendar]

View File

@ -1,462 +0,0 @@
#!/usr/bin/env bash
# Zakaria Barkouk ( Zakaria.gatter@gmail.com)
#---------------------#
# SHOW CPU INFO (TOP) #
#---------------------#
_BLOCK_1_(){ #{{{
mpstat | awk '/all/{print "'"${1:-} "'"$4}'
} #}}}
#--------------------#
# SHOW ALL CPUS INFO #
#--------------------#
_BLOCK_2_(){ #{{{
mpstat -P ALL | awk -v icon=${1:-} 'BEGIN{printf "%s ", icon} {
if($3 ~ /^[0-9]/){
printf "%s ", $4
}
}'
} #}}}
#--------------------------#
# SHOW MEMORY USAGE (FREE) #
#--------------------------#
_BLOCK_3_(){ #{{{
free -h | awk '/^Mem:/{print "'"${1:-} "'"$3}'
} #}}}
#------------------------#
# SHOW SWAP USAGE (FREE) #
#------------------------#
_BLOCK_4_(){ #{{{
free -h | awk '/^Swap:/{print "'"${1:-}"' "$3}'
} #}}}
#---------------------------#
# SHOW DATE AND TIME (DATE) #
#---------------------------#
_BLOCK_5_(){ #{{{
date +"${1:-} %R %D"
} #}}}
#----------------------------#
# SHOW BATTERY STATUS (ACPI) #
#----------------------------#
_BLOCK_6_(){ #{{{
Bat=$(acpi | awk '{gsub(",|%","",$4); print $4}');
adapt=$(acpi -a | awk '{print $3}');
if [ "$adapt" = "on-line" -a -n "$Bat" ];then
icon0=" "
icon1=" "
icon2=" "
icon3=" "
icon4=" "
elif [ "$adapt" = "on-line" ];then
icon0=""
icon1=""
icon2=""
icon3=""
icon4=""
else
icon0=""
icon1=""
icon2=""
icon3=""
icon4=""
fi
[ -z "$Bat" ] && echo "$icon0 $adapt" && return
[ "$Bat" -gt "100" ] && echo "$icon4 Full"
[ "$Bat" -gt "90" ] && echo "$icon3 $Bat%"
[ "$Bat" -gt "60" ] && echo "$icon2 $Bat%"
[ "$Bat" -gt "30" ] && echo "$icon1 $Bat%"
[ "$Bat" -lt "30" ] && echo "$icon0 $Bat%"
} #}}}
#----------------------#
# SHOW DISK USAGE (DF) #
#----------------------#
_BLOCK_7_() { #{{{
df -h "${1:-/}" | awk '/\/dev\//{print "'"${2:-}"' "$3-G"/"$2}'
} #}}}
#----------------------#
# SHOW KEYBOARD LAYOUT #
#----------------------#
_BLOCK_8_() { #{{{
awk -F '"' '/XKBLAYOUT/{print "'"${1:-} "'"$2}' /etc/default/keyboard
} #}}}
#----------------------#
# SHOW SYSTEM LANGUAGE #
#----------------------#
_BLOCK_9_(){ #{{{
echo "${1:-} ${LANG/.UTF-8/}"
} #}}}
#------------------------------#
# SHOW MACHINE UPTIME (UPTIME) #
#------------------------------#
_BLOCK_10_() { #{{{
uptime | awk '{sub(",","",$4); print "'"${1:- } "'"$3 " " $4}'
} #}}}
#----------------------#
# SHOW TRASH SIZE (DU) #
#----------------------#
_BLOCK_11_() { #{{{
[ -d "$HOME/.local/share/Trash/files" ] && {
du -hc $HOME/.local/share/Trash/files | awk '/total$/{print "'"${1:-} "'"$1}'
} || {
echo "${1:-} ---"
}
} #}}}
#-------------------#
# SHOW WINDOW USAGE #
#-------------------#
_BLOCK_12_() { #{{{
focus=$(xprop -id `xprop -root | awk '/^_NET_ACTIVE_WINDOW/{print $5}'` | awk -F '"' '/^WM_NAME/{print $2}')
if [ -z "$focus" ];then
echo "${1:-} Welcome"
else
[ "${#focus}" -gt "${2:-30}" ] && echo "${1:-} ${focus::${2:-30}} ..." || echo "${1:-} $focus"
fi
} #}}}
#-----------------------------------------#
# SHOW MOCP PLAYING SONG AND STATUS (MOC) #
#-----------------------------------------#
_BLOCK_13_() { # {{{
Title=$(mocp -Q %title)
F_Title=$(basename `mocp -Q %file | tr " " "_"`)
Status=$(mocp -Q %state)
if [ "$Status" != "PLAY" ];then
echo "${1:-} Pause"
elif [ -z "$Title" ];then
echo "${1:-} ${F_Title::30}"
else
echo "${1:-} $Title"
fi
} #}}}
#------------------------------------------------------#
# SHOW MPD PLAYING SONG AND STATUS (MPD; NCMPCCP, MPC) #
#------------------------------------------------------#
_BLOCK_14_() { #{{{
local NCMP=$(mpc | awk '/^\[playing\]/{print $1}')
if [ "$NCMP" = "[playing]" ];then
echo "${1:-} $(basename $(mpc current)) "
else
echo "${1:-} Pause "
fi
} #}}}
#-------------------------#
# SHOW CPU TEMP (XSENSER) #
#-------------------------#
_BLOCK_15_() { #{{{
sensors | awk '/^CPU/{gsub("\+",""); print "'"${1:-} "'"$2}'
} #}}}
#-------------------------#
# SHOW GPU TEMP (XSENSER) #
#-------------------------#
_BLOCK_16_() { #{{{
sensors | awk '/^GPU/{gsub("\+",""); print "'"${1:-} "'"$2}' | tail -1
} #}}}
#----------------------------------#
# SHOW TOUCHPAD STATUS (SYSCLIENT) #
#----------------------------------#
_BLOCK_17_() { #{{{
[ "$(synclient -l | awk '/Touchpad/{print $3}')" = "0" ] && echo "${1:-} Enable" || echo "${1:-} Disable"
} #}}}
#---------------------------#
# SHOW VOLUME STATUS (ALSA) #
#---------------------------#
_BLOCK_18_() { #{{{
local Vol=$(amixer get Master | awk '/Mono:/{gsub("\[|\%|\]",""); print $4}')
local Mute=$(amixer get Master | awk '/Mono:/{gsub("\[|\]",""); print $6}')
if [ "$Mute" = "off" ];then
echo -e " --"
else
if [ "$Vol" -gt "60" ];then
echo -e " $Vol%"
elif [ "$Vol" -gt "30" ];then
echo -e " $Vol%"
elif [ "$Vol" -lt "30" ];then
echo -e " $Vol%"
fi
fi
} #}}}
#----------------------------------------------#
# SHOW WIFI INFO ; IP ; CONNECTED NAME (NMCLI) #
#----------------------------------------------#
_BLOCK_19_() { #{{{
while read -a W ;do
[ "${W[2]}" = "connected" ] && {
_ip=$(nmcli device show ${W[0]} | awk '/^IP4.ADDRESS/{print $NF}')
_name=$(nmcli device show ${W[0]} | awk '/^GENERAL.CONNECTION:/{$1=""; print $0}')
echo -n "${1:-} ${_ip%/*} (${_name} )"
} || {
echo -n "${1:-} ---"
}
done< <(nmcli d | grep -w "wifi")
echo -e ""
} #}}}
#-------------------------------------------#
# SHOW ETHERNET INFO ; IP ; CONNECTED NAME #
#-------------------------------------------#
_BLOCK_20_() { #{{{
while read -a W ;do
[ "${W[2]}" = "connected" ] && {
_ip=$(nmcli device show ${W[0]} | awk '/^IP4.ADDRESS/{print $NF}')
_name=$(nmcli device show ${W[0]} | awk '/^GENERAL.CONNECTION:/{$1=""; print $0}')
echo -n "${1:-} ${_ip%/*} (${_name} ) "
} || {
echo -n "${1:-} ---"
}
done< <(nmcli d | grep "ethernet")
echo -e ""
} #}}}
#-----------------------------------------------#
# SHOW WIFI INFO ; IP6 ; CONNECTED NAME (NMCLI) #
#-----------------------------------------------#
_BLOCK_21_() { #{{{
while read -a W ;do
[ "${W[2]}" = "connected" ] && {
_ip=$(nmcli device show ${W[0]} | awk '/^IP6.ADDRESS/{print $NF}')
_name=$(nmcli device show ${W[0]} | awk '/^GENERAL.CONNECTION:/{$1=""; print $0}')
echo -n "${1:-} ${_ip%/*} (${_name} ) "
} || {
echo -n "${1:-} ---"
}
done< <(nmcli d | grep "wifi")
echo -e ""
} #}}}
#--------------------------------------------#
# SHOW ETHERNET INFO ; IP6 ; CONNECTED NAME #
#--------------------------------------------#
_BLOCK_22_() { #{{{
while read -a W ;do
[ "${W[2]}" = "connected" ] && {
_ip=$(nmcli device show ${W[0]} | awk '/^IP6.ADDRESS/{print $NF}')
_name=$(nmcli device show ${W[0]} | awk '/^GENERAL.CONNECTION:/{$1=""; print $0}')
echo -n "${1:-} ${_ip%/*} (${_name} ) "
} || {
echo -n "${1:-} ---"
}
done< <(nmcli d | grep "ethernet")
echo -e ""
} #}}}
#-------------------------------#
# SHOW PROSSES USE BY THIS USER #
#-------------------------------#
_BLOCK_23_() { #{{{
echo "${1:-} $(ps -U $USER | wc -l)"
} #}}}
#---------------------#
# SHOW SYSTEM KERENEL #
#---------------------#
_BLOCK_24_() { #{{{
echo "${1:-} $(uname -r)"
} #}}}
#-----------------------------#
# SHOW USB PLUG IN UR COMPUTE #
#-----------------------------#
_BLOCK_25_() { #{{{
echo "${1:-} $(lsblk -l -o 'TRAN' | grep -c "usb")"
} #}}}
#---------------------------------------#
# SHOW TOTAL PKGS INSTALL IN YOUR SYSTE #
#---------------------------------------#
_BLOCK_26_() { #{{{
echo "${1:-} $(dpkg -l | grep -c "^ii")"
} #}}}
#--------------------------------------------#
# CHECK IF THERE IS ANY UPDATE IN UR SYSTEM #
#--------------------------------------------#
_BLOCK_27_() { #{{{
local U_PKGS=$(apt-get -s -o APT::Get::Show-User-Simulation-Note=0 dist-upgrade | grep "^\s\s" | wc -w);
echo "${1:-} $U_PKGS"
} #}}}
#-----------------#
# SHOW UFW STATUS #
#-----------------#
_BLOCK_28_() { #{{{
[ "$(systemctl status ufw | awk '/Active:/{print $2}')" == "active" ] && echo "${1:-} On" || echo "${1:-} Off"
} #}}}
#-------------------------#
# SHOW NUMBER LOCK STATUS #
#-------------------------#
_BLOCK_29_() { #{{{
[ "$(xset -q | awk '/00:/{print $8}')" == "on" ] && echo "${1:-} On" || echo "${1:-} Off"
} #}}}
#-----------------------#
# SHOW BLUETOOTH STATUS #
#-----------------------#
_BLOCK_30_() { #{{{
[ "$(systemctl status bluetooth.target | awk '/Active:/{print $2}')" == "active" ] && echo "${1:-} On" || echo "${1:-} Off"
} #}}}
#------------------------#
# SHOW CAPS LOCK STATUS #
#------------------------#
_BLOCK_31_() { #{{{
[ "$(xset -q | awk '/00:/{print $4}')" == "on" ] && echo "${1:-} On" || echo "${1:-} Off"
} #}}}
#-----------------------------#
# DISPLAY X SCREENSAVER STATE #
#-----------------------------#
_BLOCK_32_() { #{{{
[ "$(xdg-screensaver status)" != "enabled" ] && echo "${1:-} Off" || echo "${1:-} On"
} #}}}
#--------------------------------#
# CALCULE MOUNT POINTS ON SYSTEM #
#--------------------------------#
_BLOCK_33_() { #{{{
echo "${1:-} $(cat /proc/mounts | grep -c "^/dev/sd*") "
} #}}}
#-------------------------#
# SHOW CMUS PLAYING SONG #
#-------------------------#
_BLOCK_34_() { #{{{
local CMUS_P=$(cmus-remote -Q | awk '/^status/{print $2}')
local CMUS_F=$(cmus-remote -Q | awk '/^file/{$1=""; print $2}')
if [ "$CMUS_P" = "Playing" ];then
echo "${1:-} $(basename $CMUS_F)"
else
echo "${1:-} Pause"
fi
} #}}}
#----------------------#
# SHOW APPARMOR STATUS #
#----------------------#
_BLOCK_35_() { #{{{
[ "$(aa-enabled)" = "Yes" ] && {
echo "${1:-} On"
}||{
echo "${1:-} Off"
}
} #}}}
#-----------------------------#
# SHOW TOTAL PACKAGES IN ARCH #
#-----------------------------#{{{
_BLOCK_36_(){
echo "${1:-} $(pacman -Q | grep -c "*")"
}
#}}}
#--------------------#
# SHOW I3 WORKSPACES #
#--------------------#
_BLOCK_37_() { #{{{
i3-msg -t get_workspaces | awk -F , '{
for(i=1;i<=NF;i++){
if($i~"name"){
gsub("\"name\":|\"","",$i)
WK=$i
}
if($i~"focused"){
gsub("\"focused\":","",$i)
WKF=$i
}
if($i~"urgent"){
gsub("\"urgent\":|\]|\}","",$i)
if($i=="false" && WKF=="true"){
printf "%s ", WK
}
if($i=="false" && WKF=="false"){
printf"[%s] ", WK
}
if($i=="true" && WKF=="false"){
printf "[*%s] ", WK
}
if($i=="true" && WKF=="true"){
printf "*%s ", WK
}
}
}
}'
} #}}}
#-----------------#
# SHOW USER NAME #
#-----------------#
_BLOCK_38_(){ #{{{
echo -e "${1:-} $USER"
} #}}}
#-------------------#
# SHOW VOLUME PULSE #
#-------------------#
_BLOCK_39_(){
local Vol=$(pactl list sinks | awk '/Volume: front/{print $5-"%"}')
local Mute=$(amixer get Master | awk '/Mono:/{gsub("\[|\]",""); print $6}')
if [ "$Mute" = "off" ];then
echo -e " --"
else
if [ "$Vol" -gt "60" ];then
echo -e " $Vol%"
elif [ "$Vol" -gt "30" ];then
echo -e " $Vol%"
elif [ "$Vol" -lt "30" ];then
echo -e " $Vol%"
fi
fi
}
#------------------------#
# USAGE AND HELP DIALOG #
#------------------------#
_USAGE_(){ #{{{
echo -e "
i3b-gate : is a all in one collaction of small script
show you useful unformation about your system
There is 39 deffirent script to use
for more information you can see the 'README' file
or visite 'https://gitlab.com/zakariagatter/i3blocks-gate'
SYNTAX :
i3b-gate [NUMBER] ...
"
} #}}}
#----------------#
# MAIN ARGUMENT #
#----------------#
if [ "$1" -gt "39" ] || [ -z "$1" ] ;then
_USAGE_
elif [ "$1" = "-h" ]; then
_USAGE_
else
eval $(echo "_BLOCK_$1_ $2 $3")
fi