Repository navigation
Expand file tree
/
Copy pathinput.go
More file actions
151 lines (128 loc) · 3.5 KB
/
Copy pathinput.go
File metadata and controls
151 lines (128 loc) · 3.5 KB
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
package huhx
import "charm.land/huh/v2"
// Input is a thin builder over *huh.Input that captures the validator and
// destination pointer so the non-interactive runner can drive the field
// without going through huh's bubble tea loop.
type Input struct {
inner *huh.Input
k string
value *string
accessor huh.Accessor[string]
validate func(string) error
optional bool
}
// NewInput returns a new Input wrapping huh.NewInput().
func NewInput() *Input {
return &Input{inner: huh.NewInput()}
}
// Key sets the field key used for answer lookup.
func (i *Input) Key(k string) *Input {
i.k = k
i.inner.Key(k)
return i
}
// Title sets the field title.
func (i *Input) Title(s string) *Input {
i.inner.Title(s)
return i
}
// Description sets the field description.
func (i *Input) Description(s string) *Input {
i.inner.Description(s)
return i
}
// Placeholder sets the field placeholder.
func (i *Input) Placeholder(s string) *Input {
i.inner.Placeholder(s)
return i
}
// CharLimit sets the maximum character length.
func (i *Input) CharLimit(n int) *Input {
i.inner.CharLimit(n)
return i
}
// Value binds a destination string pointer.
func (i *Input) Value(v *string) *Input {
i.value = v
i.accessor = huh.NewPointerAccessor(v)
i.inner.Value(v)
return i
}
// Accessor sets a custom accessor for reading and writing the field value.
func (i *Input) Accessor(a huh.Accessor[string]) *Input {
i.accessor = a
i.inner.Accessor(a)
return i
}
// TitleFunc sets a dynamic title function.
func (i *Input) TitleFunc(f func() string, bindings any) *Input {
i.inner.TitleFunc(f, bindings)
return i
}
// DescriptionFunc sets a dynamic description function.
func (i *Input) DescriptionFunc(f func() string, bindings any) *Input {
i.inner.DescriptionFunc(f, bindings)
return i
}
// Prompt sets the input prompt.
func (i *Input) Prompt(s string) *Input {
i.inner.Prompt(s)
return i
}
// Suggestions sets the autocomplete suggestions.
func (i *Input) Suggestions(suggestions []string) *Input {
i.inner.Suggestions(suggestions)
return i
}
// SuggestionsFunc sets a dynamic suggestions function.
func (i *Input) SuggestionsFunc(f func() []string, bindings any) *Input {
i.inner.SuggestionsFunc(f, bindings)
return i
}
// EchoMode sets the echo mode for the input.
func (i *Input) EchoMode(mode huh.EchoMode) *Input {
i.inner.EchoMode(mode)
return i
}
// Password toggles password masking for the input.
func (i *Input) Password(password bool) *Input {
i.inner.Password(password)
return i
}
// PlaceholderFunc sets a dynamic placeholder function.
func (i *Input) PlaceholderFunc(f func() string, bindings any) *Input {
i.inner.PlaceholderFunc(f, bindings)
return i
}
// Inline toggles inline rendering for the input.
func (i *Input) Inline(inline bool) *Input {
i.inner.Inline(inline)
return i
}
// Validate sets the validator on both the wrapper and the inner huh field.
func (i *Input) Validate(fn func(string) error) *Input {
i.validate = fn
i.inner.Validate(fn)
return i
}
// Optional marks the field as not required in non-interactive mode.
func (i *Input) Optional() *Input {
i.optional = true
return i
}
func (i *Input) key() string { return i.k }
func (i *Input) huhField() huh.Field { return i.inner }
func (i *Input) required() bool { return !i.optional }
func (i *Input) set(value string) error {
if i.accessor != nil {
i.accessor.Set(value)
} else if i.value != nil {
*i.value = value
}
if i.validate != nil {
if err := i.validate(value); err != nil {
return err
}
}
return nil
}