Skip to content

Commit

Permalink
Improve command handling logic by Gopher Nerd (#28)
Browse files Browse the repository at this point in the history
- [+] refactor(commands.go): improve command handling logic
 - Trim the input and check if it starts with the command prefix.
 - Split the input into command and potential arguments.
 - Retrieve the command and check if it exists in the commandHandlers map.
 - Call the handler function for the command if there are no extra arguments.
 - Return false if the input is not a command.
  • Loading branch information
H0llyW00dzZ authored Jan 7, 2024
1 parent e2dab86 commit 670694a
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions terminal/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,31 @@ type CommandHandler func(session *Session) (bool, error)
// bool: A boolean indicating if the input was a command and was handled.
// error: An error that may occur while handling the command.
func HandleCommand(input string, session *Session) (bool, error) {
if strings.HasPrefix(input, PrefixChar) {
command := strings.TrimSpace(input)
if handler, exists := commandHandlers[command]; exists {
// Call the handler function for the command
// Trim the input and check if it starts with the command prefix.
trimmedInput := strings.TrimSpace(input)
if !strings.HasPrefix(trimmedInput, PrefixChar) {
// If the input doesn't start with the command prefix, it's not a command.
return false, nil
}

// Split the input into command and potential arguments.
parts := strings.Fields(trimmedInput)
if len(parts) == 0 {
// This should not happen due to the previous check, but just in case.
return false, nil
}

// Retrieve the command and check if it exists in the commandHandlers map.
command := parts[0]
if handler, exists := commandHandlers[command]; exists {
if len(parts) == 1 {
// Call the handler function for the command if there are no extra arguments.
return handler(session)
} else {
fmt.Println(UnknownCommand)
return true, nil
}
}

// let ai handle the command where it's not a command hahaha
//fmt.Println(UnknownCommand)
return false, nil
}

Expand Down

0 comments on commit 670694a

Please sign in to comment.