From 9bbaae1d11a0deb541c81e6e50bfbab450d10ca1 Mon Sep 17 00:00:00 2001 From: Lo Date: Tue, 14 Jan 2025 13:22:09 +0100 Subject: [PATCH] fixade autocomplete --- input_parser/auto_complete.go | 269 ++++++++++++++++++++++++++++++++-- input_parser/input_parser.go | 2 +- 2 files changed, 259 insertions(+), 12 deletions(-) diff --git a/input_parser/auto_complete.go b/input_parser/auto_complete.go index bcb55e9..19e76b0 100644 --- a/input_parser/auto_complete.go +++ b/input_parser/auto_complete.go @@ -1,11 +1,19 @@ package input_parser +import ( + "bbash/environment" + "os" + "strings" +) + var commandsTab []string var prevInput string var tabIndex int +var skipCurrent bool -func AutoComplete(input string) string { +func AutoComplete(input string, env environment.Env) string { + rawInput := input inputLength := len(input) if inputLength == 0 { @@ -14,6 +22,10 @@ func AutoComplete(input string) string { if prevInput == input { + if skipCurrent { + return input + } + tabIndex = tabIndex + 1 if len(commandsTab) == 0 && tabIndex > 0 { @@ -31,7 +43,9 @@ func AutoComplete(input string) string { prevInput = input tabIndex = 0 - currentCommands := []string{ + var flagsDictionary []string + + baseCommands := []string{ "cat", "cd", "cp", @@ -50,34 +64,267 @@ func AutoComplete(input string) string { "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) - for _, currentCommand := range currentCommands { + inputLength = len(input) + + aLength := 0 + + foundAutoComplete := false + + for _, currentCommand := range autoCompleteCommands { // println("currentCommand") // println(currentCommand) - if len(currentCommand) < inputLength { + if len(currentCommand) < inputLength && !acceptAll { continue } - currentCommandWithoutEnd := currentCommand[:inputLength] - - if currentCommandWithoutEnd != input { - continue + if !acceptAll { + currentCommandWithoutEnd := currentCommand[:inputLength] + if currentCommandWithoutEnd != input { + continue + } } - commandsWorking = append(commandsWorking, currentCommand) + 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 } - prevInput = commandsTab[0] - return commandsTab[0] + removedStart := commandsTab[0] + // removedStart := commandsTab[0][inputLength:] + + prevInput = removedStart + return prevInput } diff --git a/input_parser/input_parser.go b/input_parser/input_parser.go index 90394be..8c5a5ef 100644 --- a/input_parser/input_parser.go +++ b/input_parser/input_parser.go @@ -96,7 +96,7 @@ func input_str(env *environment.Env) string { input = "exit" goto loop_exit case 9: // TAB - input = AutoComplete(input) + input = AutoComplete(input, *env) case 27: // UPP if r, _, _ := reader.ReadRune(); r != 91 { break