github.com/tilt-dev/tilt@v0.36.0/internal/hud/terminal_stream.go (about)

     1  package hud
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/tilt-dev/tilt/internal/store"
     7  	"github.com/tilt-dev/tilt/pkg/model/logstore"
     8  )
     9  
    10  type TerminalStream struct {
    11  	ProcessedLogs logstore.Checkpoint
    12  	printer       *IncrementalPrinter
    13  	filter        LogFilter
    14  	store         store.RStore
    15  }
    16  
    17  func NewTerminalStream(printer *IncrementalPrinter, filter LogFilter, store store.RStore) *TerminalStream {
    18  	return &TerminalStream{
    19  		printer: printer,
    20  		filter:  filter,
    21  		store:   store,
    22  	}
    23  }
    24  
    25  // TODO(nick): We should change this API so that TearDown gets
    26  // the RStore one last time.
    27  func (h *TerminalStream) TearDown(ctx context.Context) {
    28  	if !h.isEnabled(h.store) {
    29  		return
    30  	}
    31  
    32  	_ = h.OnChange(ctx, h.store, store.LegacyChangeSummary())
    33  
    34  	state := h.store.RLockState()
    35  	uncompleted := state.LogStore.IsLastSegmentUncompleted()
    36  	h.store.RUnlockState()
    37  
    38  	if uncompleted {
    39  		h.printer.PrintNewline()
    40  	}
    41  }
    42  
    43  func (h *TerminalStream) isEnabled(st store.RStore) bool {
    44  	state := st.RLockState()
    45  	defer st.RUnlockState()
    46  	return state.TerminalMode == store.TerminalModeStream
    47  }
    48  
    49  func (h *TerminalStream) OnChange(ctx context.Context, st store.RStore, _ store.ChangeSummary) error {
    50  	if !h.isEnabled(st) {
    51  		return nil
    52  	}
    53  
    54  	state := st.RLockState()
    55  	lines := state.LogStore.ContinuingLinesWithOptions(h.ProcessedLogs, logstore.LineOptions{
    56  		SuppressPrefix: h.filter.SuppressPrefix(),
    57  	})
    58  	lines = h.filter.Apply(lines)
    59  
    60  	checkpoint := state.LogStore.Checkpoint()
    61  	st.RUnlockState()
    62  
    63  	h.printer.Print(lines)
    64  	h.ProcessedLogs = checkpoint
    65  	return nil
    66  }
    67  
    68  var _ store.TearDowner = &TerminalStream{}