Skip to content

Commit dd5782c

Browse files
up-the-hillAntiz96
andauthored
feat: Add version diff highlighting and columns to the update list (#608)
Colorize the changed suffix of version strings when displaying pending updates and add a column alignment (via `column -t`). Closes #600 --------- Co-authored-by: Robin Candau <robincandau@protonmail.com>
1 parent c889c84 commit dd5782c

1 file changed

Lines changed: 37 additions & 7 deletions

File tree

src/lib/list_packages.sh

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ info_msg "$(eval_gettext "Looking for updates...\n")"
99
# shellcheck disable=SC2154
1010
checkupdates_db_tmpdir=$(mktemp -d "${checkupdates_db_tmpdir_prefix}XXXXX")
1111
# shellcheck disable=SC2154
12-
packages=$(CHECKUPDATES_DB="${checkupdates_db_tmpdir}" timeout "${update_check_timeout}" checkupdates "${contrib_color_opt[@]}")
12+
packages=$(CHECKUPDATES_DB="${checkupdates_db_tmpdir}" timeout "${update_check_timeout}" checkupdates --nocolor)
1313
packages_exit_code=$?
1414

1515
if [ "${packages_exit_code}" -eq 124 ]; then
@@ -24,7 +24,7 @@ if [ -n "${aur_helper}" ]; then
2424
# The former because it assumes an interactive TTY environment (causing `timeout` to behave unexpectedly)
2525
# The latter because it outputs some descriptive string in stderr when looking for updates with -Qua
2626
# shellcheck disable=SC2154
27-
unformatted_aur_packages=$(timeout "${update_check_timeout}" "${aur_helper}" --color "${pacman_color_opt}" "${devel_flag[@]}" -Qua < /dev/null 2> /dev/null)
27+
unformatted_aur_packages=$(timeout "${update_check_timeout}" "${aur_helper}" --color never "${devel_flag[@]}" -Qua < /dev/null 2> /dev/null)
2828
unformatted_aur_packages_exit_code=$?
2929
aur_packages=$(echo "${unformatted_aur_packages}" | sed 's/^ *//' | sed 's/ \+/ /g' | grep -vw "\[ignored\]$")
3030

@@ -88,29 +88,59 @@ true > "${statedir}/last_updates_check_packages"
8888
true > "${statedir}/last_updates_check_aur"
8989
true > "${statedir}/last_updates_check_flatpak"
9090

91+
# Re-color update list output with version diff highlighting
92+
color_update_list() {
93+
local line pkgname oldver newver counter seg
94+
local -a old_vers new_vers
95+
96+
while IFS= read -r line; do
97+
[ -z "${line}" ] && continue
98+
read -r pkgname oldver _ newver <<< "${line}"
99+
IFS='.-' read -ra old_vers <<< "${oldver}"
100+
IFS='.-' read -ra new_vers <<< "${newver}"
101+
counter=0
102+
seg=0
103+
while [ "${seg}" -lt "${#old_vers[@]}" ] && [ "${seg}" -lt "${#new_vers[@]}" ] && [ "${old_vers[${seg}]}" = "${new_vers[${seg}]}" ]; do
104+
counter=$(( counter + ${#old_vers[${seg}]} + 1 ))
105+
seg=$(( seg + 1 ))
106+
done
107+
# shellcheck disable=SC2154
108+
printf "%b %b -> %b\n" "${bold}${pkgname}${color_off}" "${oldver:0:${counter}}${red}${bold}${oldver:${counter}}${color_off}" "${newver:0:${counter}}${green}${bold}${newver:${counter}}${color_off}"
109+
done
110+
}
111+
91112
if [ -n "${packages}" ]; then
92113
main_msg "$(eval_gettext "Packages:")"
93-
echo -e "${packages}\n"
114+
if [ -n "${no_color}" ] || [ -n "${no_version}" ]; then
115+
echo "${packages}" | column -t
116+
else
117+
echo "${packages}" | color_update_list | column -t
118+
fi
119+
echo
94120
echo "${packages}" >> "${statedir}/last_updates_check"
95121
echo "${packages}" > "${statedir}/last_updates_check_packages"
96122
fi
97123

98124
if [ -n "${aur_packages}" ]; then
99125
main_msg "$(eval_gettext "AUR Packages:")"
100-
echo -e "${aur_packages}\n"
126+
if [ -n "${no_color}" ] || [ -n "${no_version}" ]; then
127+
echo "${aur_packages}" | column -t
128+
else
129+
echo "${aur_packages}" | color_update_list | column -t
130+
fi
131+
echo
101132
echo "${aur_packages}" >> "${statedir}/last_updates_check"
102133
echo "${aur_packages}" > "${statedir}/last_updates_check_aur"
103134
fi
104135

105136
if [ "${#flatpak_packages[@]}" -gt 0 ]; then
106137
main_msg "$(eval_gettext "Flatpak Packages:")"
107-
printf "%s\n" "${flatpak_packages[@]}" ""
138+
printf "%s\n" "${flatpak_packages[@]}" | column -t
139+
echo
108140
printf "%s\n" "${flatpak_packages[@]}" >> "${statedir}/last_updates_check"
109141
printf "%s\n" "${flatpak_packages[@]}" > "${statedir}/last_updates_check_flatpak"
110142
fi
111143

112-
sed -ri 's/\x1B\[[0-9;]*m//g' "${statedir}"/last_updates_check{,_packages,_aur,_flatpak}
113-
114144
if [ -z "${packages}" ] && [ -z "${aur_packages}" ] && [ "${#flatpak_packages[@]}" -eq 0 ]; then
115145
icon_up-to-date
116146
info_msg "$(eval_gettext "No update available\n")"

0 commit comments

Comments
 (0)