-
Notifications
You must be signed in to change notification settings - Fork 2
/
render.go
216 lines (194 loc) · 5.4 KB
/
render.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"fmt"
"log"
"os/exec"
"strings"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
// Render sets up and renders widgets to build the user interface
func Render(content *ParsedContent, theme string) {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
content.Content = replaceTabs(content.Content)
termWidth, termHeight := ui.TerminalDimensions()
target := NewTarget(0, len(content.Rules), content.Rules)
target.SelectedRowStyle.Fg = ui.StyleParserColorMap[Config.SelectForeground]
target.SelectedRowStyle.Bg = ui.StyleParserColorMap[Config.SelectBackground]
target.Rows = getTargets(content.Rules)
highlighter := NewHighlighter(theme)
dependencyWidget := widgets.NewParagraph()
dependencyWidget.Title = "Dependencies"
dependencyWidget.Text = getDependency(content.Rules, target.Index)
contentWidget := widgets.NewParagraph()
contentWidget.Title = content.FilePath
contentWidget.Text = getContent(content.Content, highlighter, content.Rules, termHeight, target.Index)
grid := ui.NewGrid()
grid.SetRect(0, 0, termWidth, termHeight)
grid.Set(
ui.NewCol(0.2,
ui.NewRow(0.1, dependencyWidget),
ui.NewRow(0.9, target),
),
ui.NewCol(0.8, contentWidget),
)
helpWidget := widgets.NewParagraph()
helpWidget.SetRect(0, 0, termWidth, termHeight)
helpWidget.Title = "Help (press <Escape> to go back)"
helpWidget.Text =
"Target navigation:\n" +
" - j and <Down>: select below\n" +
" - k and <Up>: select above\n" +
" - gg: jump to top\n" +
" - G: jump to bottom\n" +
" - <C-d>: half page down\n" +
" - <C-u>: half page up\n" +
" - <C-f>: full page down\n" +
" - <C-b>: full page up\n" +
"\n" +
"Actions:\n" +
" - q: quit\n" +
" - <Enter>: run selected target\n" +
" - /: search for target\n"
ui.Render(grid)
uiEvents := ui.PollEvents()
previousKey := ""
quit := false
help := false
run := false
for !quit && !run {
e := <-uiEvents
if target.Search.active {
// Events if in search mode
if isLetter(e.ID) {
target.Search.AppendStringToContent(e.ID)
} else {
switch e.ID {
case "<Backspace>":
target.Search.Pop()
case "<Enter>":
// Search for target
var index int = target.FindTarget(target.Search.content)
if index != -1 {
target.ScrollAmount(index - target.Index)
}
fallthrough
case "<Escape>":
target.Search.SetActive(false)
}
}
} else {
// Events if not in search mode
switch e.ID {
case "q", "<C-c>":
quit = true
case "j", "<Down>":
target.ScrollDown()
case "k", "<Up>":
target.ScrollUp()
case "g":
if previousKey == "g" {
target.ScrollTop()
}
case "G", "<End>":
target.ScrollBottom()
case "<C-d>":
target.ScrollHalfPageDown()
case "<C-u>":
target.ScrollHalfPageUp()
case "<C-f>":
target.ScrollPageDown()
case "<C-b>":
target.ScrollPageUp()
case "/":
target.Search.SetActive(true)
case "<Enter>":
run = true
case "?":
ui.Clear()
help = !help
case "<Escape>":
ui.Clear()
help = false
}
}
if previousKey == e.ID {
previousKey = ""
} else {
previousKey = e.ID
}
// Global events
switch e.ID {
case "<Resize>":
payload := e.Payload.(ui.Resize)
grid.SetRect(0, 0, payload.Width, payload.Height)
termWidth, termHeight = ui.TerminalDimensions()
ui.Clear()
}
if help {
ui.Render(helpWidget)
} else {
target.Index = target.SelectedRow
dependencyWidget.Text = getDependency(content.Rules, target.Index)
contentWidget.Text = getContent(content.Content, highlighter, content.Rules, termHeight, target.Index)
ui.Render(grid)
}
}
ui.Close()
if run {
runTarget(target.GetName(), content.FilePath)
}
}
func getTargets(rules []Rule) []string {
// Target names are the first element in each slice in rules
if len(rules) == 0 {
// No rules were found
return []string{""}
}
var targets []string
for _, rule := range rules {
targets = append(targets, rule.target)
}
return targets
}
func getDependency(rules []Rule, index int) string {
if index < len(rules) {
return rules[index].dependencies
}
return ""
}
func getContent(content []string, highlighter *Highlighter, rules []Rule, termHeight, index int) string {
contentCopy := append([]string(nil), content...)
highlightedContent := highlighter.GetHighlightedContent(contentCopy)
firstLine := 0
if index < len(rules) {
lineNumber := rules[index].lineNumber
if len(content) > termHeight-1 {
firstLine = lineNumber
}
// Highlight rule (including commands)
highlightedContent[lineNumber] = "[" + content[lineNumber] + "](fg:" + Config.SelectForeground + ",bg:" + Config.SelectBackground + ",mod:bold)"
}
return strings.ReplaceAll(strings.Join(highlightedContent[firstLine:], "\n"), "\t", strings.Repeat(" ", 4))
}
func replaceTabs(content []string) []string {
contentCopy := append([]string(nil), content...)
for i, line := range contentCopy {
contentCopy[i] = strings.ReplaceAll(line, "\t", strings.Repeat(" ", 4))
}
return contentCopy
}
func isLetter(s string) bool {
return s[0] != '<' && s[len(s)-1] != '>'
}
func runTarget(target, filePath string) {
if target != "" {
// Compose command
cmd := exec.Command("make", "-f"+filePath, target)
// CombinedOutput will return both standard output and standard error
stdoutStderr, _ := cmd.CombinedOutput()
fmt.Print(string(stdoutStderr))
}
}