331 lines
6.5 KiB
Go
331 lines
6.5 KiB
Go
package input_parser
|
|
|
|
import (
|
|
"bbash/environment"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
var commandsTab []string
|
|
var prevInput string
|
|
var tabIndex int
|
|
var skipCurrent bool
|
|
|
|
func AutoComplete(input string, env environment.Env) string {
|
|
|
|
rawInput := input
|
|
inputLength := len(input)
|
|
|
|
if inputLength == 0 {
|
|
return input
|
|
}
|
|
|
|
if prevInput == input {
|
|
|
|
if skipCurrent {
|
|
return input
|
|
}
|
|
|
|
tabIndex = tabIndex + 1
|
|
|
|
if len(commandsTab) == 0 && tabIndex > 0 {
|
|
return input
|
|
}
|
|
|
|
if tabIndex > len(commandsTab)-1 {
|
|
tabIndex = 0
|
|
}
|
|
|
|
prevInput = commandsTab[tabIndex]
|
|
return commandsTab[tabIndex]
|
|
}
|
|
|
|
prevInput = input
|
|
tabIndex = 0
|
|
|
|
var flagsDictionary []string
|
|
|
|
baseCommands := []string{
|
|
"cat",
|
|
"cd",
|
|
"cp",
|
|
"echo",
|
|
"exit",
|
|
"fritiofcommand",
|
|
"head",
|
|
"help",
|
|
"ls",
|
|
"man",
|
|
"mv",
|
|
"pwd",
|
|
"rm",
|
|
"tail",
|
|
"touch",
|
|
"whereareyou",
|
|
}
|
|
|
|
var paths []string
|
|
|
|
fs, err := os.ReadDir(env.Path)
|
|
if err != nil {
|
|
// fmt.Print(fmt.Sprintf("Error opening directory %s", env.Path))
|
|
}
|
|
for _, f := range fs {
|
|
// if (f.Name()[0] == '.') && !(slices.Contains(in.Flags, "a")) {
|
|
// continue
|
|
// } // allows for hidden directories
|
|
// fmt.Print(f.Name())
|
|
// fmt.Println()
|
|
paths = append(paths, f.Name())
|
|
|
|
}
|
|
|
|
skipCurrent = false
|
|
acceptAll := false
|
|
|
|
var autoCompleteCommands []string
|
|
indexField0 := ""
|
|
var currentIndexFields int
|
|
var nextAutoCompleteCommands []string
|
|
noMoreFlags := false
|
|
|
|
for indexFields, currentField := range strings.Fields(input) {
|
|
|
|
// print("noMoreFlags", noMoreFlags)
|
|
|
|
currentIndexFields = indexFields
|
|
|
|
if currentIndexFields == 0 {
|
|
autoCompleteCommands = baseCommands
|
|
indexField0 = currentField
|
|
|
|
// println("len(strings.Fields(input))", len(strings.Fields(input)))
|
|
// println("input[1:]k", input[len(input)-1:], "khej")
|
|
if len(strings.Fields(input)) == 1 && input[len(input)-1:] == " " {
|
|
currentIndexFields += 1
|
|
// println("den hittade mellanslag :D")
|
|
acceptAll = true
|
|
|
|
}
|
|
}
|
|
|
|
if currentIndexFields > 0 {
|
|
// autoCompleteCommands = baseFunctions
|
|
// println("currentField 1 :D", currentField)
|
|
|
|
switch indexField0 {
|
|
case "fritiofcommand":
|
|
skipCurrent = true
|
|
case "pwd":
|
|
skipCurrent = true
|
|
case "echo":
|
|
skipCurrent = true
|
|
case "ls":
|
|
autoCompleteCommands = paths
|
|
flagsDictionary = []string{
|
|
"-a", "-help",
|
|
}
|
|
case "cd":
|
|
autoCompleteCommands = paths
|
|
case "man":
|
|
autoCompleteCommands = baseCommands
|
|
case "cat":
|
|
autoCompleteCommands = paths
|
|
case "head":
|
|
autoCompleteCommands = paths
|
|
flagsDictionary = []string{
|
|
"-n", "-help",
|
|
}
|
|
case "touch":
|
|
autoCompleteCommands = paths
|
|
case "rm":
|
|
autoCompleteCommands = paths
|
|
case "cp":
|
|
autoCompleteCommands = paths
|
|
nextAutoCompleteCommands = paths
|
|
case "mv":
|
|
autoCompleteCommands = paths
|
|
nextAutoCompleteCommands = paths
|
|
case "help":
|
|
skipCurrent = true
|
|
case "tail":
|
|
autoCompleteCommands = paths
|
|
flagsDictionary = []string{
|
|
"-n", "-help",
|
|
}
|
|
case "whereareyou":
|
|
skipCurrent = true
|
|
default:
|
|
skipCurrent = true
|
|
|
|
}
|
|
|
|
if skipCurrent {
|
|
return rawInput
|
|
}
|
|
|
|
if currentIndexFields == 1 {
|
|
// print("currentFieldhär om den har - :D", currentField)
|
|
if strings.ContainsAny(currentField, "-") {
|
|
noMoreFlags = true
|
|
}
|
|
}
|
|
|
|
if len(strings.Fields(input)) == 2 && input[len(input)-1:] == " " {
|
|
// currentIndexFields += 1
|
|
// println("den hittade mellanslag 2 :D")
|
|
acceptAll = true
|
|
autoCompleteCommands = nil
|
|
|
|
if noMoreFlags {
|
|
skipCurrent = true
|
|
return rawInput
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if currentIndexFields > 2 {
|
|
skipCurrent = true
|
|
return rawInput
|
|
}
|
|
|
|
if currentIndexFields == 2 && noMoreFlags {
|
|
skipCurrent = true
|
|
return rawInput
|
|
}
|
|
|
|
if len(strings.Fields(input)) == 2 && input[len(input)-1:] == "-" {
|
|
autoCompleteCommands = flagsDictionary
|
|
|
|
}
|
|
|
|
if currentIndexFields > 0 && len(autoCompleteCommands) == 0 || currentIndexFields > 0 && strings.ContainsAny(rawInput, "-") {
|
|
// println("currentIdexFields är 2!!")
|
|
|
|
// if len(flagsDictionary) == 0 {
|
|
// skipCurrent = true
|
|
// return rawInput
|
|
// }
|
|
|
|
// } else {
|
|
// println("input", input, "test", input[len(input)-1:], "slut")
|
|
// if input[len(input)-1:] == " " {
|
|
// println("acceptAll")
|
|
// acceptAll = true
|
|
// }
|
|
// }
|
|
// println("currentField 2 :D", currentField)
|
|
autoCompleteCommands = nextAutoCompleteCommands
|
|
// println("nextAutoCompleteCommands", nextAutoCompleteCommands)
|
|
// println("autoCompleteCommands", autoCompleteCommands)
|
|
if len(nextAutoCompleteCommands) == 0 {
|
|
// println("currentField 100 :D", currentField)
|
|
// if strings.ContainsAny(rawInput, "-") {
|
|
autoCompleteCommands = flagsDictionary
|
|
// print("autoCompleteCommands", autoCompleteCommands)
|
|
|
|
// if currentIndexFields == 1 {
|
|
// noMoreFlags = true
|
|
// }
|
|
// } else {
|
|
// if len(autoCompleteCommands) == 0 {
|
|
// skipCurrent = true
|
|
// return rawInput
|
|
// }
|
|
// break
|
|
}
|
|
|
|
}
|
|
|
|
if currentIndexFields > 0 {
|
|
input = currentField
|
|
}
|
|
|
|
// println("loop", indexFields)
|
|
|
|
// if strings.ContainsAny(currentField, "-") {
|
|
|
|
// }
|
|
}
|
|
|
|
// println("autoCompleteCOmmands", autoCompleteCommands)
|
|
// println("autoCompleteCOmmands", autoCompleteCommands[0])
|
|
|
|
if autoCompleteCommands == nil {
|
|
skipCurrent = true
|
|
return rawInput
|
|
|
|
}
|
|
|
|
var commandsWorking []string
|
|
|
|
// println(commandsWorking)
|
|
|
|
inputLength = len(input)
|
|
|
|
aLength := 0
|
|
|
|
foundAutoComplete := false
|
|
|
|
for _, currentCommand := range autoCompleteCommands {
|
|
// println("currentCommand")
|
|
// println(currentCommand)
|
|
|
|
if len(currentCommand) < inputLength && !acceptAll {
|
|
continue
|
|
}
|
|
|
|
if !acceptAll {
|
|
currentCommandWithoutEnd := currentCommand[:inputLength]
|
|
if currentCommandWithoutEnd != input {
|
|
continue
|
|
}
|
|
}
|
|
|
|
foundAutoComplete = true
|
|
|
|
aLength = len(currentCommand) - len(input)
|
|
aLength = len(currentCommand) - aLength
|
|
|
|
// println("aLength")
|
|
// println(aLength)
|
|
|
|
// println("currentCommand")
|
|
// println(currentCommand)
|
|
|
|
// println("rawInput")
|
|
// println(rawInput)
|
|
|
|
if acceptAll {
|
|
commandsWorking = append(commandsWorking, rawInput+currentCommand)
|
|
} else {
|
|
commandsWorking = append(commandsWorking, rawInput+currentCommand[aLength:])
|
|
}
|
|
|
|
}
|
|
|
|
if !foundAutoComplete {
|
|
skipCurrent = true
|
|
return rawInput
|
|
}
|
|
|
|
// println("commandsWorking")
|
|
// println(commandsWorking[0])
|
|
// println(commandsWorking[1])
|
|
|
|
commandsTab = commandsWorking
|
|
|
|
if len(commandsTab) == 0 {
|
|
return input
|
|
}
|
|
|
|
removedStart := commandsTab[0]
|
|
// removedStart := commandsTab[0][inputLength:]
|
|
|
|
prevInput = removedStart
|
|
return prevInput
|
|
}
|