Skip to content

Instantly share code, notes, and snippets.

@dimitris-k
Last active December 15, 2023 11:06
Show Gist options
  • Save dimitris-k/2363b5d027b24a96fc8b8086e5e7a691 to your computer and use it in GitHub Desktop.
Save dimitris-k/2363b5d027b24a96fc8b8086e5e7a691 to your computer and use it in GitHub Desktop.
beautify "apt list --upgradable" output
#!/bin/bash
# beautify "apt list --upgradable" output
# sample output is like:
# Listing... Done
# bind9-dnsutils/stable-security 1:9.16.27-1~deb11u1 amd64 [upgradable from: 1:9.16.22-1~deb11u1]
# bind9-host/stable-security 1:9.16.27-1~deb11u1 amd64 [upgradable from: 1:9.16.22-1~deb11u1]
# bind9-libs/stable-security 1:9.16.27-1~deb11u1 amd64 [upgradable from: 1:9.16.22-1~deb11u1]
# nodejs/unknown 16.14.2-deb-1nodesource1 amd64 [upgradable from: 16.14.1-deb-1nodesource1]
# beautified output will be like:
# bind9-dnsutils : 1:9.16.22-1~deb11u1 ---> 1:9.16.27-1~deb11u1
# bind9-libs : 1:9.16.22-1~deb11u1 ---> 1:9.16.27-1~deb11u1
# nodejs : 16.14.1-deb-1nodesource1 ---> 16.14.2-deb-1nodesource1
# bind9-host : 1:9.16.22-1~deb11u1 ---> 1:9.16.27-1~deb11u1
# Similar output can be achived by:
# apt list --upgradable 2>/dev/null | grep -v 'Listing' | column -t --table-hide 3
# The awk script calculates the width of the longest package name and the longest (existing) version name
apt list --upgradable 2>/dev/null | mawk -v FS="[/ \]]" -- '
BEGIN{
package_width=0; existing_version_width=0
# these esc sequences work for 24 bit terminals. Use some other for 8 bit ones.
package_color = "\033[38;2;234;143;255;1m"
target_color = "\033[38;2;143;220;255;1m"
color_reset = "\033[0m"
}
!/Listing/ {
existing_versions[$1] = $7
target_versions[$1] = $3
field_width = length($1)
if( field_width > package_width )
package_width = field_width
field_width = length($7)
if( field_width > existing_version_width )
existing_version_width = field_width
}
END{
for (package in existing_versions)
printf("%s%*s:%s %-*s %s-> %s%s\n",
package_color, package_width, package, color_reset,
existing_version_width, existing_versions[package],
target_color, target_versions[package], color_reset)
}
'
@dimitris-k
Copy link
Author

dimitris-k commented Aug 30, 2022

A screenshot of this script, with the default color settings.

alu_screenshot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment