Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 216 lines (178 sloc) 5.86 KB
#!/bin/bash
#
# pachist - A tool to show a more readable package history in Arch Linux
#
# Copyright (C) 2013-2015 Daniel Sandman <revoltism@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
version=$(awk 'END {print $1}' < <(man pachist))
## What type of packages to list
fltr_1="starting"
fltr_2="upgraded"
fltr_3="downgraded"
fltr_4="\[ALPM\].installed"
fltr_5="\[ALPM\].reinstalled"
fltr_6="\[ALPM\].removed"
fltr_7="BAJS"
init_script() {
## Create default conf if it doesn't exist
[ ! -e "$HOME/.config/pachist.conf" ] && {
touch $HOME/.config/pachist.conf
printf '# This configuration file is created automatically with\n' >> $HOME/.config/pachist.conf
printf '# default settings upon removal.\n\n' >> $HOME/.config/pachist.conf
printf '[Lines]\nlines="20"\n\n' >> $HOME/.config/pachist.conf
printf '[Colors]\ncolor1="black"\ncolor2="white"\ncolor3="blue"\n\n' >> $HOME/.config/pachist.conf
}
}
usage() {
printf "%s version %s\n\n" "$(basename $0)" "${version##pachist-}"
printf "usage: %s [options] <number of lines>\n\n" "$(basename $0)"
printf "options:\n"
printf " -d, --date Show output after date\n"
printf " -h, --help Show this help\n"
printf " -n, --num Show line numbers\n"
printf " -p, --package Show output about a specific package\n"
printf " -q, --quiet Show less information\n"
printf " -v, --verbose Show messages\n"
printf " -V, --version Show version\n"
printf " --warn Not as verbose, just warning messages\n"
printf " <number> Number of lines to show\n\n"
printf "examples:\n"
printf " %s -v 50\n" "$(basename $0)"
printf " %s --date 'yesterday'\n" "$(basename $0)"
printf " yaourt -Syua --devel --noconfirm && %s -v 50\n\n" "$(basename $0)"
printf "configuration:\n"
printf " The configuration file can be found at\n"
printf " %s/.config/%s.conf\n\n" "$HOME" "$(basename $0)"
exit 0
}
get_color_code() {
COLOR_CODE=""
case "${1,,}" in
"black") COLOR_CODE="\033[1;30m" ;;
"blue") COLOR_CODE="\033[1;34m" ;;
"green") COLOR_CODE="\033[1;32m" ;;
"cyan") COLOR_CODE="\033[1;36m" ;;
"red") COLOR_CODE="\033[1;31m" ;;
"gray") COLOR_CODE="\033[1;30m" ;;
"purple")COLOR_CODE="\033[1;35m" ;;
"yellow")COLOR_CODE="\033[1;33m" ;;
"white") COLOR_CODE="\033[1;37m" ;;
*) COLOR_CODE="\033[1;0m" ;;
esac
echo "${COLOR_CODE}"
}
get_colors() {
## fetch colors from config
col_1="$(get_color_code $(awk -F'"' '/color1/{print $2}' $HOME/.config/pachist.conf))"
col_2="$(get_color_code $(awk -F'"' '/color2/{print $2}' $HOME/.config/pachist.conf))"
col_3="$(get_color_code $(awk -F'"' '/color3/{print $2}' $HOME/.config/pachist.conf))"
}
check_args() {
PARAM=$(getopt --options d:hnp:qvV \
--longoptions date:,help,num,package:,quiet,verbose,version,warn \
--name "$0" -- "$@")
[[ $? != 0 ]] && exit 1
eval set -- "$PARAM"
while true; do
case $1 in
-d|--date) date_search="$(date +"%F" -d "${2}")"
[[ $? != 0 ]] && exit
lines="$"
shift ;;
-h|--help) usage ;;
-n|--num) num=1 ;;
-p|--package) pkg=$2
lines="$"
shift ;;
-q|--quiet) quiet=1 ;;
-v|--verbose) fltr_7="\[ALPM-SCRIPTLET\]|[Ww]arning" ;;
-V|--version) echo $version && exit 0 ;;
--warn) fltr_7="[Ww]arning" ;;
--) shift; break ;;
*) echo "pachist: error while parsing flag $1"; exit 1 ;;
esac
shift
done
for arg do
[[ $arg == *[0-9]* ]] && lines=$(echo $arg | grep -o "[0-9,]*")
done
}
filter() {
keywords="$fltr_1|$fltr_2|$fltr_3|$fltr_4|$fltr_5|$fltr_6|$fltr_7"
awk -F' ' -v quiet=$quiet -v pkg=$pkg /"($keywords)/ && /($date_search)"/'{
if (!quiet) {
info = info "\n" $0
if ($3 == "[ALPM]") {
if ($4 == "warning:" && pkg == last_row) print info
else last_row = $5
if (pkg == $5) print info
info = ""
}
if (!pkg) print $0
next
}
if ($3 == "[ALPM]" && $4 != "warning:" && !($5 in pkg_array)) {
if (pkg && pkg != $5) next
pkg_array[$5]++
print $0
}}'
}
format() {
boot_time=$(date +"%F %H:%M" -d "$(cut -f1 -d. /proc/uptime) seconds ago")
awk -F' ' -v boot_time="${boot_time}" \
-v date_search="$date_search" \
-v pkg=$pkg \
-v quiet=$quiet \
-v col_1=$col_1 \
-v col_2=$col_2 \
-v col_3=$col_3 '{
if (quiet) {
print $5
} else if ($3 == "[ALPM-SCRIPTLET]" || $4 == "warning:") {
print $1, $2, substr($0, index($0,$3)+length($3)+1)
} else if ($3 == "[ALPM]") {
red = "\033[31m"
off = "\033[1;0m"
if (!pkg && match($0, /[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}/)) {
date = substr($0, RSTART, RLENGTH)
if (!date_search && !reboot_flag && date < boot_time) {
print ""
print "[" boot_time "]" red " SYSTEM REBOOT (last)" off
print ""
reboot_flag = "1";
}
}
print col_1 $1, $2, col_2 $4, $5, col_3 substr($0, index($0,$6)) off
} else if (!pkg && substr($0, index($0,$3)) == "[PACMAN] starting full system upgrade") {
print ""
}}' |
cat -s |
sed '/./,$!d' |
([[ $num ]] && nl -ba -nln -w3 -s ' ' || cat) |
sed -n -e $rows'p' |
tac
}
## execute and show the endresult
run_pachist() {
init_script
## Default number of lines to show
lines="$(awk -F'"' '/lines/{print $2}' $HOME/.config/pachist.conf)"
check_args "$@"
get_colors
[[ $lines != *,* ]] && rows="1,$lines" || rows="$lines"
tac /var/log/pacman.log | filter | format
}
run_pachist "$@"