aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/src/utils/print.go
blob: f13a2c45cd09bcefacf1e917f0944117d2064fbf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Print functions for color-coded text.
// Author: bratseth

package utils

import (
    "fmt"
    "io"
    "os"
)

// Set this to have output written somewhere else than os.Stdout
var Out io.Writer

func init() {
    Out = os.Stdout
}

// Prints in default color
func Print(messages ...string) {
    print("", messages)
}

// Prints in a color appropriate for errors
func Error(messages ...string) {
    print("\033[31m", messages)
}

// Prints in a color appropriate for success messages
func Success(messages ...string) {
    print("\033[32m", messages)
}

// Prints in a color appropriate for detail messages
func Detail(messages ...string) {
    print("\033[33m", messages)
}

func print(prefix string, messages []string) {
    fmt.Fprint(Out, prefix)
    for i := 0; i < len(messages); i++ {
        fmt.Fprint(Out, messages[i])
        if (i < len(messages) - 1) {
            fmt.Fprint(Out, " ")
        }
    }
    fmt.Fprintln(Out, "")
}