fixade autocomplete
This commit is contained in:
parent
2f6f75b3a4
commit
9bbaae1d11
@ -1,11 +1,19 @@
|
|||||||
package input_parser
|
package input_parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bbash/environment"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
var commandsTab []string
|
var commandsTab []string
|
||||||
var prevInput string
|
var prevInput string
|
||||||
var tabIndex int
|
var tabIndex int
|
||||||
|
var skipCurrent bool
|
||||||
|
|
||||||
func AutoComplete(input string) string {
|
func AutoComplete(input string, env environment.Env) string {
|
||||||
|
|
||||||
|
rawInput := input
|
||||||
inputLength := len(input)
|
inputLength := len(input)
|
||||||
|
|
||||||
if inputLength == 0 {
|
if inputLength == 0 {
|
||||||
@ -14,6 +22,10 @@ func AutoComplete(input string) string {
|
|||||||
|
|
||||||
if prevInput == input {
|
if prevInput == input {
|
||||||
|
|
||||||
|
if skipCurrent {
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
|
||||||
tabIndex = tabIndex + 1
|
tabIndex = tabIndex + 1
|
||||||
|
|
||||||
if len(commandsTab) == 0 && tabIndex > 0 {
|
if len(commandsTab) == 0 && tabIndex > 0 {
|
||||||
@ -31,7 +43,9 @@ func AutoComplete(input string) string {
|
|||||||
prevInput = input
|
prevInput = input
|
||||||
tabIndex = 0
|
tabIndex = 0
|
||||||
|
|
||||||
currentCommands := []string{
|
var flagsDictionary []string
|
||||||
|
|
||||||
|
baseCommands := []string{
|
||||||
"cat",
|
"cat",
|
||||||
"cd",
|
"cd",
|
||||||
"cp",
|
"cp",
|
||||||
@ -50,34 +64,267 @@ func AutoComplete(input string) string {
|
|||||||
"whereareyou",
|
"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
|
var commandsWorking []string
|
||||||
|
|
||||||
// println(commandsWorking)
|
// println(commandsWorking)
|
||||||
|
|
||||||
for _, currentCommand := range currentCommands {
|
inputLength = len(input)
|
||||||
|
|
||||||
|
aLength := 0
|
||||||
|
|
||||||
|
foundAutoComplete := false
|
||||||
|
|
||||||
|
for _, currentCommand := range autoCompleteCommands {
|
||||||
// println("currentCommand")
|
// println("currentCommand")
|
||||||
// println(currentCommand)
|
// println(currentCommand)
|
||||||
|
|
||||||
if len(currentCommand) < inputLength {
|
if len(currentCommand) < inputLength && !acceptAll {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
currentCommandWithoutEnd := currentCommand[:inputLength]
|
if !acceptAll {
|
||||||
|
currentCommandWithoutEnd := currentCommand[:inputLength]
|
||||||
if currentCommandWithoutEnd != input {
|
if currentCommandWithoutEnd != input {
|
||||||
continue
|
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
|
commandsTab = commandsWorking
|
||||||
|
|
||||||
if len(commandsTab) == 0 {
|
if len(commandsTab) == 0 {
|
||||||
return input
|
return input
|
||||||
}
|
}
|
||||||
|
|
||||||
prevInput = commandsTab[0]
|
removedStart := commandsTab[0]
|
||||||
return commandsTab[0]
|
// removedStart := commandsTab[0][inputLength:]
|
||||||
|
|
||||||
|
prevInput = removedStart
|
||||||
|
return prevInput
|
||||||
}
|
}
|
||||||
|
|||||||
@ -96,7 +96,7 @@ func input_str(env *environment.Env) string {
|
|||||||
input = "exit"
|
input = "exit"
|
||||||
goto loop_exit
|
goto loop_exit
|
||||||
case 9: // TAB
|
case 9: // TAB
|
||||||
input = AutoComplete(input)
|
input = AutoComplete(input, *env)
|
||||||
case 27: // UPP
|
case 27: // UPP
|
||||||
if r, _, _ := reader.ReadRune(); r != 91 {
|
if r, _, _ := reader.ReadRune(); r != 91 {
|
||||||
break
|
break
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user