Better input, now with ctrl

This commit is contained in:
Tobias P.L Wennberg 2025-01-14 10:39:22 +01:00
parent ada7fa9d9e
commit ed3067a9a0
2 changed files with 83 additions and 22 deletions

View File

@ -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
}

View File

@ -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: // <CTR><BACKSPACE>
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)