From ed3067a9a023cc101e29509ad57890edc949d5c2 Mon Sep 17 00:00:00 2001 From: "Tobias P.L Wennberg" Date: Tue, 14 Jan 2025 10:39:22 +0100 Subject: [PATCH] Better input, now with ctrl --- input_parser/escape_handler.go | 76 +++++++++++++++++++++++++--------- input_parser/input_parser.go | 29 ++++++++++++- 2 files changed, 83 insertions(+), 22 deletions(-) diff --git a/input_parser/escape_handler.go b/input_parser/escape_handler.go index d2f8b68..4c586d4 100644 --- a/input_parser/escape_handler.go +++ b/input_parser/escape_handler.go @@ -3,18 +3,19 @@ package input_parser import ( "bbash/environment" "bufio" + "fmt" + "strings" ) func Escape_handler(reader *bufio.Reader, history_index int, input string, env environment.Env, cursor int) (string, int, int) { next_rune, _, _ := reader.ReadRune() - //fmt.Print(next_rune) + if next_rune == 59 { + } if next_rune != 91 { // not "[" return "", history_index, cursor } next_rune, _, _ = reader.ReadRune() - //fmt.Println("print" + strconv.QuoteRune(next_rune)) - switch next_rune { case 65: //A if history_index > 0 { @@ -45,25 +46,60 @@ func Escape_handler(reader *bufio.Reader, history_index int, input string, env e if cursor < len(input) { cursor++ } - } - - /* if r, _, _ := reader.ReadRune(); r == 65 { // UPP - if history_index > 0 { - history_index-- - input = env.History[history_index] - } - break + case 49: + cursor = ctrl(reader, input, cursor) + return input, history_index, cursor } - if r, _, _ := reader.ReadRune(); r != 66 { //DOWN - if history_index < len(env.History)-1 { - history_index++ - input = env.History[history_index] - } else { - input = "" - } - break - } */ + return input, history_index, cursor } +func ctrl(reader *bufio.Reader, input string, cursor int) int { + next_rune, _, err := reader.ReadRune() + if err != nil { + fmt.Println(err.Error()) + return cursor + } + if next_rune != 59 { + return cursor + } + next_rune, _, err = reader.ReadRune() + if err != nil { + fmt.Println(err.Error()) + return cursor + } + if next_rune != 53 { + return cursor + } + next_rune, _, err = reader.ReadRune() + if err != nil { + fmt.Println(err.Error()) + return cursor + } + switch next_rune { + case 68: // ctrl left + input_at := input[:cursor] + last_index := strings.LastIndex(input_at, " ") + if last_index == -1 { + cursor = 0 + } else { + input_at = input_at[:last_index] + cursor = len(input_at) + } + case 67: // ctrl right + input_next := input[cursor:] + next_trimmed := strings.TrimLeft(input_next, " ") + last_index := strings.Index(next_trimmed, " ") + if last_index == -1 { + cursor = len(input) + } else { + cursor = cursor + last_index + (len(input_next) - len(next_trimmed)) + } + } + return cursor + + + +} + diff --git a/input_parser/input_parser.go b/input_parser/input_parser.go index 7c97715..c9a4eb5 100644 --- a/input_parser/input_parser.go +++ b/input_parser/input_parser.go @@ -86,6 +86,12 @@ func input_str(env *environment.Env) string { fmt.Print(input) for { + if cursor > len(input) { + cursor = len(input) + } + if cursor < 0 { + cursor = 0 + } r_rune, _, err := reader.ReadRune() if err != nil { fmt.Print(fmt.Sprintf("Error reading user input: %s", err.Error())) @@ -98,21 +104,40 @@ func input_str(env *environment.Env) string { case 4: // ^D input = "exit" goto loop_exit + case 8: // + input_at := input[:cursor] + input_after := input[cursor:] + last_index := strings.LastIndex(input_at, " ") + if last_index == -1 { + input_at = "" + cursor = 0 + } else { + input_at = input_at[:last_index] + cursor = len(input_at) + } + input = input_at+input_after + case 9: // TAB input = AutoComplete(input) cursor = len(input) case 27: // Escape input, history_index, cursor = Escape_handler(reader, history_index, input, *env, cursor) case 127: //packspace + input_at := input[:cursor] + input_after := input[cursor:] if len(input) > 0 { - input = input[:len(input)-1] + input_at = input_at[:len(input_at)-1] cursor-- } + input=input_at+input_after case 13: // Enter goto loop_exit default: + input_at := input[:cursor] + input_after := input[cursor:] + input_at += string(r_rune) + input = input_at+input_after cursor++ - input = input + string(r_rune) } ghost_input := AutoCompleteHistory(input, *env)