1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vrepl: add support for Home and End keys (#16116)

This commit is contained in:
locriacyber
2022-10-20 17:07:57 +00:00
committed by GitHub
parent f8a28b5a5d
commit a3b050aced
5 changed files with 72 additions and 60 deletions

View File

@@ -122,8 +122,8 @@ pub fn (mut r Readline) disable_raw_mode() {
}
// read_char reads a single character.
pub fn (r Readline) read_char() int {
return utf8_getchar()
pub fn (r Readline) read_char() !int {
return int(term.utf8_getchar() or { return err })
}
// read_line_utf8 blocks execution in a loop and awaits user input
@@ -151,7 +151,7 @@ pub fn (mut r Readline) read_line_utf8(prompt string) ?[]rune {
print(r.prompt)
for {
unsafe { C.fflush(C.stdout) }
c := r.read_char()
c := r.read_char() or { return err }
a := r.analyse(c)
if r.execute(a, c) {
break
@@ -239,15 +239,17 @@ fn (r Readline) analyse(c int) Action {
// analyse_control returns an `Action` based on the type of input read by `read_char`.
fn (r Readline) analyse_control() Action {
c := r.read_char()
c := r.read_char() or { panic('Control sequence incomplete') }
match u8(c) {
`[` {
sequence := r.read_char()
sequence := r.read_char() or { panic('Control sequence incomplete') }
match u8(sequence) {
`C` { return .move_cursor_right }
`D` { return .move_cursor_left }
`B` { return .history_next }
`A` { return .history_previous }
`H` { return .move_cursor_begining }
`F` { return .move_cursor_end }
`1` { return r.analyse_extended_control() }
`2`, `3` { return r.analyse_extended_control_no_eat(u8(sequence)) }
else {}
@@ -259,7 +261,7 @@ fn (r Readline) analyse_control() Action {
//TODO
match c {
case `[`:
sequence := r.read_char()
sequence := r.read_char()?
match sequence {
case `C`: return .move_cursor_right
case `D`: return .move_cursor_left
@@ -282,11 +284,11 @@ match c {
// analyse_extended_control returns an `Action` based on the type of input read by `read_char`.
// analyse_extended_control specialises in cursor control.
fn (r Readline) analyse_extended_control() Action {
r.read_char() // Removes ;
c := r.read_char()
r.read_char() or { panic('Control sequence incomplete') } // Removes ;
c := r.read_char() or { panic('Control sequence incomplete') }
match u8(c) {
`5` {
direction := r.read_char()
direction := r.read_char() or { panic('Control sequence incomplete') }
match u8(direction) {
`C` { return .move_cursor_word_right }
`D` { return .move_cursor_word_left }
@@ -301,7 +303,7 @@ fn (r Readline) analyse_extended_control() Action {
// analyse_extended_control_no_eat returns an `Action` based on the type of input byte given in `c`.
// analyse_extended_control_no_eat specialises in detection of delete and insert keys.
fn (r Readline) analyse_extended_control_no_eat(last_c u8) Action {
c := r.read_char()
c := r.read_char() or { panic('Control sequence incomplete') }
match u8(c) {
`~` {
match last_c {