Fixed first version of auto complete. And fixade the input sak så that it didnt blinkade så much! :D #4

Merged
t merged 1 commits from lo_branch into master 2025-01-13 13:43:04 +01:00
2 changed files with 81 additions and 3 deletions

View File

@ -0,0 +1,70 @@
package input_parser
var commandsTab []string
var prevInput string
var tabIndex int
func AutoComplete(input string) string {
inputLength := len(input)
if inputLength == 0 {
return input
}
if tabIndex+2 > len(commandsTab) {
tabIndex = -1
}
if prevInput == input {
tabIndex = tabIndex + 1
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
for _, currentCommand := range currentCommands {
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]
}

View File

@ -96,7 +96,12 @@ func input_str(env *environment.Env) string {
case 4: // ^D
input = "exit"
goto loop_exit
case 27: // UPP
case 9: // Tab
input = AutoComplete(input)
case 27: // arrow?
if r, _, _ := reader.ReadRune(); r != 91 {
break
}
@ -129,9 +134,12 @@ func input_str(env *environment.Env) string {
}
env.History[len(env.History)-1] = input
//fmt.Println(r_rune)
fmt.Print("\r\033[K")
fmt.Print("\r")
// fmt.Print("\r\033[K")
fmt.Print(env.Path)
fmt.Print(" > ")
fmt.Print("\033[K")
fmt.Print(input)
}
loop_exit: