History auto komplett :D

This commit is contained in:
Lo 2025-01-13 15:28:58 +01:00
parent eaf6c974af
commit 45f50a95ce
3 changed files with 98 additions and 3 deletions

View File

@ -6,8 +6,18 @@ 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
}
@ -19,6 +29,10 @@ func AutoComplete(input string) string {
if prevInput == input {
tabIndex = tabIndex + 1
if len(commandsTab) == 0 && tabIndex > -1 {
return input
}
prevInput = commandsTab[tabIndex]
return commandsTab[tabIndex]
}
@ -44,7 +58,11 @@ func AutoComplete(input string) string {
var commandsWorking []string
// println(commandsWorking)
for _, currentCommand := range currentCommands {
// println("currentCommand")
// println(currentCommand)
if len(currentCommand) < inputLength {
continue
@ -57,6 +75,7 @@ func AutoComplete(input string) string {
}
commandsWorking = append(commandsWorking, currentCommand)
}
commandsTab = commandsWorking

View File

@ -0,0 +1,55 @@
package input_parser
import "bbash/environment"
func AutoCompleteHistory(input string, env environment.Env) string {
inputLength := len(input)
if inputLength == 0 {
return ""
}
tabIndex = 0
// currentCommands := []string{
// "cat",
// "cd",
// "cp",
// "echo",
// "head",
// "help",
// "ls",
// "man",
// "mv",
// "pwd",
// "rm",
// "tail",
// "touch",
// }
historyWorking := ""
for _, currentHistory := range env.History[:len(env.History)-1] {
// println("currentHistory", currentHistory)
if len(currentHistory) < inputLength {
continue
}
currentCommandWithoutEnd := currentHistory[:inputLength]
if currentCommandWithoutEnd != input {
continue
}
historyWorking = currentHistory
}
if len(historyWorking) != 0 {
historyWorking = historyWorking[inputLength:]
}
return historyWorking
}

View File

@ -10,6 +10,8 @@ import (
"golang.org/x/term"
)
var prevGhostString string
type Input struct {
// The instruction a.k.a first argument
Instruction string
@ -22,9 +24,9 @@ type Input struct {
func Parse(env *environment.Env) Input {
input := input_str(env)
return parse_input(input)
return Parse_input(input)
}
func parse_input(input string) Input {
func Parse_input(input string) Input {
split := strings.Split(string(input), " ")
instruction := strings.TrimSpace(split[0])
@ -86,6 +88,9 @@ func input_str(env *environment.Env) string {
for {
r_rune, _, err := reader.ReadRune()
// fmt.Printf("Rune: %d\n", r_rune)
if err != nil {
fmt.Print(fmt.Sprintf("Error reading user input: ", err.Error()))
}
@ -98,8 +103,9 @@ func input_str(env *environment.Env) string {
goto loop_exit
case 9: // Tab
// fmt.Println("test tab")
input = AutoComplete(input)
// fmt.Println(input)
case 27: // arrow?
if r, _, _ := reader.ReadRune(); r != 91 {
@ -122,6 +128,8 @@ func input_str(env *environment.Env) string {
}
break
}
case 62:
input = input + prevGhostString
case 127: //packspace
if len(input) > 0 {
@ -132,6 +140,9 @@ func input_str(env *environment.Env) string {
default:
input = input + string(r_rune)
}
ghost_input := AutoCompleteHistory(input, *env)
env.History[len(env.History)-1] = input
//fmt.Println(r_rune)
fmt.Print("\r")
@ -141,6 +152,16 @@ func input_str(env *environment.Env) string {
fmt.Print(" > ")
fmt.Print("\033[K")
fmt.Print(input)
fmt.Print("\033[2m", ghost_input, "\033[0m")
prevGhostString = ""
backwardSteps := len(ghost_input)
if backwardSteps != 0 {
fmt.Printf("\033[%dD", backwardSteps)
prevGhostString = ghost_input
}
}
loop_exit:
term.Restore(int(std_fd), term_restore)