-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (51 loc) · 1.17 KB
/
main.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
package main
import (
"bufio"
"fmt"
cliColor "github.com/gookit/color"
"github.com/lucasb-eyer/go-colorful"
"io"
"os"
"regexp"
)
type colorWord struct {
colorful.Color
//This space is intentionally left blank for future things like self-cleanup
}
var (
c map[string]colorWord
palCounter int
pal1 []colorful.Color
)
const numColors = 50
func main() {
reader := bufio.NewReader(os.Stdin)
c = make(map[string]colorWord)
pal1, _ = colorful.HappyPalette(numColors)
palCounter = 0
for {
inLine, err := reader.ReadString('\n')
if err == io.EOF {
break
}
// TODO: add command line switch(es) for different regexp
re := regexp.MustCompile(`\w?[\w-:.\/]*\w`)
outLine := re.ReplaceAllStringFunc(inLine, doColorWord)
fmt.Print(outLine)
}
// TODO: add command line switch to print stats
fmt.Printf("Stats: %v words indexed.\n", len(c))
}
func doColorWord(in string) string {
//skip short words:
if len(in) <= 0 {
return in
}
if _, found := c[in]; !found {
c[in] = colorWord{pal1[palCounter]}
palCounter = (palCounter + 1) % numColors
}
r, g, b := c[in].RGB255()
x := cliColor.NewRGBStyle(cliColor.RGB(r, g, b))
return x.Sprint(in)
}