/
tailer.go
83 lines (68 loc) · 1.52 KB
/
tailer.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
77
78
79
80
81
82
83
package ninetail
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/ActiveState/tail"
"github.com/mattn/go-colorable"
)
var (
// red, green, yellow, magenta, cyan
ansiColorCodes = [...]int{31, 32, 33, 35, 36}
seekInfoOnStart = &tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}
colorableOutput = colorable.NewColorableStdout()
)
type Tailer struct {
*tail.Tail
colorCode int
maxWidth int
}
func NewTailers(filenames []string) []*Tailer {
maxLength := maximumNameLength(filenames)
ts := make([]*Tailer, len(filenames))
for i, filename := range filenames {
t, err := newTailer(filename, getColorCode(i), maxLength)
if err != nil {
log.Fatal(err)
}
ts[i] = t
}
return ts
}
func newTailer(filename string, colorCode int, maxWidth int) (*Tailer, error) {
t, err := tail.TailFile(filename, tail.Config{
Follow: true,
Location: seekInfoOnStart,
Logger: tail.DiscardingLogger,
})
if err != nil {
return nil, err
}
return &Tailer{
Tail: t,
colorCode: colorCode,
maxWidth: maxWidth,
}, nil
}
func (t Tailer) Do() {
for line := range t.Lines {
fmt.Fprintf(colorableOutput, "\x1b[%dm%*s\x1b[0m: %s\n", t.colorCode, t.maxWidth, t.name(), line.Text)
}
}
func (t Tailer) name() string {
return filepath.Base(t.Filename)
}
func getColorCode(index int) int {
return ansiColorCodes[index%len(ansiColorCodes)]
}
func maximumNameLength(filenames []string) int {
max := 0
for _, name := range filenames {
base := filepath.Base(name)
if len(base) > max {
max = len(base)
}
}
return max
}