Skip to content

Commit

Permalink
Use $DIRENV_LOG_FORMAT to control log formatting, rather than hard-co…
Browse files Browse the repository at this point in the history
…ding colors
  • Loading branch information
timbertson committed Feb 4, 2014
1 parent 512a954 commit e776e96
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
9 changes: 5 additions & 4 deletions cmd_stdlib.go
Expand Up @@ -26,15 +26,16 @@ direnv="%s"
# Usage: log_status <command>
#
# Logs a status message. Acts like echo,
# but wraps output in the standard direnv status color
# and directs it to stderr rather than stdout.
# but wraps output in the standard direnv log format
# (controlled by $DIRENV_LOG_FORMAT), and directs it
# to stderr rather than stdout.
#
# Example:
#
# log_status "Loading ..."
#
log_status() {
echo -e "\e[30m$@\e[0m" >&2
printf "${DIRENV_LOG_FORMAT:-direnv: %%s}\n" "$1" >&2
}
# Usage: has <command>
Expand Down Expand Up @@ -144,7 +145,7 @@ source_env() {
rcfile="$rcfile/.envrc"
rcpath="$rcpath/.envrc"
fi
log_status "direnv: loading $rcfile"
log_status "loading $rcfile"
pushd "$(dirname "$rcpath")" > /dev/null
. "./$(basename "$rcpath")"
popd > /dev/null
Expand Down
29 changes: 17 additions & 12 deletions log.go
Expand Up @@ -6,27 +6,32 @@ import (
)

const (
GreyCode = "\033[30m"
RedCode = "\033[31m"
GreenCode = "\033[32m"
CyanCode = "\033[36m"
PlainCode = "\033[0m"
ResetCode = "\033[0m"
defaultLogFormat = "direnv: %s"
errorLogFormat = "\033[31mdirenv: %s\033[0m"
)

func log(msg string, a ...interface{}) {
log_color(PlainCode, msg, a...)
var logFormat = ""

func formatLog(msg string) string {
if logFormat == "" {
logFormat = os.Getenv("DIRENV_LOG_FORMAT")
if logFormat == "" {
logFormat = defaultLogFormat
}
}
return fmt.Sprintf(logFormat, msg)
}

func log_error(msg string, a ...interface{}) {
log_color(RedCode, msg, a...)
msg = fmt.Sprintf(errorLogFormat, msg)
log(msg, a...)
}

func log_status(msg string, a ...interface{}) {
log_color(GreyCode, msg, a...)
log(formatLog(msg), a...)
}

func log_color(color string, msg string, a ...interface{}) {
func log(msg string, a ...interface{}) {
msg = fmt.Sprintf(msg, a...)
fmt.Fprintf(os.Stderr, "%sdirenv: %s%s\n", color, msg, ResetCode)
fmt.Fprintf(os.Stderr, "%s\n", msg)
}

0 comments on commit e776e96

Please sign in to comment.