-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodifiers.go
159 lines (143 loc) · 3.78 KB
/
modifiers.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"regexp"
ui "github.com/gizak/termui"
)
type modifierType string
const (
filter modifierType = "filter"
highlighter modifierType = "highlighter"
)
type modifiers []modifier
type modifier struct {
buffer
active bool
kind modifierType
fgColor string
bgColor string
}
func getModifierSpan(termWidth int) int {
minWidth := 17
columns := 12
return (minWidth / ((termWidth / columns) | 1)) + 1
}
func (state AppState) toggleModifier(selection int) AppState {
if selection < len(state.modifiers) {
state.modifiers[selection].active = !state.modifiers[selection].active
}
return state
}
func (mods modifiers) isEqual(mods2 modifiers) bool {
if len(mods) != len(mods2) {
return false
}
for i, mod := range mods {
if mod != mods2[i] {
return false
}
}
return true
}
func (state AppState) getModifiers() []string {
var listItems []string
listItems = append(listItems, "[Highlighters](fg-cyan,fg-underline)")
addHeader := true
for i, f := range state.modifiers {
var attrs, title string
if f.kind == filter && addHeader {
listItems = append(listItems, "", "[Filters](fg-cyan,fg-underline)")
addHeader = false
}
if f.active {
attrs = fmt.Sprintf("fg-%s,bg-%s", f.fgColor, f.bgColor)
} else {
attrs = fmt.Sprintf("fg-%s,bg-%s", f.bgColor, f.fgColor)
}
if i == state.selectedMod {
title = fmt.Sprintf("[[%d]](fg-black) [%s](%s,)", i+1, f.text, attrs)
} else {
title = fmt.Sprintf("[[%d]](fg-black) [%s](%s,)", i+1, f.text, attrs)
}
if i == state.selectedMod && (state.CurrentMode == modifierMode || state.CurrentMode == editModifier) {
title = fmt.Sprintf("[[%d]](bg-cyan,fg-black) [%s](%s)", i+1, f.text, attrs)
} else {
title = fmt.Sprintf("[[%d] %s](%s)", i+1, f.text, attrs)
}
if state.CurrentMode == editModifier && i == state.selectedMod {
title += "_"
}
listItems = append(listItems, title)
}
return listItems
}
func (mods modifiers) display(state AppState) *ui.Row {
var listItems []string
listItems = append(listItems, state.getModifiers()...)
if state.selectedMod == len(state.modifiers) {
listItems = append(listItems, "[ + Add Filter](fg-black,bg-green)")
} else {
listItems = append(listItems, "[ + Add Filter](fg-yellow)")
}
modList := ui.NewList()
modList.Overflow = "wrap"
modList.Items = listItems
modList.Height = logViewHeight(state.termHeight)
if state.CurrentMode == modifierMode || state.CurrentMode == editModifier {
modList.BorderFg = ui.ColorGreen
}
modSpan := getModifierSpan(state.termWidth)
return ui.NewCol(modSpan, 0, modList)
}
func (l lines) highlight(state AppState) lines {
if !anyActiveModifiers(state.modifiers, highlighter) {
return l
}
var highlightedLines lines
for _, line := range l {
for _, hl := range state.modifiers {
if !hl.active || hl.kind != highlighter || hl.text == "" {
continue
}
re, err := regexp.Compile(hl.text)
if err != nil {
continue
}
highlightString := func(s string) string {
return fmt.Sprintf("[%s](fg-%s,bg-%s)", s, hl.fgColor, hl.bgColor)
}
line = re.ReplaceAllStringFunc(line, highlightString)
}
highlightedLines = append(highlightedLines, line)
}
return highlightedLines
}
func (l lines) filter(state AppState) lines {
if !anyActiveModifiers(state.modifiers, filter) {
return l
}
var filteredLines lines
for i := range l {
// Go through l in reverse
line := l[len(l)-i-1]
matchFilter := false
for _, mod := range state.modifiers {
if !mod.active || (mod.kind != filter) {
continue
}
matched, err := regexp.Match(mod.text, []byte(line))
if err != nil {
continue
}
if matched {
matchFilter = true
break
}
}
if matchFilter {
// Build up lines in reverse
filteredLines = append([]string{line}, filteredLines...)
}
}
return filteredLines
}