-
Notifications
You must be signed in to change notification settings - Fork 2
/
golor.go
76 lines (63 loc) · 1.35 KB
/
golor.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
73
74
75
76
// 256 color terminal printing in Go.
package golor
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
import (
"crypto/sha1"
"os"
)
// 16-colors
var RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, BLACK, WHITE, GRAY int
var R, G, C, M, Y, W int
var MIN, MAX int
var TERM Terminal
type Terminal interface {
// RGB returns the color code corresponding a RGB value set.
RGB(red, green, blue int) int
Colorize(s string, fg int, bg int) string
// Assign and return an unique color for this string. Automatically
// avoid black/white colors.
AssignColor(s string) int
}
func RGB(red, green, blue int) int {
return TERM.RGB(red, green, blue)
}
func Colorize(s string, fg int, bg int) string {
return TERM.Colorize(s, fg, bg)
}
func AssignColor(s string) int {
return TERM.AssignColor(s)
}
func stringId(s string, mod int) int {
h := sha1.New()
h.Write([]byte(s))
sum := 0
for _, n := range h.Sum(nil) {
sum += int(n)
}
return sum % mod
}
func init() {
switch os.Getenv("TERM") {
case "xterm-256color":
TERM = &Xterm256{}
default:
TERM = &Legacy{}
}
RED = RGB(5, 0, 0)
GREEN = RGB(0, 5, 0)
BLUE = RGB(0, 0, 5)
CYAN = RGB(0, 5, 5)
GRAY = RGB(2, 2, 2)
MAGENTA = RGB(5, 0, 5)
YELLOW = RGB(5, 5, 0)
BLACK = 0
WHITE = RGB(5, 5, 5)
R = RED
G = GREEN
C = CYAN
M = MAGENTA
Y = YELLOW
W = WHITE
MIN = RGB(0, 0, 0)
MAX = RGB(5, 5, 5)
}