Skip to content

Commit

Permalink
Merge pull request #30 from dshoreman/split-values
Browse files Browse the repository at this point in the history
Add support for custom formats with labels
  • Loading branch information
savely-krasovsky authored Aug 7, 2024
2 parents 011725a + 188a8a4 commit 1ea5b21
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 67 deletions.
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
Tiny Waybar module to check Arch Linux updates from official repositories and AUR.

## Features
- Sends notifications about updates.
- Sends (optional) notifications about updates.
- Supports GNU gettext localization (contribute new po-files!)
- Support for [custom formats](#formatting) to show only the numbers you want.
- Checks updates from AUR using Aurweb RPC, so works independently.
- Can check for development packages upstream changes (see -d [options](#command-line-options))
- Shows updates in the tooltip.
Expand All @@ -26,7 +27,7 @@ Tiny Waybar module to check Arch Linux updates from official repositories and AU
- curl
- jq
- libnotify
- git (if using --devel [option](#command-line-options))
- git (if using `--devel` [option](#command-line-options))

## Usage

Expand Down Expand Up @@ -78,11 +79,26 @@ to use localization and don't want to store them in `/usr/share/locale`.

### Command-line options
The following options are available:
- `-i, --interval`: Interval between checks (default: 6 seconds)
- `-c, --cycles`: Cycles between online checks (e.g. 6s * 600 cycles = 3600s = 1h between online checks) (default: 600 cycles)
- `-l, --packages-limit`: Maximum number of packages to be shown in notifications and tooltip (default: 10)
- `-d, --devel`: Also check for development packages upstream changes (default:disabled)
- `-n , --notify`: Turns on notifications for updates.
| Option | Value | Default | Description |
|--------|-------|---------|-------------|
| `-f`, `--format` | `string` | `{total}` | Custom format used for main output text (see [Formatting](#formatting)) |
| `-t`, `--tooltip` | `string` | | Custom tooltip format (see [Formatting](#formatting)) |
| `-i`, `--interval` | `int` | `6` | Interval (in seconds) between checks |
| `-c`, `--cycles` | `int` | `600` | Cycles between online checks (e.g. 6s *interval* * 600 *cycles* = 3600s = 1h) |
| `-l`, `--packages-limit` | `int` | `10` | Maximum number of packages to be shown in notifications and tooltip |
| `-d`, `--devel` | `none` | `off` | Also check for upstream changes in development packages |
| `-n`, `--notify` | `none` | `off` | Turns on notifications for updates |

#### Formatting

The tooltip and main text formatters can both use "labels" to format their output.

In `--tooltip`, the `{}` label will be replaced with the package list. In `--format`, it's an alias for `{total}`.

Supported custom count labels are `{aur}`, `{dev}`, `{pacman}` and `{total}`. These labels support a custom prefix and/or suffix which can be used to define icons, for example `{A :aur:\n}`, however keep in mind:
* Values **must** be separated with a colon (`:`)
* Values **may** contain newlines and tabs (`\n`, `\r` and `\t`)
* Values **cannot** contain braces (`{` or `}`),


## Localization
Expand Down
144 changes: 84 additions & 60 deletions waybar-updates
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

usage() {
echo "Usage: waybar-updates [options]
-f, --format Custom format string with one/more of {aur}, {pacman} or {total}
-t, --tooltip Custom format string for the tooltip (use {} for the update list)
-i, --interval Interval between checks
-c, --cycles Cycles between online checks (e.g. 6s * 600 cycles = 3600s = 1h between online checks)
-l, --packages-limit Maximum number of packages to be shown in notifications and tooltip
Expand All @@ -10,16 +12,29 @@ usage() {
exit 2
}

declare -A formats=()
formats[text]="{total}"
formats[tooltip]="\t\t {\t  :pacman}{\t  :aur}{\t  :dev}\n\n{}"

interval=6
cycles_number=600
packages_limit=10
devel=false
notify=false

PARSED_ARGUMENTS=$(getopt -o "hi:c:l:dn" --long "help,interval:,cycles:,packages-limit:,devel,notify" --name "waybar-updates" -- "$@")
PARSED_ARGUMENTS=$(getopt --name "waybar-updates" -o "hf:t:i:c:l:dn" --long \
"help,format:,tooltip:,interval:,cycles:,packages-limit:,devel,notify" -- "$@")
eval set -- "$PARSED_ARGUMENTS"
while :; do
case "$1" in
-f | --format)
formats[text]="$2"
shift 2
;;
-t | --tooltip)
formats[tooltip]="$2"
shift 2
;;
-i | --interval)
interval="$2"
shift 2
Expand Down Expand Up @@ -52,7 +67,7 @@ while :; do
esac
done

function check_pacman_updates() {
check_pacman_updates() {
if [ "$1" == "online" ]; then
pacman_updates=$(checkupdates --nocolor)
elif [ "$1" == "offline" ]; then
Expand All @@ -63,7 +78,7 @@ function check_pacman_updates() {
pacman_updates_count=$(echo "$pacman_updates" | grep -vc ^$)
}

function check_devel_updates() {
check_devel_updates() {
ignored_packages=$(pacman-conf IgnorePkg)
if [ "$1" == "online" ]; then
develsuffixes="git"
Expand Down Expand Up @@ -91,7 +106,7 @@ function check_devel_updates() {
devel_updates_count=$(echo "$devel_updates" | grep -vc ^$)
}

function build_devel_list() {
build_devel_list() {
custom_pkgbuild_vars="_gitname=\|_githubuser=\|_githubrepo=\|_gitcommit=\|url=\|_pkgname=\|_gitdir=\|_repo_name=\|_gitpkgname=\|source_dir=\|_name="

truncate -s 0 "$tmp_devel_packages_file"
Expand All @@ -117,7 +132,7 @@ function build_devel_list() {
done <<< "$1"
}

function check_aur_updates() {
check_aur_updates() {
ignored_packages=$(pacman-conf IgnorePkg)
if [ -n "$ignored_packages" ]; then
old_aur_packages=$(pacman -Qm | grep -vF "$ignored_packages")
Expand Down Expand Up @@ -146,7 +161,7 @@ function check_aur_updates() {
aur_updates_count=$(echo "$aur_updates" | grep -vc ^$)
}
function check_updates() {
check_updates() {
if [ "$1" == "online" ]; then
check_pacman_updates online
check_aur_updates online
Expand All @@ -163,7 +178,62 @@ function check_updates() {
fi
}
function json() {
format() {
local text format_arg="${formats[$1]}" aur_label dev_label pacman_label total_label
[ "$aur_updates_count" -gt 0 ] && aur_label="\2${aur_updates_count}\4"
[ "$devel_updates_count" -gt 0 ] && dev_label="\2${devel_updates_count}\4"
[ "$pacman_updates_count" -gt 0 ] && pacman_label="\2${pacman_updates_count}\4"
[ "$total_updates_count" -gt 0 ] && total_label="\2${total_updates_count}\4"
text="$(echo -n "${format_arg}" | tr '\n' '\r' | sed \
-e 's/{\(\([^{]\+\):\)\?aur\(:\([^}]\+\)\)\?}/'"$aur_label"'/' \
-e 's/{\(\([^{]\+\):\)\?dev\(:\([^}]\+\)\)\?}/'"$dev_label"'/' \
-e 's/{\(\([^{]\+\):\)\?pacman\(:\([^}]\+\)\)\?}/'"$pacman_label"'/' \
-e 's/{\(\([^{]\+\):\)\?total\(:\([^}]\+\)\)\?}/'"$total_label"'/' \
)"
echo -e "${text//{\}/$2}"
}
send_notification() {
local count="$3" list="$4" source="$2" text type="$1"
local fmt="%d ${type} available from ${source}"
[[ $notify == true && $count -gt 0 ]] || return
text="$(ngettext waybar-updates "$fmt" "${fmt/update/updates}" "$count")"
notify-send -a waybar-updates -u normal -i software-update-available-symbolic \
"${text//%d/$count}" "$(echo "$list" | head -n "$packages_limit")"
}
send_output() {
local source status=pending-updates text tooltip
if [[ ! $total_updates_count -gt 0 ]]; then
json "" updated "$(gettext "waybar-updates" "System is up to date")" updated
return 1
fi
for source in pacman aur devel; do
tooltip_append "${source}_updates_count" "${source}_updates"
done
json "$(format text "$total_updates_count")" "$status" "$(format \
tooltip "$(echo -e "$tooltip" | head -n "$packages_limit")" \
)" "$status"
}
tooltip_append() {
[[ ${!1} -gt 0 ]] || return
[[ -z $tooltip ]] || tooltip+="\n"
tooltip+="${!2}"
}
json() {
jq --unbuffered --null-input --compact-output \
--arg text "$1" \
--arg alt "$2" \
Expand All @@ -172,20 +242,13 @@ function json() {
'{"text": $text, "alt": $alt, "tooltip": $tooltip, "class": $class}'
}
function cleanup() {
cleanup() {
echo "Cleaning up..."
rm -f "$tmp_aur_packages_file"
rm -f "$tmp_devel_packages_file"
exit 0
}
function sendNotification() {
if ! $notify; then
return;
fi
notify-send "$@"
}
# sync at the first start
check_updates online
pacman_updates_checksum=""
Expand Down Expand Up @@ -226,51 +289,12 @@ while true; do
continue
fi
# send pushes, limit the body by 10 packages;
# also create message that will be used as tooltip
tooltip=""
if [ "$pacman_updates_count" -gt 0 ]; then
template=$(ngettext "waybar-updates" "%d update available from pacman" "%d updates available from pacman" "$pacman_updates_count")
# shellcheck disable=SC2059
sendNotification -a waybar-updates -u normal -i software-update-available-symbolic \
"$(printf "$template" "$pacman_updates_count")" \
"$(echo "$pacman_updates" | head -n "$packages_limit")"
tooltip+="$pacman_updates"
fi
if [ "$aur_updates_count" -gt 0 ]; then
template=$(ngettext "waybar-updates" "%d update available from AUR" "%d updates available from AUR" "$aur_updates_count")
# shellcheck disable=SC2059
sendNotification -a waybar-updates -u normal -i software-update-available-symbolic \
"$(printf "$template" "$aur_updates_count")" \
"$(echo "$aur_updates" | head -n "$packages_limit")"
if [ -z "$tooltip" ]; then
tooltip+="$aur_updates"
else
tooltip+="\n$aur_updates"
fi
fi
if [ $devel == true ];then
if [ "$devel_updates_count" -gt 0 ]; then
template=$(ngettext "waybar-updates" "%d devel-update available from AUR" "%d devel-updates available from AUR" "$devel_updates_count")
# shellcheck disable=SC2059
sendNotification -a waybar-updates -u normal -i software-update-available-symbolic \
"$(printf "$template" "$devel_updates_count")" \
"$(echo "$devel_updates" | head -n "$packages_limit")"
if [ -z "$tooltip" ]; then
tooltip+="$devel_updates"
else
tooltip+="\n$devel_updates"
fi
fi
fi
if [ "$total_updates_count" -gt 0 ]; then
json $total_updates_count "pending-updates" "$(echo -e "$tooltip" | head -n "$packages_limit")" "pending-updates"
else
json "" "updated" "$(gettext "waybar-updates" "System is up to date")" "updated"
fi
# Output text and tooltip then send push notifcations, limiting the body to 10 packages;
send_output && {
send_notification update pacman "$pacman_updates_count" "$pacman_updates"
send_notification update AUR "$aur_updates_count" "$aur_updates"
send_notification devel-update AUR "$devel_updates_count" "$devel_updates"
}
sleep "$interval"
done

0 comments on commit 1ea5b21

Please sign in to comment.