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)
| 115 | // |
| 116 | // Any provided options override options given to New. |
| 117 | func (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 |