-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-devcard.go
195 lines (174 loc) · 4.88 KB
/
client-devcard.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
package server
import (
"context"
"fmt"
"html"
"slices"
"sync"
"time"
"github.com/igorhub/devcard"
"github.com/igorhub/devcard/pkg/internal/project"
)
func (c *client) navigationBlock() string {
var prevLink, upLink, nextLink string
upLink = fmt.Sprintf("[top: %s](/dc/%s?jump=%s)", c.repo.DevcardInfo.Package, c.project.Name, pkgId(c.repo.DevcardInfo))
cardsInfo := c.project.DevcardsInfo()
cardsInfo = slices.DeleteFunc(cardsInfo, func(info devcard.DevcardInfo) bool {
return info.ImportPath != c.repo.DevcardInfo.ImportPath
})
if len(cardsInfo) < 2 {
return MdToHTML("❬ " + upLink + " ❭")
}
prevIdx, nextIdx := len(cardsInfo)-1, 0
for i := range cardsInfo {
if i > 0 && cardsInfo[i-1].Name == c.repo.DevcardInfo.Name {
nextIdx = i
}
if i < len(cardsInfo)-1 && cardsInfo[i+1].Name == c.repo.DevcardInfo.Name {
prevIdx = i
}
}
prevLink = fmt.Sprintf("[prev: %s](%s)", cardsInfo[prevIdx].Caption(), cardsInfo[prevIdx].Name)
nextLink = fmt.Sprintf("[next: %s](%s)", cardsInfo[nextIdx].Caption(), cardsInfo[nextIdx].Name)
return MdToHTML("❬ " + prevLink + " | " + upLink + " | " + nextLink + " ❭")
}
func (c *client) initDevcard(devcardName string, unregisterFn func()) {
c.unregisterFn = unregisterFn
var err error
c.repo, err = c.project.CreateRepo(devcardName)
if err != nil {
c.initError(err, unregisterFn)
return
}
title := c.repo.DevcardInfo.Caption()
c.updateFn = func(ctx context.Context, ch chan<- []byte) {
ch <- msgSetTitle(title)
ch <- msgSetCellContent("-devcard-navigation", c.navigationBlock())
var (
buildTime int64
runTime int64 = -1
failedTests int
)
setBadges := func() {
var badges []string
badges = append(badges, "<code>build: "+formatTime(buildTime)+"</code>")
if runTime >= 0 {
badges = append(badges, "<code>run: "+formatTime(runTime)+"</code>")
}
if failedTests == 1 {
badges = append(badges, `<code class="err">1 test failed</code>`)
} else if failedTests > 1 {
badges = append(badges, fmt.Sprintf(`<code class="err">%d tests failed</code>`, failedTests))
}
ch <- msgSetStatusBarContent(badges...)
}
wg := sync.WaitGroup{}
wg.Add(2)
buildC := make(chan struct{})
go func() {
defer wg.Done()
init := time.Now()
for {
select {
case <-buildC:
return
case <-ctx.Done():
return
default:
}
buildTime = (time.Since(init).Milliseconds() / 100) * 100
setBadges()
time.Sleep(100 * time.Millisecond)
}
}()
runC := make(chan struct{})
go func() {
defer wg.Done()
<-buildC
init := time.Now()
for {
select {
case <-runC:
return
case <-ctx.Done():
return
default:
}
runTime = (time.Since(init).Milliseconds() / 100) * 100
setBadges()
time.Sleep(100 * time.Millisecond)
}
}()
control := make(chan string)
updates := make(chan project.UpdateMessage, 4096)
go c.repo.Run(ctx, control, updates)
go func() {
ch <- msgClear()
init := time.Now()
<-updates
close(buildC)
buildTime = time.Since(init).Milliseconds()
init = time.Now()
processUpdates(c, ch, control, updates)
close(runC)
runTime = time.Since(init).Milliseconds()
failedTests = c.repo.Test(ctx)
setBadges()
wg.Wait()
close(ch)
}()
}
}
func processUpdates(c *client, ch chan<- []byte, control chan<- string, updates <-chan project.UpdateMessage) {
var stdoutCellCreated, stderrCellCreated bool
var wg sync.WaitGroup
defer wg.Wait()
for update := range updates {
switch x := update.(type) {
case project.MsgInfo:
if x.Title != "" {
ch <- msgSetTitle(x.Title)
}
case project.MsgCell:
ch <- msgAppendCell(x.Id, renderCell(c.project, c.highlighter, x.Cell))
if cell, ok := x.Cell.(*devcard.JumpCell); ok {
wg.Add(1)
go func(cell *devcard.JumpCell) {
time.Sleep(time.Duration(cell.Delay) * time.Millisecond)
ch <- msgJump(x.Id)
wg.Done()
}(cell)
}
case project.MsgError:
b := devcard.NewErrorCell(x.Title)
if x.Err != nil {
b.Append(x.Err)
}
ch <- msgAppendCell(fmt.Sprintf("%p", &x), renderCell(c.project, c.highlighter, b))
case project.MsgPipeOut:
switch x.Pipe {
case project.PipeStdout:
if !stdoutCellCreated {
stdoutCellCreated = true
s := `<h3>Stdout:</h3><pre id="-devcard-stdout-cell"></pre>`
ch <- msgSetCellContent("-devcard-stdout", s)
}
ch <- msgAppendToCell("-devcard-stdout-cell", html.EscapeString(x.Line))
case project.PipeStderr:
if !stderrCellCreated {
stderrCellCreated = true
s := `<h3 class="err">Stderr:</h3><pre id="-devcard-stderr-cell"></pre>`
ch <- msgSetCellContent("-devcard-stderr", s)
}
ch <- msgAppendToCell("-devcard-stderr-cell", html.EscapeString(x.Line))
}
}
}
}
func formatTime(duration int64) string {
if duration < 1000 {
return fmt.Sprintf("%dms", duration)
} else {
return fmt.Sprintf("%.1fs", float64(duration)/1000)
}
}