Better input, now with ctrl
This commit is contained in:
parent
ada7fa9d9e
commit
ed3067a9a0
@ -3,18 +3,19 @@ package input_parser
|
|||||||
import (
|
import (
|
||||||
"bbash/environment"
|
"bbash/environment"
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Escape_handler(reader *bufio.Reader, history_index int, input string, env environment.Env, cursor int) (string, int, int) {
|
func Escape_handler(reader *bufio.Reader, history_index int, input string, env environment.Env, cursor int) (string, int, int) {
|
||||||
next_rune, _, _ := reader.ReadRune()
|
next_rune, _, _ := reader.ReadRune()
|
||||||
//fmt.Print(next_rune)
|
if next_rune == 59 {
|
||||||
|
}
|
||||||
if next_rune != 91 { // not "["
|
if next_rune != 91 { // not "["
|
||||||
return "", history_index, cursor
|
return "", history_index, cursor
|
||||||
}
|
}
|
||||||
next_rune, _, _ = reader.ReadRune()
|
next_rune, _, _ = reader.ReadRune()
|
||||||
|
|
||||||
//fmt.Println("print" + strconv.QuoteRune(next_rune))
|
|
||||||
|
|
||||||
switch next_rune {
|
switch next_rune {
|
||||||
case 65: //A
|
case 65: //A
|
||||||
if history_index > 0 {
|
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) {
|
if cursor < len(input) {
|
||||||
cursor++
|
cursor++
|
||||||
}
|
}
|
||||||
}
|
case 49:
|
||||||
|
cursor = ctrl(reader, input, cursor)
|
||||||
/* if r, _, _ := reader.ReadRune(); r == 65 { // UPP
|
return input, history_index, cursor
|
||||||
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
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -86,6 +86,12 @@ func input_str(env *environment.Env) string {
|
|||||||
fmt.Print(input)
|
fmt.Print(input)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
if cursor > len(input) {
|
||||||
|
cursor = len(input)
|
||||||
|
}
|
||||||
|
if cursor < 0 {
|
||||||
|
cursor = 0
|
||||||
|
}
|
||||||
r_rune, _, err := reader.ReadRune()
|
r_rune, _, err := reader.ReadRune()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Print(fmt.Sprintf("Error reading user input: %s", err.Error()))
|
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
|
case 4: // ^D
|
||||||
input = "exit"
|
input = "exit"
|
||||||
goto loop_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
|
case 9: // TAB
|
||||||
input = AutoComplete(input)
|
input = AutoComplete(input)
|
||||||
cursor = len(input)
|
cursor = len(input)
|
||||||
case 27: // Escape
|
case 27: // Escape
|
||||||
input, history_index, cursor = Escape_handler(reader, history_index, input, *env, cursor)
|
input, history_index, cursor = Escape_handler(reader, history_index, input, *env, cursor)
|
||||||
case 127: //packspace
|
case 127: //packspace
|
||||||
|
input_at := input[:cursor]
|
||||||
|
input_after := input[cursor:]
|
||||||
if len(input) > 0 {
|
if len(input) > 0 {
|
||||||
input = input[:len(input)-1]
|
input_at = input_at[:len(input_at)-1]
|
||||||
cursor--
|
cursor--
|
||||||
}
|
}
|
||||||
|
input=input_at+input_after
|
||||||
case 13: // Enter
|
case 13: // Enter
|
||||||
goto loop_exit
|
goto loop_exit
|
||||||
default:
|
default:
|
||||||
|
input_at := input[:cursor]
|
||||||
|
input_after := input[cursor:]
|
||||||
|
input_at += string(r_rune)
|
||||||
|
input = input_at+input_after
|
||||||
cursor++
|
cursor++
|
||||||
input = input + string(r_rune)
|
|
||||||
}
|
}
|
||||||
ghost_input := AutoCompleteHistory(input, *env)
|
ghost_input := AutoCompleteHistory(input, *env)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user