File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 15
15
DefaultInteractiveTextInput = InteractiveTextInputPrinter {
16
16
DefaultText : "Input text" ,
17
17
TextStyle : & ThemeDefault .PrimaryStyle ,
18
+ Mask : "" ,
18
19
}
19
20
)
20
21
@@ -23,6 +24,7 @@ type InteractiveTextInputPrinter struct {
23
24
TextStyle * Style
24
25
DefaultText string
25
26
MultiLine bool
27
+ Mask string
26
28
27
29
input []string
28
30
cursorXPos int
@@ -48,6 +50,12 @@ func (p InteractiveTextInputPrinter) WithMultiLine(multiLine ...bool) *Interacti
48
50
return & p
49
51
}
50
52
53
+ // WithMask sets the mask.
54
+ func (p InteractiveTextInputPrinter ) WithMask (mask string ) * InteractiveTextInputPrinter {
55
+ p .Mask = mask
56
+ return & p
57
+ }
58
+
51
59
// Show shows the interactive select menu and returns the selected entry.
52
60
func (p InteractiveTextInputPrinter ) Show (text ... string ) (string , error ) {
53
61
// should be the first defer statement to make sure it is executed last
@@ -200,13 +208,19 @@ func (p InteractiveTextInputPrinter) updateArea(area *AreaPrinter) string {
200
208
p .cursorYPos = 0
201
209
}
202
210
areaText := p .text
211
+
203
212
for i , s := range p .input {
204
213
if i < len (p .input )- 1 {
205
214
areaText += s + "\n "
206
215
} else {
207
216
areaText += s
208
217
}
209
218
}
219
+
220
+ if p .Mask != "" {
221
+ areaText = p .text + strings .Repeat (p .Mask , internal .GetStringMaxWidth (areaText )- internal .GetStringMaxWidth (p .text ))
222
+ }
223
+
210
224
if p .cursorXPos + internal .GetStringMaxWidth (p .input [p .cursorYPos ]) < 1 {
211
225
p .cursorXPos = - internal .GetStringMaxWidth (p .input [p .cursorYPos ])
212
226
}
Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ package pterm_test
3
3
import (
4
4
"testing"
5
5
6
+ "atomicgo.dev/keyboard"
7
+ "atomicgo.dev/keyboard/keys"
6
8
"github.com/MarvinJWendt/testza"
7
9
8
10
"github.com/pterm/pterm"
@@ -28,3 +30,14 @@ func TestInteractiveTextInputPrinter_WithTextStyle(t *testing.T) {
28
30
p := pterm .DefaultInteractiveTextInput .WithTextStyle (style )
29
31
testza .AssertEqual (t , p .TextStyle , style )
30
32
}
33
+
34
+ func TestInteractiveTextInputPrinter_WithMask (t * testing.T ) {
35
+ go func () {
36
+ keyboard .SimulateKeyPress ('a' )
37
+ keyboard .SimulateKeyPress ('b' )
38
+ keyboard .SimulateKeyPress ('c' )
39
+ keyboard .SimulateKeyPress (keys .Enter )
40
+ }()
41
+ result , _ := pterm .DefaultInteractiveTextInput .WithMask ("*" ).Show ()
42
+ testza .AssertEqual (t , result , "abc" )
43
+ }
You can’t perform that action at this time.
0 commit comments