forked from charmbracelet/huh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_note.go
266 lines (228 loc) · 5.08 KB
/
field_note.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package huh
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
)
// Note is a form note field.
type Note struct {
// customization
title string
description string
// state
showNextButton bool
focused bool
// options
skip bool
width int
height int
accessible bool
theme *Theme
keymap NoteKeyMap
}
// NewNote creates a new note field.
func NewNote() *Note {
return &Note{
showNextButton: false,
skip: true,
}
}
// Title sets the title of the note field.
func (n *Note) Title(title string) *Note {
n.title = title
return n
}
// Description sets the description of the note field.
func (n *Note) Description(description string) *Note {
n.description = description
return n
}
// Next sets whether to show the next button.
func (n *Note) Next(show bool) *Note {
n.showNextButton = show
return n
}
// Focus focuses the note field.
func (n *Note) Focus() tea.Cmd {
n.focused = true
return nil
}
// Blur blurs the note field.
func (n *Note) Blur() tea.Cmd {
n.focused = false
return nil
}
// Error returns the error of the note field.
func (n *Note) Error() error {
return nil
}
// Skip returns whether the note should be skipped or should be blocking.
func (n *Note) Skip() bool {
return n.skip
}
// Zoom returns whether the note should be zoomed.
func (n *Note) Zoom() bool {
return false
}
// KeyBinds returns the help message for the note field.
func (n *Note) KeyBinds() []key.Binding {
return []key.Binding{n.keymap.Prev, n.keymap.Submit, n.keymap.Next}
}
// Init initializes the note field.
func (n *Note) Init() tea.Cmd {
return nil
}
// Update updates the note field.
func (n *Note) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, n.keymap.Prev):
return n, PrevField
case key.Matches(msg, n.keymap.Next, n.keymap.Submit):
return n, NextField
}
return n, NextField
}
return n, nil
}
func (n *Note) activeStyles() *FieldStyles {
theme := n.theme
if theme == nil {
theme = ThemeCharm()
}
if n.focused {
return &theme.Focused
}
return &theme.Focused
}
// View renders the note field.
func (n *Note) View() string {
var (
styles = n.activeStyles()
sb strings.Builder
)
if n.title != "" {
sb.WriteString(styles.NoteTitle.Render(n.title))
}
if n.description != "" {
sb.WriteString("\n")
sb.WriteString(render(n.description))
}
if n.showNextButton {
sb.WriteString(styles.Next.Render("Next"))
}
return styles.Card.Render(sb.String())
}
// Run runs the note field.
func (n *Note) Run() error {
if n.accessible {
return n.runAccessible()
}
return Run(n)
}
// runAccessible runs an accessible note field.
func (n *Note) runAccessible() error {
var body string
if n.title != "" {
body = n.title + "\n\n"
}
body += n.description
fmt.Println(body)
fmt.Println()
return nil
}
// WithTheme sets the theme on a note field.
func (n *Note) WithTheme(theme *Theme) Field {
if n.theme != nil {
return n
}
n.theme = theme
return n
}
// WithKeyMap sets the keymap on a note field.
func (n *Note) WithKeyMap(k *KeyMap) Field {
n.keymap = k.Note
return n
}
// WithAccessible sets the accessible mode of the note field.
func (n *Note) WithAccessible(accessible bool) Field {
n.accessible = accessible
return n
}
// WithWidth sets the width of the note field.
func (n *Note) WithWidth(width int) Field {
n.width = width
return n
}
// WithHeight sets the height of the note field.
func (n *Note) WithHeight(height int) Field {
n.height = height
return n
}
// WithPosition sets the position information of the note field.
func (n *Note) WithPosition(p FieldPosition) Field {
// if the note is the only field on the screen,
// we shouldn't skip the entire group.
if p.Field == p.FirstField && p.Field == p.LastField {
n.skip = false
}
n.keymap.Prev.SetEnabled(!p.IsFirst())
n.keymap.Next.SetEnabled(!p.IsLast())
n.keymap.Submit.SetEnabled(p.IsLast())
return n
}
// GetValue satisfies the Field interface, notes do not have values.
func (n *Note) GetValue() any {
return nil
}
// GetKey satisfies the Field interface, notes do not have keys.
func (n *Note) GetKey() string {
return ""
}
func render(input string) string {
var result strings.Builder
var italic, bold, codeblock bool
for _, char := range input {
switch char {
case '_':
if !italic {
result.WriteString("\033[3m")
italic = true
} else {
result.WriteString("\033[23m")
italic = false
}
case '*':
if !bold {
result.WriteString("\033[1m")
bold = true
} else {
result.WriteString("\033[22m")
bold = false
}
case '`':
if !codeblock {
result.WriteString("\033[0;37;40m")
result.WriteString(" ")
codeblock = true
} else {
result.WriteString(" ")
result.WriteString("\033[0m")
codeblock = false
if bold {
result.WriteString("\033[1m")
}
if italic {
result.WriteString("\033[3m")
}
}
default:
result.WriteRune(char)
}
}
// Reset any open formatting
result.WriteString("\033[0m")
return result.String()
}