MCPcopy
hub / github.com/mum4k/termdash / Write

Method Write

widgets/segmentdisplay/segmentdisplay.go:117–151  ·  view source on GitHub ↗

Write writes text for the widget to display. Subsequent calls replace text written previously. All the provided text chunks are broken into characters and each character is displayed in one segment. The provided write options determine the behavior when text contains unsupported characters and set

(chunks []*TextChunk, opts ...Option)

Source from the content-addressed store, hash-verified

115//
116// Any provided options override options given to New.
117func (sd *SegmentDisplay) Write(chunks []*TextChunk, opts ...Option) error {
118 sd.mu.Lock()
119 defer sd.mu.Unlock()
120
121 for _, o := range opts {
122 o.set(sd.opts)
123 }
124 if err := sd.opts.validate(); err != nil {
125 return err
126 }
127
128 if len(chunks) == 0 {
129 return errors.New("at least one text chunk must be specified")
130 }
131 sd.reset()
132
133 for i, tc := range chunks {
134 if tc.text == "" {
135 return fmt.Errorf("text chunk[%d] is empty, all chunks must contains some text", i)
136 }
137 if ok, badRunes := sixteen.SupportsChars(tc.text); !ok && tc.wOpts.errOnUnsupported {
138 return fmt.Errorf("text chunk[%d] contains unsupported characters %v, clean the text or provide the WriteSanitize option", i, badRunes)
139 }
140 text := sixteen.Sanitize(tc.text)
141
142 pos := sd.buff.Len()
143 sd.givenWOpts = append(sd.givenWOpts, tc.wOpts)
144 wOptsIdx := len(sd.givenWOpts) - 1
145 if err := sd.wOptsTracker.Add(pos, pos+len(text), wOptsIdx); err != nil {
146 return err
147 }
148 sd.buff.WriteString(text)
149 }
150 return nil
151}
152
153// Capacity returns the number of characters that can fit into the canvas.
154// This is essentially the number of individual segments that can fit on the

Callers 4

TestSegmentDisplayFunction · 0.45
clockFunction · 0.45
rollTextFunction · 0.45
mainFunction · 0.45

Calls 7

resetMethod · 0.95
SupportsCharsFunction · 0.92
SanitizeFunction · 0.92
NewMethod · 0.80
setMethod · 0.65
validateMethod · 0.45
AddMethod · 0.45

Tested by 1

TestSegmentDisplayFunction · 0.36