[pacman-dev] [PATCHv2 00/10] Add color to script output
This is a version two of the original pacman script color patches that I sent in. This includes three new patches, and a fix to the first patch so that I don't break `pkgver()` functions in makepkg. --- Originally, output_format.sh sent some messages to sdtout and some to stderr, but this patch set sends all messages formatted by output_format to stderr making separation of script output and child processes easy to separate. The QUIET variable can be used to get the equivalent of only printing stderr. I'm going to start working on using a flag to determine where the messages from output_format go, as per Dave's suggestion. The script patches try to implement the same color styles as makepkg, mostly by consolidating the message formats into a single library file and including that in all of the files that require it. In the pacdiff patch, I simply changed the colors, making it use a similar format as the other files. output_format also got a new function, `ask`, that looks similar to `msg2` but without a new line at the end for use with `read`. I don't know if the look is okay, I was thinking about making it look like pacman's questions, with a pair of blue colons instead of an arrow. If anyone has input on this, please speak up. The rest of the patches just implement a similar look as makepkg, using `==>` and `->` to precede messages, and colored errors and warnings. The use of a unified output_format file also affords makepkg the --quiet option, setting the `QUIET` variable to suppress messages that are not warnings, errors or output from a command in a PKGBUILD. --- William Giokas (10): script: Add color to library/output_format.sh makepkg: use output_format, add --quiet option script: add color to pacman-db-upgrade scripts: add color to pacman-key scripts: add color to pacman-optimize scripts: add color to pkgdelta scripts: add color to repo-add contrib: add colored output to bacman contrib: add color to paccache contrib: make pacdiff colors same as scripts contrib/bacman.sh.in | 62 ++++++++++++++++++++++------------------ contrib/paccache.sh.in | 19 ++++++------ contrib/pacdiff.sh.in | 35 ++++++----------------- doc/makepkg.8.txt | 4 +++ doc/pacman-key.8.txt | 3 ++ doc/repo-add.8.txt | 3 ++ scripts/library/README | 6 +++- scripts/library/output_format.sh | 23 +++++++++++---- scripts/library/term_colors.sh | 21 ++++++++++++++ scripts/makepkg.sh.in | 35 +++++------------------ scripts/pacman-db-upgrade.sh.in | 11 ++++++- scripts/pacman-key.sh.in | 8 ++++-- scripts/pacman-optimize.sh.in | 11 ++++++- scripts/pkgdelta.sh.in | 6 ++++ scripts/repo-add.sh.in | 5 ++++ 15 files changed, 149 insertions(+), 103 deletions(-) create mode 100644 scripts/library/term_colors.sh Thank you, -- William Giokas 1.8.2.rc1.24.g06d67b8
Use the same colors as makepkg when printing messages using the functions in output_format.sh. Signed-off-by: William Giokas <1007380@gmail.com> --- Originally msg and msg2 printed to stdout, but that messed with makepkgs sed expressions, and they originally printed to sdterr in makepkg, so now they print to the right place. scripts/library/README | 6 +++++- scripts/library/output_format.sh | 18 +++++++++++++----- scripts/library/term_colors.sh | 21 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 scripts/library/term_colors.sh diff --git a/scripts/library/README b/scripts/library/README index 0fa0f84..5f2961c 100644 --- a/scripts/library/README +++ b/scripts/library/README @@ -6,7 +6,7 @@ Provides basic output formatting functions with levels 'msg', 'msg2', 'warning' and 'error'. The 'msg' amd 'msg2' functions print to stdout and can be silenced by defining 'QUIET'. The 'warning' and 'error' functions print to stderr with the appropriate prefix added to the -message. +message. Also includes optional color output. parseopts.sh: A getopt_long-like parser which portably supports longopts and shortopts @@ -39,3 +39,7 @@ as mawk or busybox awk. size_to_human.sh: The reverse of human_to_size, this function takes an integer byte size and prints its in human readable format, with SI prefixes (e.g. MiB, TiB). + +term_colors.sh +Set the terminal colors for the common message formats. Uses the variable +`USE_COLOR` to determine when to be used. diff --git a/scripts/library/output_format.sh b/scripts/library/output_format.sh index 9e890e7..2930200 100644 --- a/scripts/library/output_format.sh +++ b/scripts/library/output_format.sh @@ -1,21 +1,29 @@ +# Common message formats + +plain() { + (( QUIET )) && return + local mesg=$1; shift + printf "${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 +} + msg() { (( QUIET )) && return local mesg=$1; shift - printf "==> ${mesg}\n" "$@" >&1 + printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 } msg2() { (( QUIET )) && return local mesg=$1; shift - printf " -> ${mesg}\n" "$@" >&1 + printf "${BLUE} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 } warning() { local mesg=$1; shift - printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2 + printf "${YELLOW}==> $(gettext "WARNING:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 } error() { local mesg=$1; shift - printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2 -} \ No newline at end of file + printf "${RED}==> $(gettext "ERROR:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 +} diff --git a/scripts/library/term_colors.sh b/scripts/library/term_colors.sh new file mode 100644 index 0000000..a675247 --- /dev/null +++ b/scripts/library/term_colors.sh @@ -0,0 +1,21 @@ +# check if messages are to be printed using color +unset ALL_OFF BOLD BLUE GREEN RED YELLOW +if [[ -t 2 && ! $USE_COLOR = "n" ]]; then + # prefer terminal safe colored and bold text when tput is supported + if tput setaf 0 &>/dev/null; then + ALL_OFF="$(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 + ALL_OFF="\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 ALL_OFF BOLD BLUE GREEN RED YELLOW -- 1.8.2.rc1.24.g06d67b8
On 04/03/13 04:48, William Giokas wrote:
Use the same colors as makepkg when printing messages using the functions in output_format.sh.
Signed-off-by: William Giokas <1007380@gmail.com> ---
Originally msg and msg2 printed to stdout, but that messed with makepkgs sed expressions, and they originally printed to sdterr in makepkg, so now they print to the right place.
scripts/library/README | 6 +++++- scripts/library/output_format.sh | 18 +++++++++++++----- scripts/library/term_colors.sh | 21 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 scripts/library/term_colors.sh
diff --git a/scripts/library/README b/scripts/library/README index 0fa0f84..5f2961c 100644 --- a/scripts/library/README +++ b/scripts/library/README @@ -6,7 +6,7 @@ Provides basic output formatting functions with levels 'msg', 'msg2', 'warning' and 'error'. The 'msg' amd 'msg2' functions print to stdout and can be silenced by defining 'QUIET'. The 'warning' and 'error' functions print to stderr with the appropriate prefix added to the -message. +message. Also includes optional color output.
Repeating myself, but note the documentation is right about what should point to stdout...
parseopts.sh: A getopt_long-like parser which portably supports longopts and shortopts @@ -39,3 +39,7 @@ as mawk or busybox awk. size_to_human.sh: The reverse of human_to_size, this function takes an integer byte size and prints its in human readable format, with SI prefixes (e.g. MiB, TiB). + +term_colors.sh +Set the terminal colors for the common message formats. Uses the variable +`USE_COLOR` to determine when to be used.
Is there any reason to have this separate from output_format?
diff --git a/scripts/library/output_format.sh b/scripts/library/output_format.sh index 9e890e7..2930200 100644 --- a/scripts/library/output_format.sh +++ b/scripts/library/output_format.sh @@ -1,21 +1,29 @@ +# Common message formats + +plain() { + (( QUIET )) && return + local mesg=$1; shift + printf "${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 +} + msg() { (( QUIET )) && return local mesg=$1; shift - printf "==> ${mesg}\n" "$@" >&1 + printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 }
msg2() { (( QUIET )) && return local mesg=$1; shift - printf " -> ${mesg}\n" "$@" >&1 + printf "${BLUE} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 }
warning() { local mesg=$1; shift - printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2 + printf "${YELLOW}==> $(gettext "WARNING:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 }
error() { local mesg=$1; shift - printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2 -} \ No newline at end of file + printf "${RED}==> $(gettext "ERROR:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 +} diff --git a/scripts/library/term_colors.sh b/scripts/library/term_colors.sh new file mode 100644 index 0000000..a675247 --- /dev/null +++ b/scripts/library/term_colors.sh @@ -0,0 +1,21 @@ +# check if messages are to be printed using color +unset ALL_OFF BOLD BLUE GREEN RED YELLOW +if [[ -t 2 && ! $USE_COLOR = "n" ]]; then + # prefer terminal safe colored and bold text when tput is supported + if tput setaf 0 &>/dev/null; then + ALL_OFF="$(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 + ALL_OFF="\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 ALL_OFF BOLD BLUE GREEN RED YELLOW
output_format.sh is now much the same as the original message subroutines, so just use m4_include to pull that in. This also adds a `(( QUIET )) && return` to the output format section, allowing for a --quiet switch to suppress some output. Signed-off-by: William Giokas <1007380@gmail.com> --- This patch isn't terribly important, since makepkg already prints in color, I just thought consolidating the code for printing would make more sense. doc/makepkg.8.txt | 4 ++++ scripts/makepkg.sh.in | 35 +++++++---------------------------- 2 files changed, 11 insertions(+), 28 deletions(-) diff --git a/doc/makepkg.8.txt b/doc/makepkg.8.txt index 498c79b..94343b5 100644 --- a/doc/makepkg.8.txt +++ b/doc/makepkg.8.txt @@ -118,6 +118,10 @@ Options Read the package script `buildscript` instead of the `PKGBUILD` default; see linkman:PKGBUILD[5]. +*-q, \--quiet*:: + Suppress makepkg messages. Does not change printing of commands in + PKGBUILD, error or warning messages. + *-r, \--rmdeps*:: Upon successful build, remove any dependencies installed by makepkg during dependency auto-resolution and installation when using `-s`. diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index bd29d73..a90efe5 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -79,6 +79,7 @@ CHECKFUNC=0 PKGFUNC=0 PKGVERFUNC=0 SPLITPKG=0 +QUIET=0 PKGLIST=() SIGNPKG='' @@ -92,31 +93,7 @@ shopt -s extglob ### SUBROUTINES ### -plain() { - local mesg=$1; shift - printf "${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 -} - -msg() { - local mesg=$1; shift - printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 -} - -msg2() { - local mesg=$1; shift - printf "${BLUE} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 -} - -warning() { - local mesg=$1; shift - printf "${YELLOW}==> $(gettext "WARNING:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 -} - -error() { - local mesg=$1; shift - printf "${RED}==> $(gettext "ERROR:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 -} - +m4_include(library/output_format.sh) ## # Special exit call for traps, Don't print any error messages when inside, @@ -2404,6 +2381,7 @@ usage() { printf -- "$(gettext " -m, --nocolor Disable colorized output messages")\n" printf -- "$(gettext " -o, --nobuild Download and extract files only")\n" printf -- "$(gettext " -p <file> Use an alternate build script (instead of '%s')")\n" "$BUILDSCRIPT" + printf -- "$(gettext " -q, --quiet Suppress some makepkg output")\n" printf -- "$(gettext " -r, --rmdeps Remove installed dependencies after a successful build")\n" printf -- "$(gettext " -R, --repackage Repackage contents of the package without rebuilding")\n" printf -- "$(gettext " -s, --syncdeps Install missing dependencies with %s")\n" "pacman" @@ -2454,11 +2432,11 @@ fi ARGLIST=("$@") # Parse Command Line Options. -OPT_SHORT="AcdefFghiLmop:rRsSV" +OPT_SHORT="AcdefFghiLmop:qrRsSV" OPT_LONG=('allsource' 'asroot' 'check' 'clean' 'config:' 'force' 'geninteg' 'help' 'holdver' 'ignorearch' 'install' 'key:' 'log' 'nobuild' 'nocolor' - 'nocheck' 'nodeps' 'noextract' 'nosign' 'pkg:' 'repackage' 'rmdeps' - 'skipchecksums' 'skipinteg' 'skippgpcheck' 'skippgpcheck' 'sign' + 'nocheck' 'nodeps' 'noextract' 'nosign' 'pkg:' 'quiet' 'repackage' + 'rmdeps' 'skipchecksums' 'skipinteg' 'skippgpcheck' 'skippgpcheck' 'sign' 'source' 'syncdeps' 'version') # Pacman Options @@ -2500,6 +2478,7 @@ while true; do -o|--nobuild) NOBUILD=1 ;; -p) shift; BUILDFILE=$1 ;; --pkg) shift; IFS=, read -ra p <<<"$1"; PKGLIST+=("${p[@]}"); unset p ;; + -q|--quiet) QUIET=1 ;; -r|--rmdeps) RMDEPS=1 ;; -R|--repackage) REPKG=1 ;; --skipchecksums) SKIPCHECKSUMS=1 ;; -- 1.8.2.rc1.24.g06d67b8
Use color by default, -m/--no-color turns off colors. Signed-off-by: William Giokas <1007380@gmail.com> --- scripts/pacman-db-upgrade.sh.in | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/scripts/pacman-db-upgrade.sh.in b/scripts/pacman-db-upgrade.sh.in index 1b5407a..edd41db 100644 --- a/scripts/pacman-db-upgrade.sh.in +++ b/scripts/pacman-db-upgrade.sh.in @@ -28,11 +28,13 @@ declare -r myver='@PACKAGE_VERSION@' eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf) dbroot="${DBPath:-@localstatedir@/lib/pacman/}" +USE_COLOR='y' + m4_include(library/output_format.sh) usage() { printf "pacman-db-upgrade (pacman) %s\n\n" "$myver" - printf -- "$(gettext "Usage: %s [pacman_db_root]")\n\n" "$0" + printf -- "$(gettext "Usage: %s [-m|--no-color] [pacman_db_root]")\n\n" "$0" } version() { @@ -72,6 +74,13 @@ if [[ $1 = "-V" || $1 = "--version" ]]; then exit 0 fi +if [[ $1 = "-m" || $1 = "--no-color" ]]; then + USE_COLOR='n' + shift +fi + +m4_include(library/term_colors.sh) + if [[ -n $1 ]]; then dbroot="$1" fi -- 1.8.2.rc1.24.g06d67b8
On 04/03/13 04:48, William Giokas wrote:
Use color by default, -m/--no-color turns off colors.
If you are going to follow the really bad single letter option from makepkg, you should follow the long option name too.
Signed-off-by: William Giokas <1007380@gmail.com> --- scripts/pacman-db-upgrade.sh.in | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/scripts/pacman-db-upgrade.sh.in b/scripts/pacman-db-upgrade.sh.in index 1b5407a..edd41db 100644 --- a/scripts/pacman-db-upgrade.sh.in +++ b/scripts/pacman-db-upgrade.sh.in @@ -28,11 +28,13 @@ declare -r myver='@PACKAGE_VERSION@' eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf) dbroot="${DBPath:-@localstatedir@/lib/pacman/}"
+USE_COLOR='y' + m4_include(library/output_format.sh)
usage() { printf "pacman-db-upgrade (pacman) %s\n\n" "$myver" - printf -- "$(gettext "Usage: %s [pacman_db_root]")\n\n" "$0" + printf -- "$(gettext "Usage: %s [-m|--no-color] [pacman_db_root]")\n\n" "$0" }
version() { @@ -72,6 +74,13 @@ if [[ $1 = "-V" || $1 = "--version" ]]; then exit 0 fi
+if [[ $1 = "-m" || $1 = "--no-color" ]]; then + USE_COLOR='n' + shift +fi + +m4_include(library/term_colors.sh) + if [[ -n $1 ]]; then dbroot="$1" fi
Use --no-color to suppress colored output Signed-off-by: William Giokas <1007380@gmail.com> --- doc/pacman-key.8.txt | 3 +++ scripts/pacman-key.sh.in | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/doc/pacman-key.8.txt b/doc/pacman-key.8.txt index d4a6d5c..f27c873 100644 --- a/doc/pacman-key.8.txt +++ b/doc/pacman-key.8.txt @@ -77,6 +77,9 @@ Operations Locally sign the given key. This is primarily used to root the web of trust in the local private key generated by '\--init'. +*-m, \--no-color*:: + Suppress colored output from pacman-key. + *-r, \--recv-keys*:: Equivalent to '\--recv-keys' in GnuPG. diff --git a/scripts/pacman-key.sh.in b/scripts/pacman-key.sh.in index 8f75e2f..93615ec 100644 --- a/scripts/pacman-key.sh.in +++ b/scripts/pacman-key.sh.in @@ -44,6 +44,7 @@ RECEIVE=0 REFRESH=0 UPDATEDB=0 VERIFY=0 +USE_COLOR='y' DEFAULT_KEYSERVER='hkp://pool.sks-keyservers.net' @@ -64,6 +65,7 @@ usage() { printf -- "$(gettext " -e, --export Export the specified or all keyids")\n" printf -- "$(gettext " -f, --finger List fingerprint for specified or all keyids")\n" printf -- "$(gettext " -l, --list-keys List the specified or all keys")\n" + printf -- "$(gettext " -m, --no-color Suppress colored output")\n" printf -- "$(gettext " -r, --recv-keys Fetch the specified keyids")\n" printf -- "$(gettext " -u, --updatedb Update the trustdb of pacman")\n" printf -- "$(gettext " -v, --verify Verify the file(s) specified by the signature(s)")\n" @@ -511,10 +513,10 @@ if ! type gettext &>/dev/null; then } fi -OPT_SHORT="adefhlruvV" +OPT_SHORT="adefhlmruvV" OPT_LONG=('add' 'config:' 'delete' 'edit-key' 'export' 'finger' 'gpgdir:' 'help' 'import' 'import-trustdb' 'init' 'keyserver:' 'list-keys' 'list-sigs' - 'lsign-key' 'populate' 'recv-keys' 'refresh-keys' 'updatedb' + 'lsign-key' 'no-color' 'populate' 'recv-keys' 'refresh-keys' 'updatedb' 'verify' 'version') if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then exit 1 # E_INVALID_OPTION; @@ -543,6 +545,7 @@ while (( $# )); do -l|--list-keys) LISTKEYS=1 ;; --list-sigs) LISTSIGS=1 ;; --lsign-key) LSIGNKEY=1 UPDATEDB=1 ;; + -m|--no-color) USE_COLOR='n' ;; --populate) POPULATE=1 UPDATEDB=1 ;; -r|--recv-keys) RECEIVE=1 UPDATEDB=1 ;; --refresh-keys) REFRESH=1 ;; @@ -557,6 +560,7 @@ while (( $# )); do shift done +m4_include(library/term_colors.sh) if ! type -p gpg >/dev/null; then error "$(gettext "Cannot find the %s binary required for all %s operations.")" "gpg" "pacman-key" -- 1.8.2.rc1.24.g06d67b8
Color enabled by default, turn off with -m or --no-color. Signed-off-by: William Giokas <1007380@gmail.com> --- scripts/pacman-optimize.sh.in | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/scripts/pacman-optimize.sh.in b/scripts/pacman-optimize.sh.in index 53ae1c7..262bdf8 100644 --- a/scripts/pacman-optimize.sh.in +++ b/scripts/pacman-optimize.sh.in @@ -24,6 +24,8 @@ export TEXTDOMAIN='pacman-scripts' export TEXTDOMAINDIR='@localedir@' +USE_COLOR='y' + declare -r myver='@PACKAGE_VERSION@' eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf) @@ -33,7 +35,7 @@ m4_include(library/output_format.sh) usage() { printf "pacman-optimize (pacman) %s\n\n" "$myver" - printf -- "$(gettext "Usage: %s [pacman_db_root]")\n\n" "$0" + printf -- "$(gettext "Usage: %s [-m|--no-color] [pacman_db_root]")\n\n" "$0" printf -- "$(gettext "\ pacman-optimize is a little hack that should improve the performance\n\ of pacman when reading/writing to its filesystem-based database.\n\n")" @@ -84,6 +86,13 @@ if [[ $1 = "-V" || $1 = "--version" ]]; then exit 0 fi +if [[ $1 = "-m" || $1 = "--no-color" ]]; then + USE_COLOR='n' + shift +fi + +m4_include(library/term_colors.sh) + if [[ -n $1 ]]; then dbroot="$1" fi -- 1.8.2.rc1.24.g06d67b8
Use color by default. -m and --no-color disable colorized printing. Signed-off-by: William Giokas <1007380@gmail.com> --- scripts/pkgdelta.sh.in | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scripts/pkgdelta.sh.in b/scripts/pkgdelta.sh.in index f9b40c9..36c3383 100644 --- a/scripts/pkgdelta.sh.in +++ b/scripts/pkgdelta.sh.in @@ -29,6 +29,7 @@ export TEXTDOMAINDIR='@localedir@' declare -r myver='@PACKAGE_VERSION@' QUIET=0 +USE_COLOR='y' # minimal of package before deltas are generated (bytes) min_pkg_size=$((1024*1024)) @@ -54,6 +55,7 @@ This delta file can then be added to a database using repo-add.\n\n")" echo printf -- "$(gettext "Options:\n")" printf -- "$(gettext " -q, --quiet minimize output\n")" + printf -- "$(gettext " -m, --no-color remove color from output\n")" printf -- "$(gettext " --min-pkg-size minimum package size before deltas are generated\n")" printf -- "$(gettext " --max-delta-size percent of new package above which the delta will be discarded\n")" } @@ -173,6 +175,8 @@ while :; do exit 0 ;; -q|--quiet) QUIET=1;; + -m|--no-color) + USE_COLOR='n';; --min-pkg-size) if ! min_pkg_size=$(human_to_size "$2"); then echo "invalid argument '$2' for option -- '$1'" @@ -194,6 +198,8 @@ while :; do shift done +m4_include(library/term_colors.sh) + if (( $# != 2 )); then usage exit 1 -- 1.8.2.rc1.24.g06d67b8
Use color by default, use -m or --no-color to suppress colorized output. Signed-off-by: William Giokas <1007380@gmail.com> --- doc/repo-add.8.txt | 3 +++ scripts/repo-add.sh.in | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/doc/repo-add.8.txt b/doc/repo-add.8.txt index 91f3ac6..2bb9acd 100644 --- a/doc/repo-add.8.txt +++ b/doc/repo-add.8.txt @@ -57,6 +57,9 @@ Common Options If the signature is invalid, an error is produced and the update does not proceed. +*-m, \--no-color*:: + Remove color from output of repo-add or repo-remove. + repo-add Options ---------------- *-d, \--delta*:: diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in index 3e18a1a..deee0f1 100644 --- a/scripts/repo-add.sh.in +++ b/scripts/repo-add.sh.in @@ -37,6 +37,7 @@ VERIFY=0 REPO_DB_FILE= LOCKFILE= CLEAN_LOCK=0 +USE_COLOR='y' # ensure we have a sane umask set umask 0022 @@ -71,6 +72,7 @@ packages to remove can be specified on the command line.\n")" printf -- "$(gettext "Please move along, there is nothing to see here.\n")" return fi + printf -- "$(gettext " -m, --no-color remove color from output\n")" printf -- "$(gettext " -q, --quiet minimize output\n")" printf -- "$(gettext " -s, --sign sign database with GnuPG after update\n")" printf -- "$(gettext " -k, --key <key> use the specified key to sign the database\n")" @@ -610,6 +612,7 @@ while (( $# )); do -d|--delta) DELTA=1;; -n|--new) ONLYADDNEW=1;; -f|--files) WITHFILES=1;; + -m|--no-color) USE_COLOR='n';; -s|--sign) check_gpg SIGN=1 @@ -642,6 +645,8 @@ while (( $# )); do shift done +m4_include(library/term_colors.sh) + REPO_DB_FILE=${args[0]} if [[ -z $REPO_DB_FILE ]]; then usage -- 1.8.2.rc1.24.g06d67b8
Rewrote all messages into output_format.sh formats. QUIET is only kind of supported, as some messages are multiline warning/error mesages, with every line but the first being `plain`, and those would get hidden if QUIET is set. Signed-off-by: William Giokas <1007380@gmail.com> --- contrib/bacman.sh.in | 62 ++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/contrib/bacman.sh.in b/contrib/bacman.sh.in index 512de63..6bd684a 100644 --- a/contrib/bacman.sh.in +++ b/contrib/bacman.sh.in @@ -26,13 +26,16 @@ shopt -s nullglob declare -r myname='bacman' declare -r myver='@PACKAGE_VERSION@' +USE_COLOR='y' + +m4_include(../scripts/library/output_format.sh) # # User Friendliness # usage() { echo "This program recreates a package using pacman's db and system files" - echo "Usage: $myname <installed package name>" + echo "Usage: $myname [-m|--no-color] <installed package name>" echo "Example: $myname kernel26" } @@ -41,6 +44,13 @@ version() { echo 'Copyright (C) 2008 locci <carlocci_at_gmail_dot_com>' } +if [[ $1 == "--no-color" || $1 == "-m" ]]; then + USE_COLOR='n' + shift +fi + +m4_include(../scripts/library/term_colors.sh) + if (( $# != 1 )); then usage exit 1 @@ -59,14 +69,13 @@ fi # if (( EUID )); then if [[ -f /usr/bin/fakeroot ]]; then - echo "Entering fakeroot environment" + msg "Entering fakeroot environment" export INFAKEROOT="1" /usr/bin/fakeroot -u -- "$0" "$@" exit $? else - echo "WARNING: installing fakeroot or running $myname as root is required to" - echo " preserve the ownership permissions of files in some packages" - echo "" + warning "installing fakeroot or running $myname as root is required to" + plain " preserve the ownership permissions of files in some packages\n" fi fi @@ -74,7 +83,7 @@ fi # Setting environmental variables # if [[ ! -r @sysconfdir@/pacman.conf ]]; then - echo "ERROR: unable to read @sysconfdir@/pacman.conf" + error "unable to read @sysconfdir@/pacman.conf" exit 1 fi @@ -82,7 +91,7 @@ eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf) pac_db="${DBPath:-@localstatedir@/lib/pacman/}/local" if [[ ! -r @sysconfdir@/makepkg.conf ]]; then - echo "ERROR: unable to read @sysconfdir@/makepkg.conf" + error "unable to read @sysconfdir@/makepkg.conf" exit 1 fi @@ -102,34 +111,34 @@ pkg_namver=("${pkg_dir[@]##*/}") # Checks everything is in place # if [[ ! -d $pac_db ]]; then - echo "ERROR: pacman database directory ${pac_db} not found" + error "pacman database directory ${pac_db} not found" exit 1 fi if (( ${#pkg_dir[@]} != 1 )); then - printf "ERROR: %d entries for package %s found in pacman database\n" \ + error "%d entries for package %s found in pacman database" \ ${#pkg_dir[@]} "${pkg_name}" - printf "%s\n" "${pkg_dir[@]}" + msg2 "%s" "${pkg_dir[@]}" exit 1 fi if [[ ! -d $pkg_dir ]]; then - printf "ERROR: package %s is found in pacman database,\n" "${pkg_name}" - printf " but \`%s' is not a directory\n" "${pkg_dir}" + error "package %s is found in pacman database," "${pkg_name}" + plain " but \`%s' is not a directory" "${pkg_dir}" exit 1 fi # # Begin # -echo "Package: ${pkg_namver}" +msg "Package: ${pkg_namver}" work_dir=$(mktemp -d --tmpdir bacman.XXXXXXXXXX) cd "$work_dir" || exit 1 # # File copying # -echo "Copying package files..." +msg2 "Copying package files..." cat "$pkg_dir"/files | while read i; do @@ -150,17 +159,14 @@ while read i; do # Workaround to bsdtar not reporting a missing file as an error if ! [[ -e $work_dir/$i || -L $work_dir/$i ]]; then - echo "" - echo "ERROR: unable to add /$i to the package" - echo " If your user does not have permssion to read this file then" - echo " you will need to run $myname as root" + error "unable to add /$i to the package" + plain " If your user does not have permssion to read this file then" + plain " you will need to run $myname as root" rm -rf "$work_dir" exit 1 fi else - echo "" - echo "WARNING: package file /$i is missing" - echo "" + warning "package file /$i is missing" fi ;; esac @@ -178,7 +184,7 @@ pkg_size=$(du -sk | awk '{print $1 * 1024}') # .PKGINFO stuff # TODO adopt makepkg's write_pkginfo() into this or scripts/library # -echo Generating .PKGINFO metadata... +msg2 "Generating .PKGINFO metadata..." echo "# Generated by $myname $myver" > .PKGINFO if [[ $INFAKEROOT == "1" ]]; then echo "# Using $(fakeroot -v)" >> .PKGINFO @@ -273,7 +279,7 @@ chmod 644 "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null # # Generate the package # -echo "Generating the package..." +msg2 "Generating the package..." pkg_file="$pkg_dest/$pkg_namver-$pkg_arch${PKGEXT}" ret=0 @@ -287,20 +293,20 @@ case "$PKGEXT" in *tar.xz) xz -c -z - ;; *tar.Z) compress -c -f ;; *tar) cat ;; - *) echo "WARNING: '%s' is not a valid archive extension." \ - "$PKGEXT" >&2; cat ;; + *) warning "'%s' is not a valid archive extension." \ + "$PKGEXT"; cat ;; esac > "${pkg_file}"; ret=$? if (( ret )); then - echo "ERROR: unable to write package to $pkg_dest" - echo " Maybe the disk is full or you do not have write access" + error "Unable to write package to $pkg_dest" + plain " Maybe the disk is full or you do not have write access" rm -rf "$work_dir" exit 1 fi rm -rf "$work_dir" -echo Done +msg "Done." exit 0 -- 1.8.2.rc1.24.g06d67b8
a much simpler change than bacman, as it already used 'msg' and 'error', just without the option of color. Signed-off-by: William Giokas <1007380@gmail.com> --- Note, paccache already has a -m option, so I just left it without a short opt. contrib/paccache.sh.in | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/contrib/paccache.sh.in b/contrib/paccache.sh.in index 46eb230..483f5eb 100644 --- a/contrib/paccache.sh.in +++ b/contrib/paccache.sh.in @@ -27,23 +27,16 @@ declare -a candidates=() cmdopts=() whitelist=() blacklist=() declare -i delete=0 dryrun=0 filecount=0 move=0 needsroot=0 totalsaved=0 verbose=0 declare cachedir=@localstatedir@/cache/pacman/pkg delim=$'\n' keep=3 movedir= scanarch= -msg() { - local mesg=$1; shift - printf "==> $mesg\n" "$@" -} >&2 +USE_COLOR='y' -error() { - local mesg=$1; shift - printf "==> ERROR: $mesg\n" "$@" -} >&2 +m4_include(../scripts/library/output_format.sh) +m4_include(../scripts/library/parseopts.sh) die() { error "$@" exit 1 } -m4_include(../scripts/library/parseopts.sh) - # reads a list of files on stdin and prints out deletion candidates pkgfilter() { # there's whitelist and blacklist parameters passed to this @@ -193,7 +186,7 @@ version() { OPT_SHORT=':a:c:dfhi:k:m:rsuVvz' OPT_LONG=('arch:' 'cachedir:' 'dryrun' 'force' 'help' 'ignore:' 'keep:' 'move' - 'remove' 'uninstalled' 'version' 'verbose' 'null') + 'no-color' 'remove' 'uninstalled' 'version' 'verbose' 'null') if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then exit 1 @@ -233,6 +226,8 @@ while :; do keep=$(( 10#$keep )) fi shift ;; + --no-color) + USE_COLOR='n' ;; -m|--move) move=1 movedir=$2 shift ;; @@ -256,6 +251,8 @@ while :; do shift done +m4_include(../scripts/library/term_colors.sh) + # remaining args are a whitelist whitelist=("$@") -- 1.8.2.rc1.24.g06d67b8
pacdiff already had colors, but tthey were specific to pacdiff, and had no similar style to other pacman scripts. I also added in an ask() function to the output_format.sh for use with read commands (no \n at the end). Signed-off-by: William Giokas <1007380@gmail.com> --- Note: this script does not have a --no-color option, though the original did not either, and as it is more of an interactive application, I didn't think it too big of an issue to not include this. contrib/pacdiff.sh.in | 35 +++++++++-------------------------- scripts/library/output_format.sh | 5 +++++ 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in index b41544f..47779d6 100644 --- a/contrib/pacdiff.sh.in +++ b/contrib/pacdiff.sh.in @@ -24,22 +24,11 @@ declare -r myver='@PACKAGE_VERSION@' diffprog=${DIFFPROG:-vimdiff} diffsearchpath=${DIFFSEARCHPATH:-/etc} locate=0 +USE_COLOR='y' -if tput setaf 0 &>/dev/null; then - ALL_OFF="$(tput sgr0)" - BOLD="$(tput bold)" - BLUE="${BOLD}$(tput setaf 4)" - GREEN="${BOLD}$(tput setaf 2)" - YELLOW="${BOLD}$(tput setaf 3)" - PURPLE="${BOLD}$(tput setaf 5)" -else - ALL_OFF="\e[1;0m" - BOLD="\e[1;1m" - BLUE="${BOLD}\e[1;34m" - GREEN="${BOLD}\e[1;32m" - YELLOW="${BOLD}\e[1;33m" - PURPLE="${BOLD}\e[1;35m" -fi +m4_include(../scripts/library/output_format.sh) + +m4_include(../scripts/library/term_colors.sh) usage() { echo "$myname : a simple pacnew/pacorig/pacsave updater" @@ -82,24 +71,18 @@ while IFS= read -u 3 -r -d '' pacfile; do file="${pacfile%.pac*}" file_type="pac${pacfile##*.pac}" - case $file_type in - pacnew) printf "$GREEN%s$ALL_OFF" "$file_type";; - pacorig) printf "$YELLOW%s$ALL_OFF" "$file_type";; - pacsave) printf "$BLUE%s$ALL_OFF" "$file_type";; - esac - - printf " file found for $PURPLE%s$ALL_OFF\n" "$file" + msg "%s file found for %s" "$file_type" "$file" if [ ! -f "$file" ]; then - echo " $file does not exist" + warning "$file does not exist" rm -iv "$pacfile" continue fi if cmp -s "$pacfile" "$file"; then - echo " Files are identical, removing..." + msg2 "Files are identical, removing..." rm -v "$pacfile" else - printf " (V)iew, (S)kip, (R)emove %s, (O)verwrite with %s: [v/s/r/o] " "$file_type" "$file_type" + ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s: [v/s/r/o] " "$file_type" "$file_type" while read c; do case $c in r|R) rm -v "$pacfile"; break ;; @@ -108,7 +91,7 @@ while IFS= read -u 3 -r -d '' pacfile; do $diffprog "$pacfile" "$file" rm -iv "$pacfile"; break ;; s|S) break ;; - *) printf " Invalid answer. Try again: [v/s/r/o] "; continue ;; + *) ask "Invalid answer. Try again: [v/s/r/o] "; continue ;; esac done fi diff --git a/scripts/library/output_format.sh b/scripts/library/output_format.sh index 2930200..2d5e6a3 100644 --- a/scripts/library/output_format.sh +++ b/scripts/library/output_format.sh @@ -18,6 +18,11 @@ msg2() { printf "${BLUE} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 } +ask() { + local mesg=$1; shift + printf "${YELLOW} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}" "$@" >&1 +} + warning() { local mesg=$1; shift printf "${YELLOW}==> $(gettext "WARNING:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 -- 1.8.2.rc1.24.g06d67b8
On 04/03/13 04:48, William Giokas wrote:
This is a version two of the original pacman script color patches that I sent in. This includes three new patches, and a fix to the first patch so that I don't break `pkgver()` functions in makepkg.
---
Originally, output_format.sh sent some messages to sdtout and some to stderr, but this patch set sends all messages formatted by output_format to stderr making separation of script output and child processes easy to separate. The QUIET variable can be used to get the equivalent of only printing stderr. I'm going to start working on using a flag to determine where the messages from output_format go, as per Dave's suggestion.
Just because makepkg does things wrong and prints to stderr does not mean other scripts should. Only error output should got to stderr unless there is a very good reason. The previous version was correct. makepkg has to - or at least requires more complex handling - in order to keep "makepkg -g >> PKGBUILD" working
This is a v3 of the script color patch set. I'm just sending in this request-pull output to save the mailing list from a lot of emails, and to make merging easier. I hope that the changes I made are acceptable now. The output_format output hasn't changed, they go to the same stderr/stdout as before, and I added the 'plain' function. The 'ask' function uses '::' now, to follow the style of pacman. The '-m' short option was just removed from all of the scripts that didn't have it already, and '--no-color' was changed to '--noclor'. The following changes since commit 8e2648bf02b44a7dc82429327c08cbfd2426ac30: add SYMEXPORT to alpm_filelist_contains (2013-02-24 13:11:54 +1000) are available in the git repository at: https://bitbucket.org/KaiSforza/pacman.git script-color-split-3 for you to fetch changes up to 92466b262199364837f951c4bb32b5d8563f84a3: contrib: Add color to paccache (2013-03-05 17:05:08 -0600) ---------------------------------------------------------------- William Giokas (9): scripts: Add color to library/output_format.sh scripts: Add color to pacman-db-upgrade scripts: Add color to pacman-key scripts: Add color to pacman-optimize scripts: Add color to pkgdelta scripts: Add color to repo-add contrib: Make pacdiff colors the same as makepkg contrib: Add color to bacman contrib: Add color to paccache contrib/bacman.sh.in | 62 ++++++++++++++++++++++------------------ contrib/paccache.sh.in | 20 ++++++------- contrib/pacdiff.sh.in | 35 ++++++----------------- doc/pacman-key.8.txt | 3 ++ doc/repo-add.8.txt | 3 ++ scripts/library/README | 9 ++++-- scripts/library/output_format.sh | 21 ++++++++++---- scripts/library/term_colors.sh | 21 ++++++++++++++ scripts/pacman-db-upgrade.sh.in | 11 ++++++- scripts/pacman-key.sh.in | 5 +++- scripts/pacman-optimize.sh.in | 11 ++++++- scripts/pkgdelta.sh.in | 8 +++++- scripts/repo-add.sh.in | 5 ++++ 13 files changed, 137 insertions(+), 77 deletions(-) create mode 100644 scripts/library/term_colors.sh -- William Giokas | KaiSforza GnuPG Key: 0x73CD09CF Fingerprint: F73F 50EF BBE2 9846 8306 E6B8 6902 06D8 73CD 09CF
On 06/03/13 09:18, William Giokas wrote:
This is a v3 of the script color patch set. I'm just sending in this request-pull output to save the mailing list from a lot of emails, and to make merging easier. I hope that the changes I made are acceptable now.
Great - I will get to this right after I merge the pacman color ones. Allan
participants (2)
-
Allan McRae
-
William Giokas