Skip to content

Instantly share code, notes, and snippets.

@unode
Last active February 25, 2017 01:05
Show Gist options
  • Save unode/5ce418ec736d7d2b9100 to your computer and use it in GitHub Desktop.
Save unode/5ce418ec736d7d2b9100 to your computer and use it in GitHub Desktop.
AwesomeWM reusable notifications - includes text and bar. The latter useful for volume/brightness controls.
#!/usr/bin/env bash
# notify-bar.sh -- created 2015-10-11, Renato Alves
# @Last Change: 2016-04-03.
# @Revision: 0.8
#
# Code released under MIT license transcribed below:
#
# Copyright (c) 2015, Renato Alves
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
usage() {
echo >&2 ""
echo >&2 "Usage:"
echo >&2 " $0 [parameters ...] -- \"message\" max_value current_value notification_id"
echo >&2 ""
echo >&2 "Required parameters:"
echo >&2 " message = shown on the notification body"
echo >&2 " max_value = (integer) maximum possible value in the bar"
echo >&2 " current_value = (integer) current value to be represented"
echo >&2 " notification_tag = (string) keep this id constant to allow reuse of the same notification"
echo >&2 ""
echo >&2 "Optional parameters:"
echo >&2 " -t timeout = (float) time in seconds to display the notification. 0 means forever"
echo >&2 " -i icon_path = (string) path to an icon file (png is preferred)"
echo >&2 " -p preset = (string) theme presets (available: info, warn, critical)"
echo >&2 " -v = enable debugging output"
echo >&2 ""
}
repeat_char() {
if [ "$2" -ne "0" ]; then
printf -- ${1}%.0s $(seq 1 $2)
fi
}
is_integer() {
if ! [[ $1 =~ ^-?[0-9]+$ ]]; then
echo >&2 "ERROR: $1 should be an integer"
usage
exit 1
fi
}
required() {
echo >&2 "ERROR: $1 is a required argument"
usage
exit 1
}
optional() {
if [ "x$1" == "x" ]; then
echo "$2"
else
echo "$1"
fi
}
BAR_WIDTH=20
BAR_ON_SYMBOL="▌"
BAR_OFF_SYMBOL="-"
if [ $# -lt 4 ]; then
usage
exit 2
fi
# Parse args using getopt (instead of getopts) to allow arguments before options
ARGS=$(getopt -o t:i:p:v -n "$0" -- "$@")
# reorganize arguments as returned by getopt
eval set -- "$ARGS"
while true; do
case "$1" in
# Shift before to throw away option
# Shift after if option has a required positional argument
-t)
shift
TIMEOUT="$1"
shift
;;
-i)
shift
ICON="$1"
shift
;;
-p)
shift
PRESET="$(echo $1 | tr '[:lower:]' '[:upper:]')"
shift
;;
-v)
shift
VERBOSE="1"
;;
--)
shift
break
;;
esac
done
# Required arguments
HEADER="$1"
is_integer $2 "max_value" && MAX=$2
is_integer $3 "current_value" && CUR=$3
NOTIFICATION_TAG="$4"
TIMEOUT="$5"
ICON="$6"
# Default values if not specified
TIMEOUT="$(optional "$TIMEOUT" 5)"
# Ensure timeout is an integer
is_integer $TIMEOUT "timeout"
[ -n "$HEADER" ] || required "header"
[ -n "$NOTIFICATION_TAG" ] || required "notification_tag"
[ -n "$TIMEOUT" ] || TIMEOUT=""
[ -n "$ICON" ] || ICON=""
[ -n "$PRESET" ] || PRESET=""
if [ "$CUR" -gt "$MAX" ]; then
echo "ERROR: current_value cannot be larger than max_value"
usage
exit 1
fi
ON=$(expr \( $CUR \* $BAR_WIDTH \) / $MAX )
OFF=$(expr $BAR_WIDTH - $ON )
MESSAGE="$(repeat_char ${BAR_ON_SYMBOL} $ON)$(repeat_char ${BAR_OFF_SYMBOL} $OFF)"
notify-text.sh -t "$TIMEOUT" -i "$ICON" -- "$HEADER" "$MESSAGE" "$NOTIFICATION_TAG"
if [ "$VERBOSE" = "1" ]; then
# Debugging
echo "Title: $HEADER"
echo "Notify_Tag: $NOTIFICATION_TAG"
echo "Timeout: $TIMEOUT"
echo "Icon: $ICON"
echo "Preset: $PRESET"
echo "ON: $ON"
echo "OFF: $OFF"
echo "Message: $MESSAGE"
fi
#!/usr/bin/env bash
# notify-text.sh -- created 2015-10-11, Renato Alves
# @Last Change: 2015-10-16.
# @Revision: 0.5
#
# Code released under MIT license transcribed below:
#
# Copyright (c) 2015, Renato Alves
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
usage() {
echo >&2 ""
echo >&2 "Usage:"
echo >&2 " $0 [parameters ...] -- \"header\" \"message\" notification_id"
echo >&2 ""
echo >&2 "Required parameters:"
echo >&2 " header = shown on the notification header/title"
echo >&2 " message = shown on the notification body"
echo >&2 " notification_tag = (string) keep this id constant to allow reuse of the same notification"
echo >&2 ""
echo >&2 "Optional parameters:"
echo >&2 " -t timeout = (float) time in seconds to display the notification. 0 means forever"
echo >&2 " -i icon_path = (string) path to an icon file (png is preferred)"
echo >&2 " -p preset = (string) theme presets (available: info, warn, critical)"
echo >&2 " -v = enable debugging output"
echo >&2 ""
}
is_integer() {
if ! [[ $1 =~ ^-?[0-9]+$ ]]; then
echo >&2 "ERROR: $1 should be an integer"
usage
exit 1
fi
}
required() {
echo >&2 "ERROR: $1 is a required argument"
usage
exit 1
}
optional() {
if [ "x$1" == "x" ]; then
echo "$2"
else
echo "$1"
fi
}
# Parse args using getopt (instead of getopts) to allow arguments before options
ARGS=$(getopt -o t:i:p:v -n "$0" -- "$@")
# reorganize arguments as returned by getopt
eval set -- "$ARGS"
while true; do
case "$1" in
# Shift before to throw away option
# Shift after if option has a required positional argument
-t)
shift
TIMEOUT="$1"
shift
;;
-i)
shift
ICON="$1"
shift
;;
-p)
shift
PRESET="$(echo $1 | tr '[:lower:]' '[:upper:]')"
shift
;;
-v)
shift
VERBOSE="1"
;;
--)
shift
break
;;
esac
done
# Escaping raw strings in lua. As long as the content doesn't include these
# patterns we are be safe
OPEN_ESC="\[==\["
CLOSE_ESC="\]==\]"
sanitize() {
# Escapes lua raw string characters to avoid terminating the raw string prematurely
# Also make \n actually behave like newlines
echo "$1" | sed -e ':loop' -e "s:${OPEN_ESC}:[=[:g" -e "s:${CLOSE_ESC}:]=]:g" -e 't loop' -e 's:\\n:
:g'
}
# Required arguments
HEADER=$(sanitize "$1")
MESSAGE=$(sanitize "$2")
NOTIFICATION_TAG="$3"
# Default values if not specified
TIMEOUT="$(optional "$TIMEOUT" 5)"
# Ensure timeout is an integer
is_integer $TIMEOUT "timeout"
# Themed presets
NORMAL="" # Whatever is the theme default
INFO="bg=\"#0000aa\","
WARN="bg=\"#880000\","
CRITICAL="bg=\"#ff0000\","
[ -n "$HEADER" ] && HEADER="title=${OPEN_ESC}${HEADER}${CLOSE_ESC}," || required "header"
[ -n "$MESSAGE" ] && MESSAGE="text=${OPEN_ESC}${MESSAGE}${CLOSE_ESC}," || required "message"
[ -n "$NOTIFICATION_TAG" ] || required "notification_tag"
[ -n "$TIMEOUT" ] && TIMEOUT="timeout=$TIMEOUT," || TIMEOUT=""
[ -n "$ICON" ] && ICON="icon=${OPEN_ESC}${ICON}${CLOSE_ESC}," || ICON=""
[ -n "$PRESET" ] && PRESET="${!PRESET}" || PRESET=""
# Also clear notification_tags that have expired (1 hour = 3600 seconds) but
# only if a sufficient number of tags is created.
read -d '' PAYLOAD << EOF
if notifications_tags == nil then
notifications_tags = {}
notifications_tags_count = 0
update_notification_tag_timestamp = function(new_id, notify_tag)
notifications_tags[notify_tag] = {
id=new_id,
timestamp=os.time(),
}
end
end
local notify_tag = "$NOTIFICATION_TAG"
if notifications_tags[notify_tag] == nil then
update_notification_tag_timestamp(
naughty.notify({ $HEADER $MESSAGE $TIMEOUT $ICON $PRESET }).id,
notify_tag
)
notifications_tags_count = notifications_tags_count + 1
else
update_notification_tag_timestamp(
naughty.notify({ $HEADER $MESSAGE $TIMEOUT $ICON $PRESET
replaces_id=notifications_tags[notify_tag].id }).id,
notify_tag
)
end
if notifications_tags_count >= 20 then
notifications_tags_count = 0
for k,v in pairs(notifications_tags) do
local delta = os.time() - v.timestamp
if delta > 3600 then
notifications_tags[k] = nil
else
notifications_tags_count = notifications_tags_count + 1
end
end
end
EOF
echo "$PAYLOAD" | awesome-client
if [ "$VERBOSE" = "1" ]; then
# Debugging
echo "Title: $HEADER"
echo "Message: $MESSAGE"
echo "Notify_Tag: $NOTIFICATION_TAG"
echo "Timeout: $TIMEOUT"
echo "Icon: $ICON"
echo "Preset: $PRESET"
echo "Payload:
$PAYLOAD"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment