/
util.go
72 lines (65 loc) · 1.29 KB
/
util.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
65
66
67
68
69
70
71
72
// Copyright (C) 2016 Constantin Schomburg <me@cschomburg.com>
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"strings"
"github.com/fatih/color"
)
func colorize(mode, cl, text string) string {
if text == "" || cl == "" {
return text
}
if mode == "tmux" {
return colorizeTmux(cl, text)
}
c := color.Reset
switch cl {
case "black":
c = color.FgBlack
case "red":
c = color.FgRed
case "green":
c = color.FgGreen
case "yellow":
c = color.FgYellow
case "blue":
c = color.FgBlue
case "magenta":
c = color.FgMagenta
case "cyan":
c = color.FgCyan
case "white":
c = color.FgWhite
case "hi-black":
c = color.FgHiBlack
case "hi-red":
c = color.FgHiRed
case "hi-green":
c = color.FgHiGreen
case "hi-yellow":
c = color.FgHiYellow
case "hi-blue":
c = color.FgHiBlue
case "hi-magenta":
c = color.FgHiMagenta
case "hi-cyan":
c = color.FgHiCyan
case "hi-white":
c = color.FgHiWhite
}
if c != color.Reset {
text = color.New(c).SprintfFunc()(text)
}
return text
}
func colorizeTmux(cl, text string) string {
bold := ""
if strings.HasPrefix(cl, "hi-") {
bold = ",bold"
cl = strings.TrimPrefix(cl, "hi-")
}
return fmt.Sprintf("#[fg=%s]%s#[default]", cl+bold, text)
}