-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.go
110 lines (95 loc) · 2.71 KB
/
files.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import tail "github.com/hpcloud/tail"
// Files list
type Files map[string]file
type lines []string
// file contains the lines of a given file
type file struct {
lines
numUnread int
*filteredFileSelector
}
func newFile(l lines) file {
return file{
lines: l,
filteredFileSelector: &filteredFileSelector{},
}
}
type filteredFileSelector struct {
lastLen int
lastHlAndFiltered lines
lastHLFilteredSearched lines
lastModifiers modifiers
lastSearchTerm string
lastHeight int
lastOffSet int
}
func (f file) hlAndFiltered(state AppState) lines {
filteredLines := f.lastHlAndFiltered
searchTerm := state.searchBuffer.text
if f.lastLen != len(f.lines) || (!state.modifiers.isEqual(f.lastModifiers)) {
// Copy actual modifier structs, not just the slice.
newModifiers := []modifier{}
for _, mod := range state.modifiers {
newModifiers = append(newModifiers, mod)
}
f.lastModifiers = newModifiers
filteredLines = f.lines.filter(state)
filteredLines = filteredLines.highlight(state)
f.lastHlAndFiltered = filteredLines
f.lastHLFilteredSearched = filteredLines.highlightMatches(searchTerm)
} else if f.lastSearchTerm != searchTerm || f.lastLen != len(f.lines) {
f.lastSearchTerm = searchTerm
f.lastHLFilteredSearched = f.lastHlAndFiltered.highlightMatches(searchTerm)
f.lastSearchTerm = searchTerm
}
f.lastLen = len(f.lines)
return f.lastHLFilteredSearched
}
func (state AppState) getSelectedFileName() string {
return state.LogViews[state.selected].FileName
}
func (state AppState) getSelectedView() LogView {
return state.LogViews[state.selected]
}
func (state AppState) getSelectedFile() file {
return state.Files[state.getSelectedFileName()]
}
func watchFile(fileName string, actions chan<- Action) {
addNewLine := func(fileName string, newLine string) {
actions <- AppendLine{FileName: fileName, Line: newLine}
}
go tailFile(fileName, addNewLine)
}
func addWatchers(fileNames []string, actions chan<- Action) {
for _, fileName := range fileNames {
watchFile(fileName, actions)
}
}
// AppendLine to file
type AppendLine struct {
FileName string
Line string
}
// Apply the AppendLine
func (action AppendLine) Apply(state AppState) AppState {
file := state.Files[action.FileName]
file.lines = append(file.lines, action.Line)
file.numUnread++
state.Files[action.FileName] = file
state.clearUnreadCounts()
return state
}
func tailFile(fileName string, callback func(string, string)) {
t, err := tail.TailFile(fileName, tail.Config{
Follow: true,
Logger: tail.DiscardingLogger,
MustExist: true,
})
if err != nil {
panic(err)
}
for line := range t.Lines {
callback(fileName, line.Text)
}
}