bbash/input_parser/auto_complete.go
2025-01-13 15:28:58 +01:00

90 lines
1.3 KiB
Go

package input_parser
var commandsTab []string
var prevInput string
var tabIndex int
func AutoComplete(input string) string {
// print(" commandsTab ")
// print(len(commandsTab))
// print(" prevInput ")
// print(prevInput)
// print(" tabIndex ")
// print(tabIndex)
inputLength := len(input)
// print(" inputLength ")
// print(inputLength)
if inputLength == 0 {
return input
}
if tabIndex+2 > len(commandsTab) {
tabIndex = -1
}
if prevInput == input {
tabIndex = tabIndex + 1
if len(commandsTab) == 0 && tabIndex > -1 {
return input
}
prevInput = commandsTab[tabIndex]
return commandsTab[tabIndex]
}
prevInput = input
tabIndex = 0
currentCommands := []string{
"cat",
"cd",
"cp",
"echo",
"head",
"help",
"ls",
"man",
"mv",
"pwd",
"rm",
"tail",
"touch",
}
var commandsWorking []string
// println(commandsWorking)
for _, currentCommand := range currentCommands {
// println("currentCommand")
// println(currentCommand)
if len(currentCommand) < inputLength {
continue
}
currentCommandWithoutEnd := currentCommand[:inputLength]
if currentCommandWithoutEnd != input {
continue
}
commandsWorking = append(commandsWorking, currentCommand)
}
commandsTab = commandsWorking
if len(commandsTab) == 0 {
return input
}
prevInput = commandsTab[0]
return commandsTab[0]
}