Add Text.SetLast

This commit is contained in:
Trevor Slocum 2024-10-14 13:09:06 -07:00
parent ec4f094f57
commit 8aa49057b7
2 changed files with 33 additions and 0 deletions

View file

@ -272,6 +272,30 @@ func (f *TextField) SetText(text string) {
f.redraw = true
}
// SetLast sets the text of the last line of the field. Newline characters are
// replaced with spaces.
func (f *TextField) SetLast(text string) {
f.Lock()
defer f.Unlock()
t := bytes.ReplaceAll([]byte(text), []byte("\n"), []byte(" "))
f.processIncoming()
bufferLen := len(f.buffer)
if bufferLen == 0 {
f.incoming = append(f.incoming[:0], t...)
} else {
f.buffer[bufferLen-1] = t
if f.needWrap == -1 || f.needWrap > bufferLen-1 {
f.needWrap = bufferLen - 1
}
}
f.modified = true
f.redraw = true
}
// SetPrefix sets the text shown before the content of the field.
func (f *TextField) SetPrefix(text string) {
f.Lock()

View file

@ -161,6 +161,15 @@ func (t *Text) SetText(text string) {
t.resizeFont()
}
// SetLast sets the text of the last line of the field.
func (t *Text) SetLast(text string) {
t.Lock()
defer t.Unlock()
t.field.SetLast(text)
t.resizeFont()
}
func (t *Text) resizeFont() {
if !t.textResize {
if t.textAutoSize == t.textSize {