Skip to content

Commit

Permalink
Use terminal colours for direnv output
Browse files Browse the repository at this point in the history
Error messages are printed red, while expected status messages
(about what is being loaded / exported) are printed in dim black.
  • Loading branch information
timbertson committed Feb 1, 2014
1 parent d38957e commit 512a954
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cmd_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var CmdExport = &Cmd{
}
oldEnv = backupDiff.Reverse().Patch(env)
if foundRC == nil {
log("unloading")
log_status("unloading")
newEnv = oldEnv.Copy()
delete(newEnv, DIRENV_DIR)
delete(newEnv, DIRENV_MTIME)
Expand Down Expand Up @@ -111,7 +111,7 @@ var CmdExport = &Cmd{
}
sort.Strings(out)
if len(out) > 0 {
log("export %s", strings.Join(out, " "))
log_status("export %s", strings.Join(out, " "))
}
}

Expand Down
18 changes: 16 additions & 2 deletions cmd_stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ const STDLIB = `# These are the commands available in an .envrc context
set -e
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.
#
# Example:
#
# log_status "Loading ..."
#
log_status() {
echo -e "\e[30m$@\e[0m" >&2
}
# Usage: has <command>
#
# Returns 0 if the <command> is available. Returns 1 otherwise. It can be a
Expand Down Expand Up @@ -130,7 +144,7 @@ source_env() {
rcfile="$rcfile/.envrc"
rcpath="$rcpath/.envrc"
fi
echo "direnv: loading $rcfile"
log_status "direnv: loading $rcfile"
pushd "$(dirname "$rcpath")" > /dev/null
. "./$(basename "$rcpath")"
popd > /dev/null
Expand Down Expand Up @@ -304,7 +318,7 @@ layout_go() {
#
use() {
local cmd="$1"
echo "Using $@"
log_status "Using $@"
shift
use_$cmd "$@"
}
Expand Down
23 changes: 22 additions & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,28 @@ import (
"os"
)

const (
GreyCode = "\033[30m"
RedCode = "\033[31m"
GreenCode = "\033[32m"
CyanCode = "\033[36m"
PlainCode = "\033[0m"
ResetCode = "\033[0m"
)

func log(msg string, a ...interface{}) {
log_color(PlainCode, msg, a...)
}

func log_error(msg string, a ...interface{}) {
log_color(RedCode, msg, a...)
}

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

func log_color(color string, msg string, a ...interface{}) {
msg = fmt.Sprintf(msg, a...)
fmt.Fprintf(os.Stderr, "direnv: %s\n", msg)
fmt.Fprintf(os.Stderr, "%sdirenv: %s%s\n", color, msg, ResetCode)
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func main() {

err := CommandsDispatch(env, args)
if err != nil {
log("error %v", err)
log_error("error %v", err)
os.Exit(1)
}
}

0 comments on commit 512a954

Please sign in to comment.