@@ -22,40 +22,50 @@ import (
2222 "slices"
2323
2424 "github.com/charmbracelet/huh"
25+ "github.com/slackapi/slack-cli/internal/style"
2526)
2627
27- // charmInputPrompt prompts for text input using a charm huh form
28- func charmInputPrompt (_ * IOStreams , _ context.Context , message string , cfg InputPromptConfig ) (string , error ) {
29- var input string
28+ // buildInputForm constructs a huh form for text input prompts.
29+ func buildInputForm (message string , cfg InputPromptConfig , input * string ) * huh.Form {
3030 field := huh .NewInput ().
3131 Title (message ).
32- Value (& input )
32+ Value (input )
3333 if cfg .Required {
3434 field .Validate (huh .ValidateMinLength (1 ))
3535 }
36- err := huh .NewForm (huh .NewGroup (field )).WithTheme (ThemeSlack ()).Run ()
36+ return huh .NewForm (huh .NewGroup (field )).WithTheme (style .ThemeSlack ())
37+ }
38+
39+ // charmInputPrompt prompts for text input using a charm huh form
40+ func charmInputPrompt (_ * IOStreams , _ context.Context , message string , cfg InputPromptConfig ) (string , error ) {
41+ var input string
42+ err := buildInputForm (message , cfg , & input ).Run ()
3743 if err != nil {
3844 return "" , err
3945 }
4046 return input , nil
4147}
4248
49+ // buildConfirmForm constructs a huh form for yes/no confirmation prompts.
50+ func buildConfirmForm (message string , choice * bool ) * huh.Form {
51+ field := huh .NewConfirm ().
52+ Title (message ).
53+ Value (choice )
54+ return huh .NewForm (huh .NewGroup (field )).WithTheme (style .ThemeSlack ())
55+ }
56+
4357// charmConfirmPrompt prompts for a yes/no confirmation using a charm huh form
4458func charmConfirmPrompt (_ * IOStreams , _ context.Context , message string , defaultValue bool ) (bool , error ) {
4559 var choice = defaultValue
46- field := huh .NewConfirm ().
47- Title (message ).
48- Value (& choice )
49- err := huh .NewForm (huh .NewGroup (field )).WithTheme (ThemeSlack ()).Run ()
60+ err := buildConfirmForm (message , & choice ).Run ()
5061 if err != nil {
5162 return false , err
5263 }
5364 return choice , nil
5465}
5566
56- // charmSelectPrompt prompts the user to select one option using a charm huh form
57- func charmSelectPrompt (_ * IOStreams , _ context.Context , msg string , options []string , cfg SelectPromptConfig ) (SelectPromptResponse , error ) {
58- var selected string
67+ // buildSelectForm constructs a huh form for single-selection prompts.
68+ func buildSelectForm (msg string , options []string , cfg SelectPromptConfig , selected * string ) * huh.Form {
5969 var opts []huh.Option [string ]
6070 for _ , opt := range options {
6171 key := opt
@@ -70,13 +80,19 @@ func charmSelectPrompt(_ *IOStreams, _ context.Context, msg string, options []st
7080 field := huh .NewSelect [string ]().
7181 Title (msg ).
7282 Options (opts ... ).
73- Value (& selected )
83+ Value (selected )
7484
7585 if cfg .PageSize > 0 {
7686 field .Height (cfg .PageSize + 2 )
7787 }
7888
79- err := huh .NewForm (huh .NewGroup (field )).WithTheme (ThemeSlack ()).Run ()
89+ return huh .NewForm (huh .NewGroup (field )).WithTheme (style .ThemeSlack ())
90+ }
91+
92+ // charmSelectPrompt prompts the user to select one option using a charm huh form
93+ func charmSelectPrompt (_ * IOStreams , _ context.Context , msg string , options []string , cfg SelectPromptConfig ) (SelectPromptResponse , error ) {
94+ var selected string
95+ err := buildSelectForm (msg , options , cfg , & selected ).Run ()
8096 if err != nil {
8197 return SelectPromptResponse {}, err
8298 }
@@ -85,26 +101,30 @@ func charmSelectPrompt(_ *IOStreams, _ context.Context, msg string, options []st
85101 return SelectPromptResponse {Prompt : true , Index : index , Option : selected }, nil
86102}
87103
88- // charmPasswordPrompt prompts for a password (hidden input) using a charm huh form
89- func charmPasswordPrompt (_ * IOStreams , _ context.Context , message string , cfg PasswordPromptConfig ) (PasswordPromptResponse , error ) {
90- var input string
104+ // buildPasswordForm constructs a huh form for password (hidden input) prompts.
105+ func buildPasswordForm (message string , cfg PasswordPromptConfig , input * string ) * huh.Form {
91106 field := huh .NewInput ().
92107 Title (message ).
93108 EchoMode (huh .EchoModePassword ).
94- Value (& input )
109+ Value (input )
95110 if cfg .Required {
96111 field .Validate (huh .ValidateMinLength (1 ))
97112 }
98- err := huh .NewForm (huh .NewGroup (field )).WithTheme (ThemeSlack ()).Run ()
113+ return huh .NewForm (huh .NewGroup (field )).WithTheme (style .ThemeSlack ())
114+ }
115+
116+ // charmPasswordPrompt prompts for a password (hidden input) using a charm huh form
117+ func charmPasswordPrompt (_ * IOStreams , _ context.Context , message string , cfg PasswordPromptConfig ) (PasswordPromptResponse , error ) {
118+ var input string
119+ err := buildPasswordForm (message , cfg , & input ).Run ()
99120 if err != nil {
100121 return PasswordPromptResponse {}, err
101122 }
102123 return PasswordPromptResponse {Prompt : true , Value : input }, nil
103124}
104125
105- // charmMultiSelectPrompt prompts the user to select multiple options using a charm huh form
106- func charmMultiSelectPrompt (_ * IOStreams , _ context.Context , message string , options []string ) ([]string , error ) {
107- var selected []string
126+ // buildMultiSelectForm constructs a huh form for multiple-selection prompts.
127+ func buildMultiSelectForm (message string , options []string , selected * []string ) * huh.Form {
108128 var opts []huh.Option [string ]
109129 for _ , opt := range options {
110130 opts = append (opts , huh .NewOption (opt , opt ))
@@ -113,9 +133,15 @@ func charmMultiSelectPrompt(_ *IOStreams, _ context.Context, message string, opt
113133 field := huh .NewMultiSelect [string ]().
114134 Title (message ).
115135 Options (opts ... ).
116- Value (& selected )
136+ Value (selected )
117137
118- err := huh .NewForm (huh .NewGroup (field )).WithTheme (ThemeSlack ()).Run ()
138+ return huh .NewForm (huh .NewGroup (field )).WithTheme (style .ThemeSlack ())
139+ }
140+
141+ // charmMultiSelectPrompt prompts the user to select multiple options using a charm huh form
142+ func charmMultiSelectPrompt (_ * IOStreams , _ context.Context , message string , options []string ) ([]string , error ) {
143+ var selected []string
144+ err := buildMultiSelectForm (message , options , & selected ).Run ()
119145 if err != nil {
120146 return []string {}, err
121147 }
0 commit comments