mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
repl: fix compilation after 6921d46
This commit is contained in:
parent
8b3990225a
commit
3b03edd7cb
@ -1,18 +1,15 @@
|
||||
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// Linux version
|
||||
// Will serve as more advanced input method
|
||||
// Based on the work of https://github.com/AmokHuginnsson/replxx
|
||||
|
||||
module readline
|
||||
|
||||
import term
|
||||
|
||||
#include <termios.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
// Defines actions to execute
|
||||
enum Action {
|
||||
eof
|
||||
@ -35,8 +32,10 @@ enum Action {
|
||||
}
|
||||
|
||||
fn C.tcgetattr() int
|
||||
|
||||
fn C.tcsetattr() int
|
||||
//fn C.ioctl() int
|
||||
|
||||
// fn C.ioctl() int
|
||||
fn C.raise()
|
||||
|
||||
// Enable the raw mode of the terminal
|
||||
@ -49,9 +48,9 @@ pub fn (mut r Readline) enable_raw_mode() {
|
||||
return
|
||||
}
|
||||
mut raw := r.orig_termios
|
||||
raw.c_iflag &= ~( C.BRKINT | C.ICRNL | C.INPCK | C.ISTRIP | C.IXON )
|
||||
raw.c_cflag |= ( C.CS8 )
|
||||
raw.c_lflag &= ~( C.ECHO | C.ICANON | C.IEXTEN | C.ISIG )
|
||||
raw.c_iflag &= ~(C.BRKINT | C.ICRNL | C.INPCK | C.ISTRIP | C.IXON)
|
||||
raw.c_cflag |= (C.CS8)
|
||||
raw.c_lflag &= ~(C.ECHO | C.ICANON | C.IEXTEN | C.ISIG)
|
||||
raw.c_cc[C.VMIN] = 1
|
||||
raw.c_cc[C.VTIME] = 0
|
||||
C.tcsetattr(0, C.TCSADRAIN, &raw)
|
||||
@ -68,9 +67,9 @@ pub fn (mut r Readline) enable_raw_mode_nosig() {
|
||||
return
|
||||
}
|
||||
mut raw := r.orig_termios
|
||||
raw.c_iflag &= ~( C.BRKINT | C.ICRNL | C.INPCK | C.ISTRIP | C.IXON )
|
||||
raw.c_cflag |= ( C.CS8 )
|
||||
raw.c_lflag &= ~( C.ECHO | C.ICANON | C.IEXTEN )
|
||||
raw.c_iflag &= ~(C.BRKINT | C.ICRNL | C.INPCK | C.ISTRIP | C.IXON)
|
||||
raw.c_cflag |= (C.CS8)
|
||||
raw.c_lflag &= ~(C.ECHO | C.ICANON | C.IEXTEN)
|
||||
raw.c_cc[C.VMIN] = 1
|
||||
raw.c_cc[C.VTIME] = 0
|
||||
C.tcsetattr(0, C.TCSADRAIN, &raw)
|
||||
@ -104,14 +103,12 @@ pub fn (mut r Readline) read_line_utf8(prompt string) ?ustring {
|
||||
if r.previous_lines.len <= 1 {
|
||||
r.previous_lines << ''.ustring()
|
||||
r.previous_lines << ''.ustring()
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
r.previous_lines[0] = ''.ustring()
|
||||
}
|
||||
if !r.is_raw {
|
||||
r.enable_raw_mode()
|
||||
}
|
||||
|
||||
print(r.prompt)
|
||||
for {
|
||||
C.fflush(C.stdout)
|
||||
@ -121,7 +118,6 @@ pub fn (mut r Readline) read_line_utf8(prompt string) ?ustring {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
r.previous_lines[0] = ''.ustring()
|
||||
r.search_index = 0
|
||||
r.disable_raw_mode()
|
||||
@ -161,10 +157,10 @@ pub fn read_line(prompt string) ?string {
|
||||
|
||||
fn get_prompt_offset(prompt string) int {
|
||||
mut len := 0
|
||||
|
||||
for i := 0; i < prompt.len; i++ {
|
||||
if prompt[i] == `\e` {
|
||||
for ;i < prompt.len && prompt[i] != `m`; i++ {}
|
||||
for ; i < prompt.len && prompt[i] != `m`; i++ {
|
||||
}
|
||||
} else {
|
||||
len = len + 1
|
||||
}
|
||||
@ -173,7 +169,7 @@ fn get_prompt_offset(prompt string) int {
|
||||
}
|
||||
|
||||
fn (r Readline) analyse(c int) Action {
|
||||
match c {
|
||||
match byte(c) {
|
||||
`\0` { return .eof }
|
||||
0x3 { return .eof } // End of Text
|
||||
0x4 { return .eof } // End of Transmission
|
||||
@ -187,17 +183,20 @@ fn (r Readline) analyse(c int) Action {
|
||||
1 { return .move_cursor_begining } // ^A
|
||||
5 { return .move_cursor_end } // ^E
|
||||
26 { return .suspend } // CTRL + Z, SUB
|
||||
else { return if c >= ` ` { Action.insert_character } else { Action.nothing } }
|
||||
else { return if c >= ` ` {
|
||||
Action.insert_character
|
||||
} else {
|
||||
Action.nothing
|
||||
} }
|
||||
}
|
||||
}
|
||||
|
||||
fn (r Readline) analyse_control() Action {
|
||||
c := r.read_char()
|
||||
|
||||
match c {
|
||||
match byte(c) {
|
||||
`[` {
|
||||
sequence := r.read_char()
|
||||
match sequence {
|
||||
match byte(sequence) {
|
||||
`C` { return .move_cursor_right }
|
||||
`D` { return .move_cursor_left }
|
||||
`B` { return .history_next }
|
||||
@ -208,12 +207,10 @@ match c {
|
||||
else {}
|
||||
}
|
||||
}
|
||||
else { }
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
//TODO
|
||||
else {}
|
||||
}
|
||||
/*
|
||||
//TODO
|
||||
match c {
|
||||
case `[`:
|
||||
sequence := r.read_char()
|
||||
@ -232,21 +229,17 @@ match c {
|
||||
}
|
||||
else:
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
return .nothing
|
||||
}
|
||||
|
||||
fn (r Readline) analyse_extended_control() Action {
|
||||
r.read_char() // Removes ;
|
||||
c := r.read_char()
|
||||
match c {
|
||||
match byte(c) {
|
||||
`5` {
|
||||
direction := r.read_char()
|
||||
match direction {
|
||||
match byte(direction) {
|
||||
`C` { return .move_cursor_word_right }
|
||||
`D` { return .move_cursor_word_left }
|
||||
else {}
|
||||
@ -259,14 +252,12 @@ fn (r Readline) analyse_extended_control() Action {
|
||||
|
||||
fn (r Readline) analyse_extended_control_no_eat(last_c byte) Action {
|
||||
c := r.read_char()
|
||||
match c {
|
||||
`~` {
|
||||
match last_c {
|
||||
match byte(c) {
|
||||
`~` { match last_c {
|
||||
`3` { return .delete_right } // Suppr key
|
||||
`2` { return .overwrite }
|
||||
else {}
|
||||
}
|
||||
}
|
||||
} }
|
||||
else {}
|
||||
}
|
||||
return .nothing
|
||||
@ -301,27 +292,27 @@ fn get_screen_columns() int {
|
||||
return cols
|
||||
}
|
||||
|
||||
fn shift_cursor(xpos int, yoffset int) {
|
||||
fn shift_cursor(xpos, yoffset int) {
|
||||
if yoffset != 0 {
|
||||
if yoffset > 0 {
|
||||
term.cursor_down(yoffset)
|
||||
}
|
||||
else {
|
||||
term.cursor_up(- yoffset)
|
||||
} else {
|
||||
term.cursor_up(-yoffset)
|
||||
}
|
||||
}
|
||||
// Absolute X position
|
||||
print('\x1b[${xpos + 1}G')
|
||||
print('\x1b[${xpos+1}G')
|
||||
}
|
||||
|
||||
fn calculate_screen_position(x_in int, y_in int, screen_columns int, char_count int, inp []int) []int {
|
||||
fn calculate_screen_position(x_in, y_in, screen_columns, char_count int, inp []int) []int {
|
||||
mut out := inp
|
||||
mut x := x_in
|
||||
mut y := y_in
|
||||
out[0] = x
|
||||
out[1] = y
|
||||
for chars_remaining := char_count; chars_remaining > 0; {
|
||||
chars_this_row := if (x + chars_remaining) < screen_columns { chars_remaining } else { screen_columns - x }
|
||||
chars_this_row := if (x + chars_remaining) < screen_columns { chars_remaining } else { screen_columns -
|
||||
x }
|
||||
out[0] = x + chars_this_row
|
||||
out[1] = y
|
||||
chars_remaining -= chars_this_row
|
||||
@ -338,11 +329,12 @@ fn calculate_screen_position(x_in int, y_in int, screen_columns int, char_count
|
||||
// Will redraw the line
|
||||
fn (mut r Readline) refresh_line() {
|
||||
mut end_of_input := [0, 0]
|
||||
end_of_input = calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.current.len, end_of_input)
|
||||
end_of_input = calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.current.len,
|
||||
end_of_input)
|
||||
end_of_input[1] += r.current.count('\n'.ustring())
|
||||
mut cursor_pos := [0, 0]
|
||||
cursor_pos = calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.cursor, cursor_pos)
|
||||
|
||||
cursor_pos = calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.cursor,
|
||||
cursor_pos)
|
||||
shift_cursor(0, -r.cursor_row_offset)
|
||||
term.erase_toend()
|
||||
print(r.prompt)
|
||||
@ -350,7 +342,7 @@ fn (mut r Readline) refresh_line() {
|
||||
if end_of_input[0] == 0 && end_of_input[1] > 0 {
|
||||
print('\n')
|
||||
}
|
||||
shift_cursor(cursor_pos[0] - r.prompt_offset, - (end_of_input[1] - cursor_pos[1]))
|
||||
shift_cursor(cursor_pos[0] - r.prompt_offset, -(end_of_input[1] - cursor_pos[1]))
|
||||
r.cursor_row_offset = cursor_pos[1]
|
||||
}
|
||||
|
||||
@ -366,9 +358,10 @@ fn (mut r Readline) eof() bool {
|
||||
|
||||
fn (mut r Readline) insert_character(c int) {
|
||||
if !r.overwrite || r.cursor == r.current.len {
|
||||
r.current = r.current.left(r.cursor).ustring().add( utf32_to_str(u32(c)).ustring() ).add( r.current.right(r.cursor).ustring() )
|
||||
r.current = r.current.left(r.cursor).ustring().add(utf32_to_str(u32(c)).ustring()).add(r.current.right(r.cursor).ustring())
|
||||
} else {
|
||||
r.current = r.current.left(r.cursor).ustring().add( utf32_to_str(u32(c)).ustring() ).add( r.current.right(r.cursor + 1).ustring() )
|
||||
r.current = r.current.left(r.cursor).ustring().add(utf32_to_str(u32(c)).ustring()).add(r.current.right(r.cursor +
|
||||
1).ustring())
|
||||
}
|
||||
r.cursor++
|
||||
// Refresh the line to add the new character
|
||||
@ -383,7 +376,7 @@ fn (mut r Readline) delete_character() {
|
||||
return
|
||||
}
|
||||
r.cursor--
|
||||
r.current = r.current.left(r.cursor).ustring().add( r.current.right(r.cursor + 1).ustring() )
|
||||
r.current = r.current.left(r.cursor).ustring().add(r.current.right(r.cursor + 1).ustring())
|
||||
r.refresh_line()
|
||||
}
|
||||
|
||||
@ -392,7 +385,7 @@ fn (mut r Readline) suppr_character() {
|
||||
if r.cursor > r.current.len {
|
||||
return
|
||||
}
|
||||
r.current = r.current.left(r.cursor).ustring().add( r.current.right(r.cursor + 1).ustring() )
|
||||
r.current = r.current.left(r.cursor).ustring().add(r.current.right(r.cursor + 1).ustring())
|
||||
r.refresh_line()
|
||||
}
|
||||
|
||||
@ -400,7 +393,7 @@ fn (mut r Readline) suppr_character() {
|
||||
fn (mut r Readline) commit_line() bool {
|
||||
r.previous_lines.insert(1, r.current)
|
||||
a := '\n'.ustring()
|
||||
r.current = r.current.add( a )
|
||||
r.current = r.current.add(a)
|
||||
r.cursor = r.current.len
|
||||
if r.is_tty {
|
||||
r.refresh_line()
|
||||
@ -441,16 +434,20 @@ fn (r Readline) is_break_character(c string) bool {
|
||||
|
||||
fn (mut r Readline) move_cursor_word_left() {
|
||||
if r.cursor > 0 {
|
||||
for ; r.cursor > 0 && r.is_break_character(r.current.at(r.cursor - 1)); r.cursor-- {}
|
||||
for ; r.cursor > 0 && !r.is_break_character(r.current.at(r.cursor - 1)); r.cursor-- {}
|
||||
for ; r.cursor > 0 && r.is_break_character(r.current.at(r.cursor - 1)); r.cursor-- {
|
||||
}
|
||||
for ; r.cursor > 0 && !r.is_break_character(r.current.at(r.cursor - 1)); r.cursor-- {
|
||||
}
|
||||
r.refresh_line()
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut r Readline) move_cursor_word_right() {
|
||||
if r.cursor < r.current.len {
|
||||
for ; r.cursor < r.current.len && r.is_break_character(r.current.at(r.cursor)); r.cursor++ {}
|
||||
for ; r.cursor < r.current.len && !r.is_break_character(r.current.at(r.cursor)); r.cursor++ {}
|
||||
for ; r.cursor < r.current.len && r.is_break_character(r.current.at(r.cursor)); r.cursor++ {
|
||||
}
|
||||
for ; r.cursor < r.current.len && !r.is_break_character(r.current.at(r.cursor)); r.cursor++ {
|
||||
}
|
||||
r.refresh_line()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user