diff options
author | shellkr | 2020-05-25 00:03:17 +0200 |
---|---|---|
committer | shellkr | 2020-05-25 00:03:17 +0200 |
commit | baf73ca771d945e976c073f8e40adf04f1f59ed7 (patch) | |
tree | 2ddb963f993bc2bbc9bc96e2a4a5db8288237509 | |
parent | 0e5469836c2cb4ea8cc2f34620773fd4b1c46406 (diff) | |
download | aur-baf73ca771d945e976c073f8e40adf04f1f59ed7.tar.gz |
Added color support to verbose option
-rwxr-xr-x | pachist | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/pachist b/pachist new file mode 100755 index 000000000000..4f1a5ae46082 --- /dev/null +++ b/pachist @@ -0,0 +1,219 @@ +#!/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="yellow"\ncolor3="white"\ncolor4="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))" + col_4="$(get_color_code $(awk -F'"' '/color4/{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 ($2 == "[ALPM]") { + if ($3 == "warning:" && pkg == last_row) print info + else last_row = $4 + + if (pkg == $4) print info + info = "" + } + + if (!pkg) print $0 + + next + } + + if ($2 == "[ALPM]" && $3 != "warning:" && !($4 in pkg_array)) { + if (pkg && pkg != $4) next + + pkg_array[$4]++ + print $0 + }}' +} + +format() { + boot_time=$(date +'%FT%H:%M:%S+0100' -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 \ + -v col_4=$col_4 '{ + if (quiet) { + print $4 + } else if ($2 == "[ALPM-SCRIPTLET]" || $3 == "warning:") { + print col_1 $1, col_3 $3, substr($0, index($0,$3)+length($3)+1) + } else if ($2 == "[ALPM]") { + red = "\033[31m" + off = "\033[1;0m" + + if (!pkg && match($0, /[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}+[0-9]{4}/)) { + 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, col_2 $3, col_3 $4, col_4 substr($0, index($0,$5)) off + + } else if (!pkg && substr($0, index($0,$2)) == "[PACMAN] starting full system upgrade") { + print "---" + }}' | +# ([ "$(sed -e 's/\[//' -e 's/T.*//' <<<$1)" = "$(date +'%F')" ] && echo " > "$1 || echo "nope") | + sed -e 's/T/ /g' -e 's/+....//' $1 | + 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 "$@" |