-
Notifications
You must be signed in to change notification settings - Fork 303
/
terminal_stream.go
59 lines (46 loc) · 1.36 KB
/
terminal_stream.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
package hud
import (
"context"
"github.com/tilt-dev/tilt/internal/store"
"github.com/tilt-dev/tilt/pkg/model/logstore"
)
type TerminalStream struct {
ProcessedLogs logstore.Checkpoint
printer *IncrementalPrinter
store store.RStore
}
func NewTerminalStream(printer *IncrementalPrinter, store store.RStore) *TerminalStream {
return &TerminalStream{printer: printer, store: store}
}
// TODO(nick): We should change this API so that TearDown gets
// the RStore one last time.
func (h *TerminalStream) TearDown(ctx context.Context) {
if !h.isEnabled(h.store) {
return
}
_ = h.OnChange(ctx, h.store, store.LegacyChangeSummary())
state := h.store.RLockState()
uncompleted := state.LogStore.IsLastSegmentUncompleted()
h.store.RUnlockState()
if uncompleted {
h.printer.PrintNewline()
}
}
func (h *TerminalStream) isEnabled(st store.RStore) bool {
state := st.RLockState()
defer st.RUnlockState()
return state.TerminalMode == store.TerminalModeStream
}
func (h *TerminalStream) OnChange(ctx context.Context, st store.RStore, _ store.ChangeSummary) error {
if !h.isEnabled(st) {
return nil
}
state := st.RLockState()
lines := state.LogStore.ContinuingLines(h.ProcessedLogs)
checkpoint := state.LogStore.Checkpoint()
st.RUnlockState()
h.printer.Print(lines)
h.ProcessedLogs = checkpoint
return nil
}
var _ store.TearDowner = &TerminalStream{}