добавляет IsChanged() и ResetWithoutValue()

This commit is contained in:
Maxim Slipenko 2022-11-09 11:39:49 +00:00
parent fac7a01329
commit d40721ef34

View File

@ -212,6 +212,8 @@ type Model struct {
// viewport is the vertically-scrollable viewport of the multi-line text // viewport is the vertically-scrollable viewport of the multi-line text
// input. // input.
viewport *viewport.Model viewport *viewport.Model
isChanged bool
} }
// New creates a new model with default settings. // New creates a new model with default settings.
@ -281,6 +283,12 @@ func (m *Model) SetValue(s string) {
m.InsertString(s) m.InsertString(s)
} }
func (m *Model) SetValueAndReset(s string) {
m.Reset()
m.InsertString(s)
m.ResetWithoutValue()
}
// InsertString inserts a string at the cursor position. // InsertString inserts a string at the cursor position.
func (m *Model) InsertString(s string) { func (m *Model) InsertString(s string) {
lines := strings.Split(s, "\n") lines := strings.Split(s, "\n")
@ -320,6 +328,10 @@ func (m Model) Value() string {
return strings.TrimSuffix(v.String(), "\n") return strings.TrimSuffix(v.String(), "\n")
} }
func (m Model) IsChanged() bool {
return m.isChanged
}
// Length returns the number of characters currently in the text input. // Length returns the number of characters currently in the text input.
func (m *Model) Length() int { func (m *Model) Length() int {
var l int var l int
@ -450,10 +462,15 @@ func (m *Model) Blur() {
// Reset sets the input to its default state with no input. // Reset sets the input to its default state with no input.
func (m *Model) Reset() { func (m *Model) Reset() {
m.value = make([][]rune, minHeight, maxHeight) m.value = make([][]rune, minHeight, maxHeight)
m.ResetWithoutValue()
}
func (m *Model) ResetWithoutValue() {
m.col = 0 m.col = 0
m.row = 0 m.row = 0
m.viewport.GotoTop() m.viewport.GotoTop()
m.SetCursor(0) m.SetCursor(0)
m.isChanged = false
} }
// handle a clipboard paste event, if supported. // handle a clipboard paste event, if supported.
@ -940,6 +957,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
break break
} }
m.isChanged = true
m.col = min(m.col, len(m.value[m.row])) m.col = min(m.col, len(m.value[m.row]))
m.value[m.row] = append(m.value[m.row][:m.col], append(msg.Runes, m.value[m.row][m.col:]...)...) m.value[m.row] = append(m.value[m.row][:m.col], append(msg.Runes, m.value[m.row][m.col:]...)...)
m.SetCursor(m.col + len(msg.Runes)) m.SetCursor(m.col + len(msg.Runes))