1414
1515package iostreams
1616
17- // Charm-based prompt implementations using the huh library
18- // These are used when the "charm " experiment is enabled
17+ // Charm-based prompt implementations using the huh library.
18+ // These are used when the "huh " experiment is enabled.
1919
2020import (
2121 "context"
2222 "errors"
2323 "slices"
2424
2525 huh "charm.land/huh/v2"
26+ "github.com/slackapi/slack-cli/internal/experiment"
2627 "github.com/slackapi/slack-cli/internal/slackerror"
2728 "github.com/slackapi/slack-cli/internal/style"
2829)
2930
31+ // newForm wraps a field in a huh form, applying the Slack theme when the lipgloss experiment is enabled.
32+ func newForm (io * IOStreams , field huh.Field ) * huh.Form {
33+ form := huh .NewForm (huh .NewGroup (field ))
34+ if io != nil && io .config .WithExperimentOn (experiment .Lipgloss ) {
35+ form = form .WithTheme (style .ThemeSlack ())
36+ }
37+ return form
38+ }
39+
3040// buildInputForm constructs a huh form for text input prompts.
31- func buildInputForm (message string , cfg InputPromptConfig , input * string ) * huh.Form {
41+ func buildInputForm (io * IOStreams , message string , cfg InputPromptConfig , input * string ) * huh.Form {
3242 field := huh .NewInput ().
3343 Title (message ).
3444 Prompt (style .Chevron () + " " ).
@@ -37,13 +47,13 @@ func buildInputForm(message string, cfg InputPromptConfig, input *string) *huh.F
3747 if cfg .Required {
3848 field .Validate (huh .ValidateMinLength (1 ))
3949 }
40- return huh . NewForm ( huh . NewGroup ( field )). WithTheme ( style . ThemeSlack () )
50+ return newForm ( io , field )
4151}
4252
4353// charmInputPrompt prompts for text input using a charm huh form
44- func charmInputPrompt (_ * IOStreams , _ context.Context , message string , cfg InputPromptConfig ) (string , error ) {
54+ func charmInputPrompt (io * IOStreams , _ context.Context , message string , cfg InputPromptConfig ) (string , error ) {
4555 var input string
46- err := buildInputForm (message , cfg , & input ).Run ()
56+ err := buildInputForm (io , message , cfg , & input ).Run ()
4757 if errors .Is (err , huh .ErrUserAborted ) {
4858 return "" , slackerror .New (slackerror .ErrProcessInterrupted )
4959 } else if err != nil {
@@ -53,17 +63,17 @@ func charmInputPrompt(_ *IOStreams, _ context.Context, message string, cfg Input
5363}
5464
5565// buildConfirmForm constructs a huh form for yes/no confirmation prompts.
56- func buildConfirmForm (message string , choice * bool ) * huh.Form {
66+ func buildConfirmForm (io * IOStreams , message string , choice * bool ) * huh.Form {
5767 field := huh .NewConfirm ().
5868 Title (message ).
5969 Value (choice )
60- return huh . NewForm ( huh . NewGroup ( field )). WithTheme ( style . ThemeSlack () )
70+ return newForm ( io , field )
6171}
6272
6373// charmConfirmPrompt prompts for a yes/no confirmation using a charm huh form
64- func charmConfirmPrompt (_ * IOStreams , _ context.Context , message string , defaultValue bool ) (bool , error ) {
74+ func charmConfirmPrompt (io * IOStreams , _ context.Context , message string , defaultValue bool ) (bool , error ) {
6575 var choice = defaultValue
66- err := buildConfirmForm (message , & choice ).Run ()
76+ err := buildConfirmForm (io , message , & choice ).Run ()
6777 if errors .Is (err , huh .ErrUserAborted ) {
6878 return false , slackerror .New (slackerror .ErrProcessInterrupted )
6979 } else if err != nil {
@@ -73,7 +83,7 @@ func charmConfirmPrompt(_ *IOStreams, _ context.Context, message string, default
7383}
7484
7585// buildSelectForm constructs a huh form for single-selection prompts.
76- func buildSelectForm (msg string , options []string , cfg SelectPromptConfig , selected * string ) * huh.Form {
86+ func buildSelectForm (io * IOStreams , msg string , options []string , cfg SelectPromptConfig , selected * string ) * huh.Form {
7787 var opts []huh.Option [string ]
7888 for _ , opt := range options {
7989 key := opt
@@ -91,13 +101,13 @@ func buildSelectForm(msg string, options []string, cfg SelectPromptConfig, selec
91101 Options (opts ... ).
92102 Value (selected )
93103
94- return huh . NewForm ( huh . NewGroup ( field )). WithTheme ( style . ThemeSlack () )
104+ return newForm ( io , field )
95105}
96106
97107// charmSelectPrompt prompts the user to select one option using a charm huh form
98- func charmSelectPrompt (_ * IOStreams , _ context.Context , msg string , options []string , cfg SelectPromptConfig ) (SelectPromptResponse , error ) {
108+ func charmSelectPrompt (io * IOStreams , _ context.Context , msg string , options []string , cfg SelectPromptConfig ) (SelectPromptResponse , error ) {
99109 var selected string
100- err := buildSelectForm (msg , options , cfg , & selected ).Run ()
110+ err := buildSelectForm (io , msg , options , cfg , & selected ).Run ()
101111 if errors .Is (err , huh .ErrUserAborted ) {
102112 return SelectPromptResponse {}, slackerror .New (slackerror .ErrProcessInterrupted )
103113 } else if err != nil {
@@ -109,7 +119,7 @@ func charmSelectPrompt(_ *IOStreams, _ context.Context, msg string, options []st
109119}
110120
111121// buildPasswordForm constructs a huh form for password (hidden input) prompts.
112- func buildPasswordForm (message string , cfg PasswordPromptConfig , input * string ) * huh.Form {
122+ func buildPasswordForm (io * IOStreams , message string , cfg PasswordPromptConfig , input * string ) * huh.Form {
113123 field := huh .NewInput ().
114124 Title (message ).
115125 Prompt (style .Chevron () + " " ).
@@ -118,13 +128,13 @@ func buildPasswordForm(message string, cfg PasswordPromptConfig, input *string)
118128 if cfg .Required {
119129 field .Validate (huh .ValidateMinLength (1 ))
120130 }
121- return huh . NewForm ( huh . NewGroup ( field )). WithTheme ( style . ThemeSlack () )
131+ return newForm ( io , field )
122132}
123133
124134// charmPasswordPrompt prompts for a password (hidden input) using a charm huh form
125- func charmPasswordPrompt (_ * IOStreams , _ context.Context , message string , cfg PasswordPromptConfig ) (PasswordPromptResponse , error ) {
135+ func charmPasswordPrompt (io * IOStreams , _ context.Context , message string , cfg PasswordPromptConfig ) (PasswordPromptResponse , error ) {
126136 var input string
127- err := buildPasswordForm (message , cfg , & input ).Run ()
137+ err := buildPasswordForm (io , message , cfg , & input ).Run ()
128138 if errors .Is (err , huh .ErrUserAborted ) {
129139 return PasswordPromptResponse {}, slackerror .New (slackerror .ErrProcessInterrupted )
130140 } else if err != nil {
@@ -134,7 +144,7 @@ func charmPasswordPrompt(_ *IOStreams, _ context.Context, message string, cfg Pa
134144}
135145
136146// buildMultiSelectForm constructs a huh form for multiple-selection prompts.
137- func buildMultiSelectForm (message string , options []string , selected * []string ) * huh.Form {
147+ func buildMultiSelectForm (io * IOStreams , message string , options []string , selected * []string ) * huh.Form {
138148 var opts []huh.Option [string ]
139149 for _ , opt := range options {
140150 opts = append (opts , huh .NewOption (opt , opt ))
@@ -145,13 +155,13 @@ func buildMultiSelectForm(message string, options []string, selected *[]string)
145155 Options (opts ... ).
146156 Value (selected )
147157
148- return huh . NewForm ( huh . NewGroup ( field )). WithTheme ( style . ThemeSlack () )
158+ return newForm ( io , field )
149159}
150160
151161// charmMultiSelectPrompt prompts the user to select multiple options using a charm huh form
152- func charmMultiSelectPrompt (_ * IOStreams , _ context.Context , message string , options []string ) ([]string , error ) {
162+ func charmMultiSelectPrompt (io * IOStreams , _ context.Context , message string , options []string ) ([]string , error ) {
153163 var selected []string
154- err := buildMultiSelectForm (message , options , & selected ).Run ()
164+ err := buildMultiSelectForm (io , message , options , & selected ).Run ()
155165 if errors .Is (err , huh .ErrUserAborted ) {
156166 return []string {}, slackerror .New (slackerror .ErrProcessInterrupted )
157167 } else if err != nil {
0 commit comments