/
highlight.go
51 lines (43 loc) · 979 Bytes
/
highlight.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
// (c) 2020, Alex Willmer, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package logging
import (
"errors"
"fmt"
"strings"
"golang.org/x/crypto/ssh/terminal"
)
// Highlighting modes available
const (
Plain Highlight = iota
Colors
)
var errUnknownHighlight = errors.New("unknown highlight")
// Highlight mode to apply to displayed logs
type Highlight int
// ToHighlight chooses a highlighting mode
func ToHighlight(h string, fd uintptr) (Highlight, error) {
switch strings.ToUpper(h) {
case "PLAIN":
return Plain, nil
case "COLORS":
return Colors, nil
case "AUTO":
if !terminal.IsTerminal(int(fd)) {
return Plain, nil
}
return Colors, nil
default:
return Plain, fmt.Errorf("unknown highlight mode: %s", h)
}
}
func (h *Highlight) MarshalJSON() ([]byte, error) {
switch *h {
case Plain:
return []byte("\"PLAIN\""), nil
case Colors:
return []byte("\"COLORS\""), nil
default:
return nil, errUnknownHighlight
}
}