-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmsg.go
64 lines (53 loc) · 1.47 KB
/
msg.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2013-2014 Rocky Bernstein.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package repl
import (
"fmt"
"os"
"sort"
"strings"
"github.com/mgutz/ansi"
"code.google.com/p/go-columnize"
)
var termReset, termBold, termHighlight string
func init() {
termReset = ansi.ColorCode("reset")
termBold = ansi.ColorCode("+b")
termHighlight = ansi.ColorCode("+h")
}
func Errmsg(format string, a ...interface{}) (n int, err error) {
if *Highlight {
format = termHighlight + format + termReset + "\n"
} else {
format = "** " + format + "\n"
}
return fmt.Fprintf(os.Stdout, format, a...)
}
func MsgNoCr(format string, a ...interface{}) (n int, err error) {
format = format
return fmt.Fprintf(os.Stdout, format, a...)
}
func Msg(format string, a ...interface{}) (n int, err error) {
format = format + "\n"
return fmt.Fprintf(os.Stdout, format, a...)
}
// A more emphasized version of msg. For section headings.
func Section(format string, a ...interface{}) (n int, err error) {
if *Highlight {
format = termBold + format + termReset + "\n"
} else {
format = format + "\n"
}
return fmt.Fprintf(os.Stdout, format, a...)
}
func PrintSorted(title string, names []string) {
Section(title + ":")
sort.Strings(names)
opts := columnize.DefaultOptions()
opts.LinePrefix = " "
opts.DisplayWidth = Maxwidth
columnizedNames := strings.TrimRight(columnize.Columnize(names, opts),
"\n")
Msg(columnizedNames)
}