]> arthur.barton.de Git - ax-unix.git/blobdiff - lib/ax/ax-common.sh
mail-wrapper: Implement new --time [-t] option to show the duration
[ax-unix.git] / lib / ax / ax-common.sh
index e51f81616b14775ca2e5b2ad14c121f351d5da36..60f124535a34705832dd8ffa5af06b1235a62e49 100644 (file)
@@ -1,7 +1,7 @@
 #!/bin/sh
 #
 # ax-common.sh -- Common Functions for Shell Scripts
-# Copyright (c)2013-2017 Alexander Barton (alex@barton.de)
+# Copyright (c)2013-2018 Alexander Barton (alex@barton.de)
 #
 # 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
@@ -14,7 +14,7 @@
 # shellcheck disable=SC2034
 ax_common_sourced=2
 
-# Display a colored message.
+# Display a colored message (a plain message, when not writing to a terminal).
 #  $1    Level: -=title, 0=ok, 1=warning, 2=error.
 #  $2    Word(s) to highlight.
 #  $3-n  Remaining word(s). [optional]
@@ -26,8 +26,13 @@ ax_msg1() {
                "-")    c="1";  shift; ;;       # bold
                *)      c="0";
        esac
-       # print colored word(s):
-       printf "\033[0;%sm%s\033[0m " "${c}" "${1}"
+       if [ -t 1 ]; then
+               # writing to a terminal, print colored word(s):
+               printf "\\033[0;%sm%s\\033[0m " "${c}" "${1}"
+       else
+               # print plain text:
+               printf "%s " "${1}"
+       fi
        shift
        # print remaining word(s) and trailing newline:
        echo "${*}"
@@ -48,11 +53,7 @@ ax_msg() {
 ax_error() {
        if [ "$1" = "-l" ]; then
                shift
-               if [ -n "$NAME" ]; then
-                       logger -t "$NAME" "$*"
-               else
-                       logger "$*"
-               fi
+               logger -t "${NAME:-${0##*/}}" -p err "$*"
        fi
        ax_msg1 2 "$*" >&2
 }