diff options
author | Dave Reisner <dreisner@archlinux.org> | 2011-07-01 14:31:52 +0200 |
---|---|---|
committer | Dave Reisner <dreisner@archlinux.org> | 2011-07-01 21:52:24 +0200 |
commit | ff378318f68224e2568073faa364ad6a327416e8 (patch) | |
tree | ee30cedfdeb1e3c3e0fc15b59d8c5c561a5cc842 | |
parent | f7ce8941ea55af8f2268575441030728be82d266 (diff) | |
download | mkinitcpio-ff378318f68224e2568073faa364ad6a327416e8.tar.gz mkinitcpio-ff378318f68224e2568073faa364ad6a327416e8.tar.xz |
lsinitcpio: add optional colorized output
Signed-off-by: Dave Reisner <dreisner@archlinux.org>
-rw-r--r-- | Makefile | 4 | ||||
-rwxr-xr-x | lsinitcpio | 59 |
2 files changed, 46 insertions, 17 deletions
@@ -27,7 +27,9 @@ install: all -e 's|^PRESETDIR=.*|PRESETDIR=/etc/mkinitcpio.d|' \ < mkinitcpio > ${DESTDIR}/sbin/mkinitcpio - sed "s|%VERSION%|${VERSION}|g" < lsinitcpio > ${DESTDIR}/bin/lsinitcpio + sed -e 's|\(^declare FUNCTIONS\)=.*|\1=/lib/initcpio/functions|' \ + -e 's|%VERSION%|${VERSION}|g' \ + < lsinitcpio > ${DESTDIR}/bin/lsinitcpio chmod 755 ${DESTDIR}/bin/lsinitcpio ${DESTDIR}/sbin/mkinitcpio @@ -5,11 +5,11 @@ shopt -s extglob -die() { - local mesg=$1; shift - printf "error: $mesg\n" "$@" - exit 1 -} +declare verbose= +declare list='--list' +declare -i color=1 +declare NC= BOLD= BLUE= GREEN= RED= YELLOW= +declare FUNCTIONS=functions usage() { cat<<USAGE @@ -19,6 +19,7 @@ usage: ${0##*/} [options] <initramfs> Options: -a analyze contents -h display this help + -n disable colorized output -v more verbose output -x extract image to disk @@ -39,13 +40,19 @@ decomp() { ${compress:-cat} ${compress:+-cd} "$@" } -declare verbose= -declare list='--list' +. "$FUNCTIONS" -while getopts ':ahvx' flag; do +# override the die method from functions +die() { + error "$@" + exit 1 +} + +while getopts ':ahnvx' flag; do case $flag in a) analyze=1 ;; h) usage ;; + n) color=0 ;; v) verbose='--verbose' ;; x) unset list ;; \?) die "invalid option -- '$OPTARG'" ;; @@ -55,6 +62,26 @@ shift $(( OPTIND - 1 )) declare image=$1 +if [[ -t 2 ]] && (( color )); then + # prefer terminal safe colored and bold text when tput is supported + if tput setaf 0 &>/dev/null; then + NC="$(tput sgr0)" + BOLD="$(tput bold)" + BLUE="$BOLD$(tput setaf 4)" + GREEN="$BOLD$(tput setaf 2)" + RED="$BOLD$(tput setaf 1)" + YELLOW="$BOLD$(tput setaf 3)" + else + NC="\e[1;0m" + BOLD="\e[1;1m" + BLUE="$BOLD\e[1;34m" + GREEN="$BOLD\e[1;32m" + RED="$BOLD\e[1;31m" + YELLOW="$BOLD\e[1;33m" + fi +fi +readonly NC BOLD BLUE GREEN RED YELLOW + [[ $image ]] || usage [[ -f $image ]] || die "$image: No such file" @@ -102,18 +129,18 @@ if (( analyze )); then done # print results - printf '==> Image: %s\n' "$(readlink -e "$image")" - printf '==> Kernel: %s\n' "${kernver:-unknown}" + msg 'Image: %s' "$(readlink -e "$image")" + msg 'Kernel: %s' "${kernver:-unknown}" if [[ $compress ]]; then - printf '==> Compressed with: %s\n' "$compress" - printf ' -> Compression ratio: %s\n' "$ratio" - printf ' -> Estimated decompression time: %ss\n' "$decomptime" + msg 'Compressed with: %s' "$compress" + msg2 'Compression ratio: %s' "$ratio" + msg2 'Estimated decompression time: %ss' "$decomptime" fi printf '\n' if (( ${#modules[*]} )); then - printf '==> Included modules:\n' + msg 'Included modules:' for mod in "${modules[@]}"; do printf ' %s' "$mod" in_array "${mod//_/-}" "${explicitmod[@]//_/-}" && printf ' [explicit]' @@ -122,12 +149,12 @@ if (( analyze )); then printf '\n' fi - printf '==> Included binaries:\n' + msg 'Included binaries:' printf ' %s\n' "${binaries[@]}" printf '\n' if (( ${#hooks[*]} )); then - printf '==> Hook run order:\n' + msg 'Hook run order:' printf ' %s\n' "${hooks[@]}" printf '\n' fi |