-
Notifications
You must be signed in to change notification settings - Fork 8
/
console.go
1083 lines (907 loc) · 27.5 KB
/
console.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package consolesink
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"math/rand"
"os"
"sort"
"strings"
"sync"
"time"
"unicode"
"github.com/morikuni/aec"
"github.com/muesli/reflow/truncate"
"github.com/spf13/pflag"
"golang.org/x/exp/slices"
"namespacelabs.dev/foundation/internal/console/colors"
"namespacelabs.dev/foundation/internal/console/termios"
"namespacelabs.dev/foundation/internal/text/timefmt"
"namespacelabs.dev/foundation/std/tasks"
"namespacelabs.dev/foundation/std/tasks/idtypes"
)
var (
OutputActionID = false
DisplayWaitingActions = false
DebugConsoleOutput = false
DebugOutputDecisions = false
)
const (
includeToolIDs = false
maxLogSourcesAccounted = 30 // Sources of the last [num] output messages.
FPS = 60
)
var (
// These assume a black background.
ColorSticky = aec.NewRGB8Bit(0x00, 0x2b, 0xac)
ColorFade = aec.LightBlackF
ColorToolName = aec.Color8BitF(aec.NewRGB8Bit(0x30, 0x30, 0x30))
ColorToolId = aec.Color8BitF(aec.NewRGB8Bit(0x30, 0x30, 0x30))
ColorsToolBar = []aec.RGB8Bit{
// 7 items to provide for better distribution against ids.
aec.NewRGB8Bit(0x56, 0x00, 0xac),
aec.NewRGB8Bit(0x56, 0x00, 0xd7),
aec.NewRGB8Bit(0x56, 0x2b, 0xd7),
aec.NewRGB8Bit(0x56, 0x56, 0xd7),
aec.NewRGB8Bit(0x56, 0x81, 0xd7),
aec.NewRGB8Bit(0x56, 0xac, 0xd7),
aec.NewRGB8Bit(0x56, 0xd7, 0xd7),
}
StickyRequired = map[string]bool{
"webui": true,
"commands": true,
}
StickyPriorities = map[string]int{
"stack": 1,
"webui": 10,
"commands": 99,
}
DefaultStickyPriority = 50
)
var (
usBar = aec.LightCyanB.Apply(" ")
stickyBar = aec.Color8BitB(ColorSticky).Apply(" ")
toolBars []string
)
func init() {
for _, t := range ColorsToolBar {
toolBars = append(toolBars, aec.Color8BitB(t).Apply(" ")+" ")
}
}
func SetupFlags(flags *pflag.FlagSet) {
flags.BoolVar(&OutputActionID, "console_output_action_id", OutputActionID, "If set to true, each action is prefixed with its internal ID.")
flags.BoolVar(&DisplayWaitingActions, "console_display_waiting_actions", DisplayWaitingActions, "If set to true, internal waiting actions are also rendered.")
_ = flags.MarkHidden("console_output_action_id")
_ = flags.MarkHidden("console_display_waiting_actions")
}
type consoleOutput struct {
id idtypes.IdAndHash
actionID tasks.ActionID
name string
cat idtypes.CatOutputType
lines [][]byte
}
type consoleEvent struct {
output consoleOutput
setSticky struct {
name string
contents []byte
}
attachmentUpdatedForID tasks.ActionID // Set if we got an attachments updated message.
ev tasks.EventData // Set on Start() and Done().
results tasks.ResultData // Set on Done() or AttachmentsUpdated().
progress tasks.ActionProgress
renderingMode string // One of "rendering", or "input". In "input", rendering is disabled.
onInput chan struct{} // When the console enters the input mode, the console closes this channel.
}
type Atom struct {
key string
value string
result bool
}
type ConsoleSink struct {
ConsoleSinkOpts
out *os.File
outbuf *bytes.Buffer // A buffer is utilized when preparing output, to avoiding having multiple individual writes hit the console.
previousLines [][]byte // We keep a copy of the last rendered lines to avoid redrawing if the output doesn't change.
waitDone chan struct{}
ch chan consoleEvent
ticker <-chan time.Time
idling bool
buffer []consoleOutput // Pending regular log output lines.
running []*Renderable // Computed EventData for waiting/running actions.
root *node // Root of the tree of displayable events.
nodes map[tasks.ActionID]*node // Map of actionID->tree node.
startedCounting time.Time // When did we start counting.
waitForIdle []chan struct{}
stickyContent []*stickyContent // Multi-line content that is always displayed above actions.
logSources logSources // Sources of log output (think: action IDs)
debugOut *json.Encoder
}
type stickyContent struct {
name string
content [][]byte
}
type Renderable struct {
Data tasks.EventData // The original event data.
Scope []string // List of packages this line item pertains to.
Serialized []Atom // Pre-rendered arguments.
Cached bool // Whether this item represents a cache hit.
Progress tasks.ActionProgress // This is not great, as we're using memory sharing here, but keeping it simple.
}
type node struct {
item *Renderable
replacement *node // If this node is a `compute.wait`, replace it with the actual computation it is waiting on.
hidden bool // Whether this node has been marked hidden (because e.g. there are multiple nodes to the same anchor).
children []tasks.ActionID
}
type logSources struct {
mu sync.Mutex
sources []tasks.ActionID // Sources of log output (think: action IDs)
}
var _ tasks.ActionSink = &ConsoleSink{}
type RendererFunc func(io.Writer, colors.Style, time.Time, Renderable) string
type ConsoleSinkOpts struct {
Interactive bool // If set to false, only emit buffer output and logged actions.
InhibitReport bool
MaxLevel int // Only display actions at this level or below (all actions are still computed).
Renderer RendererFunc
}
func NewSink(out *os.File, opts ConsoleSinkOpts) *ConsoleSink {
return &ConsoleSink{
out: out,
ConsoleSinkOpts: opts,
outbuf: bytes.NewBuffer(make([]byte, 0, 4*1024)), // Start with 4k, enough to hold 20 lines of 100 bytes. bytes.Buffer will grow as needed.
idling: true,
}
}
func (c *ConsoleSink) Start() func() {
c.waitDone = make(chan struct{})
// We never close `ch`; the reason for that is that it would require a lot of coordination
// across all possible goroutines that can spawn actions, produce logging, etc. This
// design needs a bit of re-thinking as we do need central coordination for rendering, but
// perhaps writing to a buffer would be sufficient.
ch := make(chan consoleEvent)
c.ch = ch
interval := (1000 / FPS) * time.Millisecond
if DebugConsoleOutput {
interval = 300 * time.Millisecond
}
t := time.NewTicker(interval)
c.ticker = t.C
var out *os.File
if DebugOutputDecisions {
var err error
out, err = os.CreateTemp("", "consoledebug")
if err != nil {
panic(err)
}
c.debugOut = json.NewEncoder(out)
c.debugOut.SetIndent("", " ")
log.Println("Debugging console to", out.Name())
}
done := make(chan struct{})
go c.run(done)
return func() {
close(done)
<-c.waitDone
t.Stop()
if out != nil {
out.Close()
log.Println("Debugged console to", out.Name())
}
}
}
func (c *ConsoleSink) run(canceled chan struct{}) {
defer close(c.waitDone)
rendering := true
loop:
for {
select {
case <-canceled:
break loop
case msg := <-c.ch:
if msg.renderingMode != "" {
if msg.renderingMode == "rendering" {
rendering = true
} else {
c.waitForIdle = append(c.waitForIdle, msg.onInput)
}
}
if msg.output.lines != nil {
c.buffer = append(c.buffer, msg.output)
}
if msg.setSticky.name != "" {
found := false
for k, sc := range c.stickyContent {
if sc.name == msg.setSticky.name {
if len(msg.setSticky.contents) == 0 {
c.stickyContent = append(c.stickyContent[:k], c.stickyContent[k+1:]...)
} else {
sc.content = bytes.Split(msg.setSticky.contents, []byte("\n"))
}
found = true
break
}
}
if !found && len(msg.setSticky.contents) > 0 {
c.stickyContent = append(c.stickyContent, &stickyContent{
name: msg.setSticky.name,
content: bytes.Split(msg.setSticky.contents, []byte("\n")),
})
slices.SortStableFunc(c.stickyContent, func(a, b *stickyContent) bool {
var acost, bcost int
if p, ok := StickyPriorities[a.name]; ok {
acost = p
} else {
acost = DefaultStickyPriority
}
if p, ok := StickyPriorities[b.name]; ok {
bcost = p
} else {
bcost = DefaultStickyPriority
}
return acost < bcost
})
}
}
if msg.attachmentUpdatedForID != "" {
item := c.addOrGet(msg.attachmentUpdatedForID, false)
if item != nil {
if msg.progress != nil {
item.Progress = msg.progress
}
item.precompute(msg.results)
// recomputeTree is not required because parent/children relationships have not changed.
}
}
if msg.ev.ActionID != "" {
item := c.addOrGet(msg.ev.ActionID, true)
item.Data = msg.ev
item.Progress = msg.progress
item.precompute(msg.results)
c.recomputeTree()
}
case t := <-c.ticker:
if !rendering {
continue
}
running, waiting, anchored := c.countStates()
if running+waiting+anchored == 0 {
c.redraw(t, true) // Entering input mode - flush the state.
for _, waitingForIdle := range c.waitForIdle {
// Unblock callers waiting for the input state.
close(waitingForIdle)
}
rendering = len(c.waitForIdle) == 0
c.waitForIdle = nil
} else {
c.redraw(t, false)
}
}
}
// Flush anything that is pending.
c.redraw(time.Now(), true)
}
func (c *ConsoleSink) addOrGet(actionID tasks.ActionID, addIfMissing bool) *Renderable {
index := -1
for k, r := range c.running {
if r.Data.ActionID == actionID {
index = k
}
}
if index < 0 {
if !addIfMissing {
return nil
}
index = len(c.running)
c.running = append(c.running, &Renderable{})
}
return c.running[index]
}
func (li *Renderable) precompute(results tasks.ResultData) {
data := li.Data
var serialized []Atom
if data.AnchorID != "" {
serialized = append(serialized, Atom{key: "anchor", value: data.AnchorID.String()})
}
li.Scope = data.Scope.PackageNamesAsString()
for _, arg := range data.Arguments {
var value string
switch arg.Name {
case "cached":
if b, ok := arg.Msg.(bool); ok && b {
li.Cached = true
}
default:
if b, err := idtypes.SerializeToBytes(arg.Msg); err == nil {
value = string(b)
} else {
value = fmt.Sprintf("failed to serialize to json: %v", err)
}
}
if value != "" {
serialized = append(serialized, Atom{key: arg.Name, value: value})
}
}
for _, r := range results.Items {
var value string
if b, err := idtypes.SerializeToBytes(r.Msg); err == nil {
value = string(b)
} else {
value = fmt.Sprintf("failed to serialize to json: %v", err)
}
if value != "" {
serialized = append(serialized, Atom{key: r.Name, value: value, result: true})
}
}
li.Serialized = serialized
}
func (c *ConsoleSink) recomputeTree() {
nodes := map[tasks.ActionID]*node{}
root := &node{}
anchors := map[tasks.ActionID]bool{}
var runningCount int
for _, item := range c.running {
nodes[item.Data.ActionID] = &node{item: item}
if !item.Data.Indefinite {
runningCount++
}
}
for _, item := range c.running {
r := item.Data
parent := parentOf(root, nodes, r.ParentID)
parent.children = append(parent.children, r.ActionID)
if r.AnchorID != "" && nodes[r.AnchorID] != nil {
// We used to replace "waiting" nodes with the lines they're waiting on.
// But that turned out to be confusing when there are multiple waiters,
// because it seems like we're doing the same work N times. So now we
// only do it once.
if !anchors[r.AnchorID] {
nodes[r.ActionID].replacement = nodes[r.AnchorID]
anchors[r.AnchorID] = true
} else {
nodes[r.ActionID].hidden = true
}
}
}
// If a line item has at least one anchor, unattached from it's original root.
for anchorID := range anchors {
anchorParent := parentOf(root, nodes, nodes[anchorID].item.Data.ParentID)
anchorParent.children = without(anchorParent.children, anchorID)
}
c.root = root
c.nodes = nodes
sortNodes(nodes, root)
if runningCount > 0 {
if c.idling {
c.idling = false
c.startedCounting = time.Now()
}
} else {
c.idling = true
}
}
func without(strs []tasks.ActionID, str tasks.ActionID) []tasks.ActionID {
var newStrs []tasks.ActionID
for _, s := range strs {
if s != str {
newStrs = append(newStrs, s)
}
}
return newStrs
}
func sortNodes(nodes map[tasks.ActionID]*node, n *node) {
sort.Slice(n.children, func(i, j int) bool {
// If an action is anchored, use the anchor's start time for sorting purposes.
a := follow(nodes[n.children[i]])
b := follow(nodes[n.children[j]])
return a.item.Data.Started.Before(b.item.Data.Started)
})
for _, id := range n.children {
sortNodes(nodes, nodes[id])
}
}
func follow(n *node) *node {
if n.replacement != nil {
return n.replacement
}
return n
}
func parentOf(root *node, tree map[tasks.ActionID]*node, id tasks.ActionID) *node {
if id == "" {
return root
} else {
if p, ok := tree[id]; !ok {
// Referenced parent doesn't exist, attach to root.
return root
} else {
return p
}
}
}
type debugData struct {
Width uint
Height uint
Flush bool
Previous uint
BufferCount int
Running []debugRunning
}
type debugRunning struct {
ID tasks.ActionID
Name string
Created time.Time
State string
Completed *time.Time
}
func (c *ConsoleSink) redraw(t time.Time, flush bool) {
if !c.Interactive {
c.drawFrame(c.out, c.outbuf, t, 0, 0, flush)
return
}
var width uint
var height uint
if w, err := termios.TermSize(c.out.Fd()); err == nil {
width = uint(w.Width)
height = uint(w.Height)
}
previousLines := c.previousLines
cursorWasReset := false
resetCursorOnce := func() {
if !cursorWasReset && c.Interactive {
// Hide the cursor while re-rendering.
fmt.Fprint(c.out, aec.Hide)
if !DebugConsoleOutput {
if x := uint(len(previousLines)); x > 0 {
fmt.Fprint(c.out, aec.Up(x))
}
}
cursorWasReset = true
}
}
defer func() {
if cursorWasReset {
fmt.Fprint(c.out, aec.Show)
}
}()
rawOut := checkDirtyWriter{out: c.out, onFirstWrite: func() {
resetCursorOnce()
// If anything is trying to write directly, clear the screen first.
fmt.Fprint(c.out, aec.EraseDisplay(aec.EraseModes.Tail))
}}
c.drawFrame(&rawOut, c.outbuf, t, width, height, flush)
newFrame := make([]byte, len(c.outbuf.Bytes()))
copy(newFrame, c.outbuf.Bytes())
c.outbuf.Reset()
var newLines [][]byte
if len(newFrame) > 0 {
newLines = bytes.Split(bytes.TrimRightFunc(newFrame, unicode.IsSpace), []byte("\n"))
}
c.previousLines = newLines
// If there was no console buffer output, check if perhaps we're emitting exactly the same.
// If that's the case, then skip re-rendering, to avoid moving the cursor around.
if !rawOut.dirty && linesEqual(previousLines, newLines) {
return
}
// Only reset the cursor if there's something to render.
resetCursorOnce()
for k, line := range newLines {
if !rawOut.dirty && k < len(previousLines) && bytes.Equal(line, previousLines[k]) {
// We could look for a common prefix here, and do a narrower clear. But
// ANSI codes make this a bit complicated, as we can't easily cut a line
// in the middle of an ansi sequence. So for now, we repaint the whole
// line as needed.
fmt.Fprint(c.out, aec.Down(1))
continue
}
fmt.Fprint(c.out, aec.EraseLine(aec.EraseModes.Tail))
_, _ = c.out.Write(line)
fmt.Fprint(c.out, "\n\r")
}
// Clear any previously rendering additional lines.
fmt.Fprint(c.out, aec.EraseDisplay(aec.EraseModes.Tail))
}
func linesEqual(a, b [][]byte) bool {
if len(a) != len(b) {
return false
}
for k, aline := range a {
if !bytes.Equal(aline, b[k]) {
return false
}
}
return true
}
type checkDirtyWriter struct {
out io.Writer
dirty bool
onFirstWrite func()
}
func (c *checkDirtyWriter) Write(p []byte) (int, error) {
if !c.dirty {
c.dirty = true
if c.onFirstWrite != nil {
c.onFirstWrite()
}
}
return c.out.Write(p)
}
func (c *ConsoleSink) countStates() (running, waiting, anchored int) {
running = 0
waiting = 0
anchored = 0
for _, r := range c.running {
if r.Data.State == tasks.ActionRunning {
if !r.Data.Indefinite {
if r.Data.AnchorID != "" {
anchored++
} else {
running++
}
}
} else if r.Data.State == tasks.ActionWaiting {
waiting++
}
}
return
}
// drawFrame renders a single frame and returns `false` if further rendering should be stopped.
func (c *ConsoleSink) drawFrame(raw, out io.Writer, t time.Time, width, height uint, flush bool) {
running, waiting, anchored := c.countStates()
var completed, completedAnchors int
var printableCompleted []Renderable
for _, r := range c.running {
if r.Data.State != tasks.ActionWaiting && r.Data.State != tasks.ActionRunning {
hasError := (r.Data.Err != nil && tasks.ErrorType(r.Data.Err) == tasks.ErrTypeIsRegular)
shouldLog := tasks.LogActions && (DisplayWaitingActions || r.Data.AnchorID == "")
if (shouldLog || hasError) && r.Data.Level <= c.MaxLevel {
printableCompleted = append(printableCompleted, *r)
}
completed++
if r.Data.AnchorID != "" {
completedAnchors++
}
}
}
if tasks.LogActions && len(printableCompleted) > 0 {
sort.Slice(printableCompleted, func(i, j int) bool {
return printableCompleted[i].Data.Completed.Before(printableCompleted[j].Data.Completed)
})
for _, r := range printableCompleted {
fmt.Fprint(raw, aec.EraseLine(aec.EraseModes.Tail))
renderCompletedAction(raw, colors.WithColors, r)
}
}
// Drain any pending logging message.
var hdrBuf bytes.Buffer
for _, block := range c.buffer {
if block.name != "" && block.name != idtypes.KnownStdout && block.name != idtypes.KnownStderr {
if block.cat == idtypes.CatOutputUs {
fmt.Fprint(&hdrBuf, usBar)
} else {
colorIndex := block.id.Digest % uint64(len(toolBars))
if includeToolIDs {
fmt.Fprint(&hdrBuf, toolBars[colorIndex], ColorToolId.Apply(block.id.ID)+" "+ColorToolName.Apply(block.name))
} else {
fmt.Fprint(&hdrBuf, toolBars[colorIndex], ColorToolName.Apply(block.name))
}
}
for _, line := range block.lines {
fmt.Fprintf(raw, "%s%s %s\n", aec.EraseLine(aec.EraseModes.Tail), hdrBuf.Bytes(), line)
c.recordLogSource(block.actionID)
}
hdrBuf.Reset()
} else {
for _, line := range block.lines {
fmt.Fprintf(raw, "%s%s\n", aec.EraseLine(aec.EraseModes.Tail), line)
c.recordLogSource(block.actionID)
}
}
}
bufferCount := len(c.buffer)
c.buffer = nil
if c.debugOut != nil {
var running []debugRunning
for _, r := range c.running {
var completed *time.Time
if r.Data.State == tasks.ActionDone {
completed = &r.Data.Completed
}
running = append(running, debugRunning{
ID: r.Data.ActionID,
Name: r.Data.Name,
Created: r.Data.Created,
State: string(r.Data.State),
Completed: completed,
})
}
_ = c.debugOut.Encode(debugData{
Width: width,
Height: height,
Flush: flush,
Running: running,
BufferCount: bufferCount,
})
}
// If at least one item has completed, re-compute the display tree. This is expensive
// but kept simple for now. Will optimize later.
if completed > 0 {
var newRunning []*Renderable
for _, r := range c.running {
if r.Data.State.IsRunning() {
newRunning = append(newRunning, r)
}
}
c.running = newRunning
c.recomputeTree()
}
if !c.Interactive {
return
}
actionBlockLines, maxDepth, renderActions := c.calculateActionHeight(height)
if len(c.stickyContent) > 0 {
availableHeight := int(height-actionBlockLines) - 3 // Always leave at least 3 lines for logs.
renderBlocks, surpressed := c.fitStickies(availableHeight)
hdr := fmt.Sprintf("%s ", stickyBar)
if availableHeight > 2 {
c.writeLineWithMaxW(out, width, hdr, "")
}
for k, block := range renderBlocks {
if k > 0 && len(block.content) > 0 {
c.writeLineWithMaxW(out, width, hdr, "")
}
for _, line := range block.content {
c.writeLineWithMaxW(out, width, fmt.Sprintf("%s%s", hdr, line), "")
}
}
if len(surpressed) > 0 {
c.writeLineWithMaxW(out, width, hdr, fmt.Sprintf("[... hiding %s]", strings.Join(surpressed, ", ")))
}
if availableHeight > 2 {
c.writeLineWithMaxW(out, width, hdr, "")
}
}
if (waiting + running + anchored) == 0 {
return
}
if flush {
return
}
shortenLabel := ""
if renderActions {
// Recurse through the line item tree.
c.renderLineRec(out, width, c.root, t, 0, 0, maxDepth)
} else {
shortenLabel = "[...] "
}
if !c.InhibitReport {
report := ""
report += fmt.Sprintf("[+] %s%s", shortenLabel, timefmt.Seconds(t.Sub(c.startedCounting)))
report += fmt.Sprintf(" %s %s running", num(aec.GreenF, running), plural(running, "action", "actions"))
if waiting > 0 {
report += fmt.Sprintf(", %s waiting", num(aec.CyanF, waiting))
}
c.writeLineWithMaxW(out, width, report+".", "")
}
}
func (c *ConsoleSink) calculateActionHeight(height uint) (uint, uint, bool) {
if c.root == nil {
return 0, 0, false
}
// The height of the fixed action block.
const reportLines = 2
// The idea here is that we traverse the tree to figure out how many drawn lines would
// have been emitted. And if we see too many, we try to reduce the tree depth, until
// the number of lines is acceptable.
maxDepth, lineCount := c.maxRenderDepth(c.root, 0, 16)
if height <= reportLines {
return 0, 0, false
}
maxHeight := (height - reportLines) / 2 // Only use up to half-height of actions.
for lineCount > maxHeight {
maxDepth--
if maxDepth < 2 { // If we would go below 2, then we lose so much information may as well only render the report.
return 0, 0, false
}
_, lineCount = c.maxRenderDepth(c.root, 0, maxDepth)
}
return lineCount, maxDepth, true
}
func (c *ConsoleSink) fitStickies(availableHeight int) ([]*stickyContent, []string) {
if measureBlocksHeight(c.stickyContent) <= availableHeight {
return c.stickyContent, nil
}
var surpressed []string
var actual []*stickyContent
for _, block := range c.stickyContent {
if StickyRequired[block.name] {
actual = append(actual, block)
} else {
surpressed = append(surpressed, block.name)
}
}
if measureBlocksHeight(actual) <= availableHeight {
return actual, surpressed
}
names := make([]string, len(c.stickyContent))
for k, block := range c.stickyContent {
names[k] = block.name
}
return nil, names
}
func measureBlocksHeight(blocks []*stickyContent) int {
requiredHeight := 2 // Margin
for k, block := range blocks {
if k > 0 {
requiredHeight++
}
requiredHeight += len(block.content)
}
return requiredHeight
}
func (c *ConsoleSink) recordLogSource(id tasks.ActionID) {
c.logSources.mu.Lock()
defer c.logSources.mu.Unlock()
if len(c.logSources.sources) < maxLogSourcesAccounted {
c.logSources.sources = append(c.logSources.sources, id)
return
}
c.logSources.sources = append(c.logSources.sources[1:], id)
}
func (c *ConsoleSink) RecentInputSourcesContain(actionId tasks.ActionID) bool {
c.logSources.mu.Lock()
defer c.logSources.mu.Unlock()
for _, logSource := range c.logSources.sources {
if logSource == actionId {
return true
}
}
return false
}
func plural(count int, singular, plural string) string {
if count == 1 {
return singular
}
return plural
}
func num(c aec.ANSI, d int) string {
return c.Apply(fmt.Sprintf("%d", d))
}
func (c *ConsoleSink) maxRenderDepth(n *node, currDepth, maxDepth uint) (uint, uint) {
if currDepth >= maxDepth {
return 0, 0
}
depth := uint(0)
drawn := uint(0)
for _, id := range n.children {
child := follow(c.nodes[id])
if child.hidden {
// If hidden, don't even go through it's children.
continue
}
subDepth, subDrawn := c.maxRenderDepth(child, currDepth+1, maxDepth)
drawn += subDrawn
if !skipRendering(child.item.Data, c.MaxLevel) {
drawn++
}
subDepth++
if subDepth > depth {
depth = subDepth
}
}
return depth, drawn
}
func skipRendering(data tasks.EventData, maxLevel int) bool {
skip := data.Level > maxLevel
skip = skip || data.Indefinite
skip = skip || (!DisplayWaitingActions && data.State == tasks.ActionWaiting)
return skip
}
func (c *ConsoleSink) renderLineRec(out io.Writer, width uint, n *node, t time.Time, inputDepth int, currDepth, maxDepth uint) {
if currDepth >= maxDepth {
return
}
var lineb bytes.Buffer
for _, id := range n.children {
child := follow(c.nodes[id])
if child.hidden {
// If hidden, don't even go through it's children.
continue
}
data := child.item.Data
depth := inputDepth
if !skipRendering(data, c.MaxLevel) {
// Although this is not very efficient as we're thrashing strings, we need to make sure
// we don't print more than one line, as that would disrupt the line acount we keep track
// of to make for a smooth update in place.
// XXX precompute these lines as they don't change if the arguments don't change.
lineb.Reset()
var suffix string
if c.Renderer != nil {
suffix = c.Renderer(&lineb, colors.WithColors, t, *child.item)
} else {
fmt.Fprint(&lineb, renderPrefix(depth))
renderLine(&lineb, colors.WithColors, *child.item)
if data.State == tasks.ActionRunning {
d := t.Sub(data.Started)
suffix = " (" + timefmt.Seconds(d) + ") "
} else if data.State == tasks.ActionWaiting {
suffix = " (waiting) "
}
}
c.writeLineWithMaxW(out, width, lineb.String(), suffix)