From eaf6c974af83e1a9b3fc6c0278717156c3592b53 Mon Sep 17 00:00:00 2001 From: Lo Date: Mon, 13 Jan 2025 13:37:10 +0100 Subject: [PATCH] =?UTF-8?q?Fixed=20first=20version=20of=20auto=20complete.?= =?UTF-8?q?=20And=20fixade=20the=20input=20sak=20s=C3=A5=20that=20it=20did?= =?UTF-8?q?nt=20blinkade=20s=C3=A5=20much!=20:D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- input_parser/auto_complete.go | 70 +++++++++++++++++++++++++++++++++++ input_parser/input_parser.go | 14 +++++-- 2 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 input_parser/auto_complete.go diff --git a/input_parser/auto_complete.go b/input_parser/auto_complete.go new file mode 100644 index 0000000..0e77a3d --- /dev/null +++ b/input_parser/auto_complete.go @@ -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] +} diff --git a/input_parser/input_parser.go b/input_parser/input_parser.go index 0cae4cf..d793e2c 100644 --- a/input_parser/input_parser.go +++ b/input_parser/input_parser.go @@ -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,12 +134,15 @@ 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: +loop_exit: term.Restore(int(std_fd), term_restore) fmt.Println() return input -- 2.50.1