forked from charmbracelet/huh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_note.go
203 lines (168 loc) Β· 3.91 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
package huh
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
)
func init() {
// XXX: For now, unset padding on default glamour styles.
glamour.DarkStyleConfig.Document.BlockPrefix = ""
glamour.DarkStyleConfig.Document.Margin = pointerTo[uint](0)
glamour.LightStyleConfig.Document.BlockPrefix = ""
glamour.LightStyleConfig.Document.Margin = pointerTo[uint](0)
}
// Note is a form note field.
type Note struct {
// customization
title string
description string
// state
showNextButton bool
focused bool
renderer *glamour.TermRenderer
// options
width int
accessible bool
theme *Theme
keymap *NoteKeyMap
}
// NewNote creates a new note field.
func NewNote() *Note {
r, err := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
glamour.WithEmoji(),
glamour.WithWordWrap(0),
)
if err != nil {
r, _ = glamour.NewTermRenderer()
}
return &Note{
showNextButton: false,
renderer: r,
}
}
// 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
}
// KeyBinds returns the help message for the note field.
func (n *Note) KeyBinds() []key.Binding {
return []key.Binding{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):
return n, nextField
}
return n, nextField
}
return n, nil
}
// View renders the note field.
func (n *Note) View() string {
styles := n.theme.Blurred
if n.focused {
styles = n.theme.Focused
}
var (
sb strings.Builder
body string
)
if n.title != "" {
body = fmt.Sprintf("# %s\n", n.title)
}
body += n.description
md, _ := n.renderer.Render(body)
sb.WriteString(md)
if n.showNextButton {
sb.WriteString(styles.Next.Render("Next"))
}
return styles.Base.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 = fmt.Sprintf("# %s\n", n.title)
}
body += n.description
md, _ := n.renderer.Render(body)
fmt.Println(n.theme.Blurred.Base.Render(strings.TrimSpace(md)))
fmt.Println()
return nil
}
// WithTheme sets the theme on a note field.
func (n *Note) WithTheme(theme *Theme) Field {
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
}
// 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 ""
}
// pointerTo returns a pointer to a value.
func pointerTo[T any](v T) *T {
return &v
}