implemened cursor control and better escape char handling

This commit is contained in:
Hansson 2025-01-14 09:04:38 +01:00
parent 2661860245
commit b071d3dd78
2 changed files with 77 additions and 21 deletions

View File

@ -0,0 +1,63 @@
package input_parser
import (
"bbash/environment"
"bufio"
)
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 != 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 {
history_index--
input = env.History[history_index]
}
case 66: //B
if history_index < len(env.History)-1 {
history_index++
input = env.History[history_index]
}
if history_index == len(env.History)-1 {
input = ""
cursor = 0
}
case 68: //D
if cursor > 0 {
cursor--
}
case 67: //C
if cursor < len(input) {
cursor++
}
}
/* if r, _, _ := reader.ReadRune(); r == 65 { // UPP
if history_index > 0 {
history_index--
input = env.History[history_index]
}
break
}
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
}

View File

@ -78,6 +78,8 @@ func input_str(env *environment.Env) string {
history_index := len(env.History) // not -1 since we start at the new one
env.History = append(env.History, input)
cursor := 0
env.History[len(env.History)-1] = input
fmt.Print("\r\033[K")
fmt.Print(env.Path)
@ -101,35 +103,18 @@ func input_str(env *environment.Env) string {
input = AutoComplete(input)
case 27: // arrow?
if r, _, _ := reader.ReadRune(); r != 91 {
break
}
if r, _, _ := reader.ReadRune(); r == 65 { // UPP
if history_index > 0 {
history_index--
input = env.History[history_index]
}
break
}
if r, _, _ := reader.ReadRune(); r != 66 { //DOWN
if history_index < len(env.History)-1 {
history_index++
input = env.History[history_index]
} else {
input = ""
}
break
}
case 27: // Escape
input, history_index, cursor = Escape_handler(reader, history_index, input, *env, cursor)
case 127: //packspace
if len(input) > 0 {
input = input[:len(input)-1]
cursor--
}
case 13: // Enter
goto loop_exit
default:
cursor++
input = input + string(r_rune)
}
env.History[len(env.History)-1] = input
@ -141,6 +126,14 @@ func input_str(env *environment.Env) string {
fmt.Print(" > ")
fmt.Print("\033[K")
fmt.Print(input)
fmt.Print("\r")
fmt.Print(env.Path)
fmt.Print(" > ")
for i := 0; i < cursor; i++ {
fmt.Print("\033[C")
}
}
loop_exit:
term.Restore(int(std_fd), term_restore)