From d40721ef34128005663d5da2961f61966d4ac872 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Wed, 9 Nov 2022 11:39:49 +0000 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D1=82=20IsChanged()=20=D0=B8=20ResetWithoutValue()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- textarea/textarea.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/textarea/textarea.go b/textarea/textarea.go index d552719..5c0cd6b 100644 --- a/textarea/textarea.go +++ b/textarea/textarea.go @@ -212,6 +212,8 @@ type Model struct { // viewport is the vertically-scrollable viewport of the multi-line text // input. viewport *viewport.Model + + isChanged bool } // New creates a new model with default settings. @@ -281,6 +283,12 @@ func (m *Model) SetValue(s string) { m.InsertString(s) } +func (m *Model) SetValueAndReset(s string) { + m.Reset() + m.InsertString(s) + m.ResetWithoutValue() +} + // InsertString inserts a string at the cursor position. func (m *Model) InsertString(s string) { lines := strings.Split(s, "\n") @@ -320,6 +328,10 @@ func (m Model) Value() string { 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. func (m *Model) Length() int { var l int @@ -450,10 +462,15 @@ func (m *Model) Blur() { // Reset sets the input to its default state with no input. func (m *Model) Reset() { m.value = make([][]rune, minHeight, maxHeight) + m.ResetWithoutValue() +} + +func (m *Model) ResetWithoutValue() { m.col = 0 m.row = 0 m.viewport.GotoTop() m.SetCursor(0) + m.isChanged = false } // handle a clipboard paste event, if supported. @@ -940,6 +957,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { break } + m.isChanged = true + 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.SetCursor(m.col + len(msg.Runes))