@@ -2,11 +2,15 @@ package ui
22
33import (
44 "fmt"
5-
5+ "github.com/charmbracelet/bubbles/help"
6+ "github.com/charmbracelet/bubbles/key"
67 "github.com/charmbracelet/bubbles/textarea"
78 tea "github.com/charmbracelet/bubbletea"
9+ "github.com/charmbracelet/lipgloss"
810)
911
12+ var quitValueStyle = lipgloss .NewStyle ().Margin (0 , 0 , 0 , 2 )
13+
1014type TextAreaModel struct {
1115 label string
1216 canceled bool
@@ -25,7 +29,9 @@ type TextAreaModel struct {
2529 // validateErrPrefix is the prompt prefix when the verification is successful
2630 validateErrPrefix string
2731
28- input textarea.Model
32+ helpKeys keyMap
33+ help help.Model
34+ input textarea.Model
2935}
3036
3137func NewTextArea (label string ) * TextAreaModel {
@@ -37,6 +43,8 @@ func NewTextArea(label string) *TextAreaModel {
3743 return & TextAreaModel {
3844 input : ti ,
3945 label : label ,
46+ helpKeys : helpKeys ,
47+ help : help .New (),
4048 validateFunc : DefaultValidateFunc ,
4149 validateOkPrefix : DefaultValidateOkPrefix ,
4250 validateErrPrefix : DefaultValidateErrPrefix ,
@@ -90,13 +98,13 @@ func (m *TextAreaModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
9098 var cmd tea.Cmd
9199
92100 switch tmsg := msg .(type ) {
101+ case tea.WindowSizeMsg :
102+ // If we set a width on the help menu it can gracefully truncate
103+ // its view as needed.
104+ m .help .Width = tmsg .Width
93105 case tea.KeyMsg :
94- switch tmsg .Type {
95- case tea .KeyEsc :
96- if m .input .Focused () {
97- m .input .Blur ()
98- }
99- case tea .KeyCtrlJ :
106+ switch {
107+ case key .Matches (tmsg , m .helpKeys .Save ):
100108 // If the real-time verification function does not return an error,
101109 // then the input has been completed
102110 if m .err == nil {
@@ -105,10 +113,14 @@ func (m *TextAreaModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
105113 }
106114 // If there is a verification error, the error message should be display
107115 m .showErr = true
108- case tea . KeyCtrlC :
116+ case key . Matches ( tmsg , m . helpKeys . Quit ) :
109117 m .canceled = true
110118 return m , tea .Quit
111- case tea .KeyRunes :
119+ case tmsg .Type == tea .KeyEsc :
120+ if m .input .Focused () {
121+ m .input .Blur ()
122+ }
123+ case tmsg .Type == tea .KeyRunes :
112124 // Hide verification failure message when entering content again
113125 m .showErr = false
114126 m .err = nil
@@ -133,7 +145,7 @@ func (m *TextAreaModel) View() string {
133145 "%s %s\n %s\n " ,
134146 FontColor (m .validateOkPrefix , colorValidateOk ),
135147 m .label ,
136- m .Value (),
148+ quitValueStyle . Render ( fmt . Sprintf ( m .Value ()) ),
137149 )
138150 }
139151
@@ -143,6 +155,7 @@ func (m *TextAreaModel) View() string {
143155 }
144156
145157 var showMsg , errMsg string
158+ helpView := m .help .View (m .helpKeys )
146159 if m .err != nil {
147160 showMsg = fmt .Sprintf (
148161 "%s %s\n %s" ,
@@ -156,10 +169,11 @@ func (m *TextAreaModel) View() string {
156169 }
157170 } else {
158171 showMsg = fmt .Sprintf (
159- "%s %s\n %s" ,
172+ "%s %s\n %s\n %s " ,
160173 FontColor (m .validateOkPrefix , colorValidateOk ),
161174 m .label ,
162175 m .input .View (),
176+ helpStyle .Render (helpView ),
163177 )
164178 }
165179
0 commit comments