diff --git a/input_parser/auto_complete.go b/input_parser/auto_complete.go index 0e77a3d..20d28f1 100644 --- a/input_parser/auto_complete.go +++ b/input_parser/auto_complete.go @@ -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 diff --git a/input_parser/auto_complete_history.go b/input_parser/auto_complete_history.go new file mode 100644 index 0000000..11368a2 --- /dev/null +++ b/input_parser/auto_complete_history.go @@ -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 +} diff --git a/input_parser/input_parser.go b/input_parser/input_parser.go index d793e2c..e5d26e5 100644 --- a/input_parser/input_parser.go +++ b/input_parser/input_parser.go @@ -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)