Skip to content

Commit da15e84

Browse files
committed
feat: added dynamic scope prompt and switch to strings.Builder
1 parent f998246 commit da15e84

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

main.go

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var (
4343
// Global Vars
4444
var (
4545
commitTypes []string
46+
scopes []string
4647
gitRoot string
4748
)
4849

@@ -102,6 +103,7 @@ func loadConfig() {
102103
// Set Default Config Values
103104
viper.SetDefault("use_defaults", true)
104105
viper.SetDefault("custom_commit_types", []string{})
106+
viper.SetDefault("scopes", []string{})
105107

106108
default_commit_types := []string{"feat", "fix", "build", "chore", "ci", "docs", "refactor", "test"}
107109

@@ -116,8 +118,9 @@ func loadConfig() {
116118
commitTypes = viper.GetStringSlice("custom_commit_types")
117119
}
118120

119-
// dedup slice just in case defaults added to custom_commit_type
121+
// dedup slices just in case
120122
commitTypes = removeDuplicateStr(commitTypes)
123+
scopes = removeDuplicateStr(viper.GetStringSlice("scopes"))
121124
}
122125

123126
func openGitRepo() (*git.Repository, error) {
@@ -153,41 +156,58 @@ func parseFlags() {
153156
}
154157

155158
func promptForCommit(commitTypes []string) (string, error) {
156-
var commitMessage string
157-
158-
// Select commit type
159-
// commitTypes := []string{"feat", "fix", "build", "chore", "ci", "docs", "refactor", "test"}
159+
var commitMessage strings.Builder
160+
var scope string
160161

161162
// Use PTerm's interactive select feature to present the options to the user and capture their selection
162-
selectedOption, _ := pterm.DefaultInteractiveSelect.WithOptions(commitTypes).WithDefaultText("Commit Type").WithMaxHeight(20).Show()
163+
commitType, _ := pterm.DefaultInteractiveSelect.WithOptions(commitTypes).WithDefaultText("Commit Type").WithMaxHeight(20).Show()
163164

164-
// Create an interactive text input with single line input mode and show it
165+
if len(scopes) > 0 {
166+
scope, _ = pterm.DefaultInteractiveSelect.WithOptions(scopes).WithDefaultText("Scope (optional)").WithMaxHeight(10).Show()
167+
} else {
168+
scope, _ = pterm.DefaultInteractiveTextInput.WithDefaultText("Scope (optional)").Show()
169+
}
170+
171+
// Prompt for single line short description
165172
shortDescription, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("Short Description").Show()
166173

167-
// This allows the user to input multiple lines of text.
174+
// Pompt for optional multiline long description
168175
longDescription, _ := pterm.DefaultInteractiveTextInput.WithMultiLine().WithDefaultText("Long Description (optional)").Show()
169176

170177
if len(longDescription) > 0 {
171-
longDescription = "\n\n" + strings.TrimSpace(longDescription)
178+
longDescription = strings.TrimSpace(longDescription)
172179
}
173180

174-
// Show an interactive confirmation dialog and get the result.
181+
// confirm is this commit includes a breaking change
175182
breakingChange, _ := pterm.DefaultInteractiveConfirm.WithDefaultText("Breaking Change").WithDefaultValue(false).Show()
176183

184+
// build commit message
185+
commitMessage.WriteString(commitType)
186+
187+
if len(scope) > 0 {
188+
commitMessage.WriteString("(" + scope + ")")
189+
}
190+
191+
var breakingChangeMessage string
192+
177193
if breakingChange {
178194
// Prompt for breaking change message
179-
breakingChangeMessage, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("Breaking Change Note").Show()
180-
181-
if len(breakingChangeMessage) > 0 {
182-
commitMessage = selectedOption + "!: " + shortDescription + longDescription + "\n\n" + "BREAKING CHANGE: " + breakingChangeMessage
183-
} else {
184-
commitMessage = selectedOption + "!: " + shortDescription + longDescription
185-
}
195+
breakingChangeMessage, _ = pterm.DefaultInteractiveTextInput.WithDefaultText("Breaking Change Note").Show()
196+
197+
commitMessage.WriteString("!: " + shortDescription)
186198
} else {
187-
commitMessage = selectedOption + ": " + shortDescription + longDescription
199+
commitMessage.WriteString(": " + shortDescription)
200+
}
201+
202+
if len(longDescription) > 0 {
203+
commitMessage.WriteString("\n\n" + longDescription)
204+
}
205+
206+
if len(breakingChangeMessage) > 0 {
207+
commitMessage.WriteString("\n\nBREAKING CHANGE: " + breakingChangeMessage)
188208
}
189209

190-
return commitMessage, nil
210+
return commitMessage.String(), nil
191211
}
192212

193213
func removeDuplicateStr(strSlice []string) []string {

0 commit comments

Comments
 (0)