Skip to content

Commit

Permalink
refactor: Refactor commit message printing and improve user interaction.
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenvanduocit committed Mar 13, 2023
1 parent b75708e commit 2e4ffe0
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
if err != nil {
explain, err := explainError(context.Background(), client, err)
if err != nil {
printError("failed to explain error: " + err.Error())
printError(err.Error())
os.Exit(1)
}
printError(explain)
Expand All @@ -45,37 +45,41 @@ func main() {
if isDirty() {
fmt.Println("Please stage your changes and try again")
} else {
fmt.Println("Nothing to commit")
fmt.Println("Nothing to commit, working tree clean")
}

os.Exit(0)
}

commitMessage := ""
// loop until the commit message is generated
for {
ctx, _ := context.WithTimeout(context.Background(), time.Second*10)
messages = append(messages, &Message{
Role: "user",
Content: diff,
})

// generate commit message
printNormal("Assistant: " + generateLoadingMessage())
commitMessage, err = client.ChatComplete(ctx, messages)
if err != nil {
explain, err := explainError(ctx, client, err)
if err != nil {
printError("failed to explain error: " + err.Error())
printError(err.Error())
os.Exit(1)
}
printError(explain)
os.Exit(1)
}

if commitMessage == "" {
printNormal("No commit message generated, please try again")
printNormal("Assistant: I don't know what to say about this diff, please give me a hint")
} else {
printNormal("Assistant: " + commitMessage)
}

// Loop until the user response
userRequest := ""
for {
fmt.Println("Assistant: " + generateInteractiveMessage())
Expand All @@ -85,7 +89,7 @@ func main() {
if err != nil {
explain, err := explainError(ctx, client, err)
if err != nil {
printError("failed to explain error: " + err.Error())
printError(err.Error())
os.Exit(1)
}
printError(explain)
Expand All @@ -95,7 +99,7 @@ func main() {
userRequest = strings.TrimSpace(userRequest)

if userRequest == "" {
printWarning("Please enter your response, say yes if you want to use the message or press Ctrl+C to exit")
printWarning("Assistant: Please enter your response, say yes if you want to use the message or press Ctrl+C to exit")
continue
}

Expand All @@ -121,11 +125,11 @@ func main() {
commitMessage = joinPrefix(prefix, commitMessage)

if err := commit(commitMessage); err != nil {
printError("failed to commit: " + err.Error())
printError("Assistant: failed to commit: " + err.Error())
os.Exit(1)
}

printSuccess("Commit successfully with message: " + commitMessage)
printSuccess("Assistant: Commit successfully with message: " + commitMessage)
}

func joinPrefix(prefix string, message string) string {
Expand Down

0 comments on commit 2e4ffe0

Please sign in to comment.