2020-02-03 05:00:36 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-08-30 20:57:54 +02:00
|
|
|
// 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
|
2019-10-01 05:28:06 +02:00
|
|
|
// Based on the work of https://github.com/AmokHuginnsson/replxx
|
2019-08-30 20:57:54 +02:00
|
|
|
module readline
|
|
|
|
|
2019-08-31 19:09:02 +02:00
|
|
|
import term
|
2020-09-18 15:34:00 +02:00
|
|
|
import os
|
2019-08-31 19:09:02 +02:00
|
|
|
|
2019-08-30 20:57:54 +02:00
|
|
|
#include <termios.h>
|
2019-09-01 19:00:14 +02:00
|
|
|
#include <sys/ioctl.h>
|
2019-08-31 19:09:02 +02:00
|
|
|
// Defines actions to execute
|
|
|
|
enum Action {
|
2020-08-27 11:20:31 +02:00
|
|
|
eof
|
|
|
|
nothing
|
|
|
|
insert_character
|
|
|
|
commit_line
|
|
|
|
delete_left
|
|
|
|
delete_right
|
|
|
|
move_cursor_left
|
|
|
|
move_cursor_right
|
|
|
|
move_cursor_begining
|
|
|
|
move_cursor_end
|
|
|
|
move_cursor_word_left
|
|
|
|
move_cursor_word_right
|
|
|
|
history_previous
|
|
|
|
history_next
|
|
|
|
overwrite
|
|
|
|
clear_screen
|
|
|
|
suspend
|
2019-08-30 20:57:54 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 17:00:56 +01:00
|
|
|
fn C.tcgetattr() int
|
2020-08-27 11:20:31 +02:00
|
|
|
|
2019-11-22 17:00:56 +01:00
|
|
|
fn C.tcsetattr() int
|
2020-08-27 11:20:31 +02:00
|
|
|
|
2019-11-22 17:00:56 +01:00
|
|
|
fn C.raise()
|
|
|
|
|
2020-09-18 15:34:00 +02:00
|
|
|
fn C.getppid() int
|
|
|
|
|
2020-03-27 09:55:15 +01:00
|
|
|
// Enable the raw mode of the terminal
|
|
|
|
// In raw mode all keypresses are directly sent to the program and no interpretation is done
|
|
|
|
// Catches the SIGUSER (CTRL+C) Signal
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Readline) enable_raw_mode() {
|
2020-08-27 11:20:31 +02:00
|
|
|
if C.tcgetattr(0, &r.orig_termios) == -1 {
|
|
|
|
r.is_tty = false
|
|
|
|
r.is_raw = false
|
|
|
|
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_cc[C.VMIN] = 1
|
|
|
|
raw.c_cc[C.VTIME] = 0
|
|
|
|
C.tcsetattr(0, C.TCSADRAIN, &raw)
|
|
|
|
r.is_raw = true
|
|
|
|
r.is_tty = true
|
2019-08-30 20:57:54 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 09:55:15 +01:00
|
|
|
// Enable the raw mode of the terminal
|
|
|
|
// Does not catch the SIGUSER (CTRL+C) Signal
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Readline) enable_raw_mode_nosig() {
|
2020-08-27 11:20:31 +02:00
|
|
|
if C.tcgetattr(0, &r.orig_termios) == -1 {
|
|
|
|
r.is_tty = false
|
|
|
|
r.is_raw = false
|
|
|
|
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_cc[C.VMIN] = 1
|
|
|
|
raw.c_cc[C.VTIME] = 0
|
|
|
|
C.tcsetattr(0, C.TCSADRAIN, &raw)
|
|
|
|
r.is_raw = true
|
|
|
|
r.is_tty = true
|
2019-08-30 20:57:54 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 09:55:15 +01:00
|
|
|
// Disable the raw mode of the terminal
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Readline) disable_raw_mode() {
|
2020-08-27 11:20:31 +02:00
|
|
|
if r.is_raw {
|
|
|
|
C.tcsetattr(0, C.TCSADRAIN, &r.orig_termios)
|
|
|
|
r.is_raw = false
|
|
|
|
}
|
2019-08-30 20:57:54 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 09:55:15 +01:00
|
|
|
// Read a single char
|
|
|
|
pub fn (r Readline) read_char() int {
|
2020-08-27 11:20:31 +02:00
|
|
|
return utf8_getchar()
|
2019-08-30 20:57:54 +02:00
|
|
|
}
|
|
|
|
|
2019-08-31 19:09:02 +02:00
|
|
|
// Main function of the readline module
|
|
|
|
// Will loop and ingest characters until EOF or Enter
|
2019-10-06 15:28:41 +02:00
|
|
|
// Returns the completed line as utf8 ustring
|
|
|
|
// Will return an error if line is empty
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Readline) read_line_utf8(prompt string) ?ustring {
|
2020-08-27 11:20:31 +02:00
|
|
|
r.current = ''.ustring()
|
|
|
|
r.cursor = 0
|
|
|
|
r.prompt = prompt
|
|
|
|
r.search_index = 0
|
|
|
|
r.prompt_offset = get_prompt_offset(prompt)
|
|
|
|
if r.previous_lines.len <= 1 {
|
|
|
|
r.previous_lines << ''.ustring()
|
|
|
|
r.previous_lines << ''.ustring()
|
|
|
|
} else {
|
|
|
|
r.previous_lines[0] = ''.ustring()
|
|
|
|
}
|
|
|
|
if !r.is_raw {
|
|
|
|
r.enable_raw_mode()
|
|
|
|
}
|
|
|
|
print(r.prompt)
|
|
|
|
for {
|
|
|
|
C.fflush(C.stdout)
|
|
|
|
c := r.read_char()
|
|
|
|
a := r.analyse(c)
|
|
|
|
if r.execute(a, c) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.previous_lines[0] = ''.ustring()
|
|
|
|
r.search_index = 0
|
|
|
|
r.disable_raw_mode()
|
|
|
|
if r.current.s == '' {
|
|
|
|
return error('empty line')
|
|
|
|
}
|
|
|
|
return r.current
|
2019-08-31 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
2019-10-06 15:28:41 +02:00
|
|
|
// Returns the string from the utf8 ustring
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Readline) read_line(prompt string) ?string {
|
2020-10-21 14:24:45 +02:00
|
|
|
s := r.read_line_utf8(prompt) ?
|
2020-08-27 11:20:31 +02:00
|
|
|
return s.s
|
2019-10-06 15:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Standalone function without persistent functionnalities (eg: history)
|
|
|
|
// Returns utf8 based ustring
|
|
|
|
pub fn read_line_utf8(prompt string) ?ustring {
|
2020-08-27 11:20:31 +02:00
|
|
|
mut r := Readline{}
|
2020-10-21 14:24:45 +02:00
|
|
|
s := r.read_line_utf8(prompt) ?
|
2020-08-27 11:20:31 +02:00
|
|
|
return s
|
2019-10-06 15:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Standalone function without persistent functionnalities (eg: history)
|
|
|
|
// Return string from utf8 ustring
|
|
|
|
pub fn read_line(prompt string) ?string {
|
2020-08-27 11:20:31 +02:00
|
|
|
mut r := Readline{}
|
2020-10-21 14:24:45 +02:00
|
|
|
s := r.read_line(prompt) ?
|
2020-08-27 11:20:31 +02:00
|
|
|
return s
|
2019-09-23 12:43:24 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 17:33:21 +01:00
|
|
|
fn get_prompt_offset(prompt string) int {
|
2020-08-27 11:20:31 +02:00
|
|
|
mut len := 0
|
|
|
|
for i := 0; i < prompt.len; i++ {
|
|
|
|
if prompt[i] == `\e` {
|
|
|
|
for ; i < prompt.len && prompt[i] != `m`; i++ {
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
len = len + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prompt.len - len
|
2019-11-10 17:33:21 +01:00
|
|
|
}
|
|
|
|
|
2019-10-16 11:46:24 +02:00
|
|
|
fn (r Readline) analyse(c int) Action {
|
2020-08-27 11:20:31 +02:00
|
|
|
match byte(c) {
|
2020-10-15 07:08:27 +02:00
|
|
|
`\0`, 0x3, 0x4, 255 { return .eof } // NUL, End of Text, End of Transmission
|
|
|
|
`\n`, `\r` { return .commit_line }
|
2020-08-27 11:20:31 +02:00
|
|
|
`\f` { return .clear_screen } // CTRL + L
|
2020-10-15 07:08:27 +02:00
|
|
|
`\b`, 127 { return .delete_left } // BS, DEL
|
2020-08-27 11:20:31 +02:00
|
|
|
27 { return r.analyse_control() } // ESC
|
|
|
|
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
|
2020-10-21 14:24:45 +02:00
|
|
|
} }
|
2020-08-27 11:20:31 +02:00
|
|
|
}
|
2019-08-31 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (r Readline) analyse_control() Action {
|
2020-08-27 11:20:31 +02:00
|
|
|
c := r.read_char()
|
|
|
|
match byte(c) {
|
|
|
|
`[` {
|
|
|
|
sequence := r.read_char()
|
|
|
|
match byte(sequence) {
|
|
|
|
`C` { return .move_cursor_right }
|
|
|
|
`D` { return .move_cursor_left }
|
|
|
|
`B` { return .history_next }
|
|
|
|
`A` { return .history_previous }
|
|
|
|
`1` { return r.analyse_extended_control() }
|
2020-10-15 07:08:27 +02:00
|
|
|
`2`, `3` { return r.analyse_extended_control_no_eat(byte(sequence)) }
|
2020-08-27 11:20:31 +02:00
|
|
|
else {}
|
|
|
|
}
|
2019-12-07 16:16:19 +01:00
|
|
|
}
|
2020-08-27 11:20:31 +02:00
|
|
|
else {}
|
2019-12-07 16:16:19 +01:00
|
|
|
}
|
2020-08-27 11:20:31 +02:00
|
|
|
/*
|
|
|
|
//TODO
|
2019-12-07 16:16:19 +01:00
|
|
|
match c {
|
|
|
|
case `[`:
|
|
|
|
sequence := r.read_char()
|
|
|
|
match sequence {
|
|
|
|
case `C`: return .move_cursor_right
|
|
|
|
case `D`: return .move_cursor_left
|
|
|
|
case `B`: return .history_next
|
|
|
|
case `A`: return .history_previous
|
|
|
|
case `1`: return r.analyse_extended_control()
|
|
|
|
case `2`: return r.analyse_extended_control_no_eat(sequence)
|
|
|
|
case `3`: return r.analyse_extended_control_no_eat(sequence)
|
|
|
|
case `9`:
|
|
|
|
foo()
|
|
|
|
bar()
|
|
|
|
else:
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
}
|
2020-08-27 11:20:31 +02:00
|
|
|
*/
|
|
|
|
return .nothing
|
2019-08-31 20:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (r Readline) analyse_extended_control() Action {
|
2020-08-27 11:20:31 +02:00
|
|
|
r.read_char() // Removes ;
|
|
|
|
c := r.read_char()
|
|
|
|
match byte(c) {
|
|
|
|
`5` {
|
|
|
|
direction := r.read_char()
|
|
|
|
match byte(direction) {
|
|
|
|
`C` { return .move_cursor_word_right }
|
|
|
|
`D` { return .move_cursor_word_left }
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
return .nothing
|
2019-08-31 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
2019-09-04 20:08:41 +02:00
|
|
|
fn (r Readline) analyse_extended_control_no_eat(last_c byte) Action {
|
2020-08-27 11:20:31 +02:00
|
|
|
c := r.read_char()
|
|
|
|
match byte(c) {
|
|
|
|
`~` { match last_c {
|
|
|
|
`3` { return .delete_right } // Suppr key
|
|
|
|
`2` { return .overwrite }
|
|
|
|
else {}
|
|
|
|
} }
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
return .nothing
|
2019-09-02 12:14:46 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) execute(a Action, c int) bool {
|
2020-08-27 11:20:31 +02:00
|
|
|
match a {
|
|
|
|
.eof { return r.eof() }
|
|
|
|
.insert_character { r.insert_character(c) }
|
|
|
|
.commit_line { return r.commit_line() }
|
|
|
|
.delete_left { r.delete_character() }
|
|
|
|
.delete_right { r.suppr_character() }
|
|
|
|
.move_cursor_left { r.move_cursor_left() }
|
|
|
|
.move_cursor_right { r.move_cursor_right() }
|
|
|
|
.move_cursor_begining { r.move_cursor_begining() }
|
|
|
|
.move_cursor_end { r.move_cursor_end() }
|
|
|
|
.move_cursor_word_left { r.move_cursor_word_left() }
|
|
|
|
.move_cursor_word_right { r.move_cursor_word_right() }
|
|
|
|
.history_previous { r.history_previous() }
|
|
|
|
.history_next { r.history_next() }
|
|
|
|
.overwrite { r.switch_overwrite() }
|
|
|
|
.clear_screen { r.clear_screen() }
|
|
|
|
.suspend { r.suspend() }
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
return false
|
2019-08-31 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
2019-09-01 19:00:14 +02:00
|
|
|
fn get_screen_columns() int {
|
2020-08-27 11:20:31 +02:00
|
|
|
ws := Winsize{}
|
|
|
|
cols := if C.ioctl(1, C.TIOCGWINSZ, &ws) == -1 { 80 } else { int(ws.ws_col) }
|
|
|
|
return cols
|
|
|
|
}
|
|
|
|
|
2020-10-21 14:24:45 +02:00
|
|
|
fn shift_cursor(xpos int, yoffset int) {
|
2020-08-27 11:20:31 +02:00
|
|
|
if yoffset != 0 {
|
|
|
|
if yoffset > 0 {
|
|
|
|
term.cursor_down(yoffset)
|
|
|
|
} else {
|
|
|
|
term.cursor_up(-yoffset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Absolute X position
|
2020-10-21 14:24:45 +02:00
|
|
|
print('\x1b[${xpos + 1}G')
|
2020-08-27 11:20:31 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 14:24:45 +02:00
|
|
|
fn calculate_screen_position(x_in int, y_in int, screen_columns int, char_count int, inp []int) []int {
|
2020-08-27 11:20:31 +02:00
|
|
|
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 }
|
|
|
|
out[0] = x + chars_this_row
|
|
|
|
out[1] = y
|
|
|
|
chars_remaining -= chars_this_row
|
|
|
|
x = 0
|
|
|
|
y++
|
|
|
|
}
|
|
|
|
if out[0] == screen_columns {
|
|
|
|
out[0] = 0
|
|
|
|
out[1]++
|
|
|
|
}
|
|
|
|
return out
|
2019-09-01 19:00:14 +02:00
|
|
|
}
|
|
|
|
|
2019-08-31 19:09:02 +02:00
|
|
|
// Will redraw the line
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) refresh_line() {
|
2020-08-27 11:20:31 +02:00
|
|
|
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[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)
|
|
|
|
shift_cursor(0, -r.cursor_row_offset)
|
|
|
|
term.erase_toend()
|
|
|
|
print(r.prompt)
|
|
|
|
print(r.current)
|
|
|
|
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]))
|
|
|
|
r.cursor_row_offset = cursor_pos[1]
|
2019-08-31 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
2019-09-04 20:08:41 +02:00
|
|
|
// End the line without a newline
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) eof() bool {
|
2020-08-27 11:20:31 +02:00
|
|
|
r.previous_lines.insert(1, r.current)
|
|
|
|
r.cursor = r.current.len
|
|
|
|
if r.is_tty {
|
|
|
|
r.refresh_line()
|
|
|
|
}
|
|
|
|
return true
|
2019-09-04 20:08:41 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) insert_character(c int) {
|
2020-08-27 11:20:31 +02:00
|
|
|
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())
|
|
|
|
} 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.cursor++
|
|
|
|
// Refresh the line to add the new character
|
|
|
|
if r.is_tty {
|
|
|
|
r.refresh_line()
|
|
|
|
}
|
2019-08-31 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes the character behind cursor.
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) delete_character() {
|
2020-08-27 11:20:31 +02:00
|
|
|
if r.cursor <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.cursor--
|
|
|
|
r.current = r.current.left(r.cursor).ustring().add(r.current.right(r.cursor + 1).ustring())
|
|
|
|
r.refresh_line()
|
2019-08-31 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
2019-09-02 12:14:46 +02:00
|
|
|
// Removes the character in front of cursor.
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) suppr_character() {
|
2020-08-27 11:20:31 +02:00
|
|
|
if r.cursor > r.current.len {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.current = r.current.left(r.cursor).ustring().add(r.current.right(r.cursor + 1).ustring())
|
|
|
|
r.refresh_line()
|
2019-09-02 12:14:46 +02:00
|
|
|
}
|
|
|
|
|
2019-08-31 19:09:02 +02:00
|
|
|
// Add a line break then stops the main loop
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) commit_line() bool {
|
2020-08-27 11:20:31 +02:00
|
|
|
r.previous_lines.insert(1, r.current)
|
|
|
|
a := '\n'.ustring()
|
|
|
|
r.current = r.current.add(a)
|
|
|
|
r.cursor = r.current.len
|
|
|
|
if r.is_tty {
|
|
|
|
r.refresh_line()
|
|
|
|
println('')
|
|
|
|
}
|
|
|
|
return true
|
2019-08-31 19:09:02 +02:00
|
|
|
}
|
2019-08-31 20:24:33 +02:00
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) move_cursor_left() {
|
2020-08-27 11:20:31 +02:00
|
|
|
if r.cursor > 0 {
|
|
|
|
r.cursor--
|
|
|
|
r.refresh_line()
|
|
|
|
}
|
2019-08-31 20:24:33 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) move_cursor_right() {
|
2020-08-27 11:20:31 +02:00
|
|
|
if r.cursor < r.current.len {
|
|
|
|
r.cursor++
|
|
|
|
r.refresh_line()
|
|
|
|
}
|
2019-08-31 20:24:33 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) move_cursor_begining() {
|
2020-08-27 11:20:31 +02:00
|
|
|
r.cursor = 0
|
|
|
|
r.refresh_line()
|
2019-08-31 20:24:33 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) move_cursor_end() {
|
2020-08-27 11:20:31 +02:00
|
|
|
r.cursor = r.current.len
|
|
|
|
r.refresh_line()
|
2019-08-31 20:24:33 +02:00
|
|
|
}
|
|
|
|
|
2019-09-01 19:00:14 +02:00
|
|
|
// Check if the character is considered as a word-breaking character
|
2019-09-23 12:43:24 +02:00
|
|
|
fn (r Readline) is_break_character(c string) bool {
|
2020-08-27 11:20:31 +02:00
|
|
|
break_characters := ' \t\v\f\a\b\r\n`~!@#$%^&*()-=+[{]}\\|;:\'",<.>/?'
|
|
|
|
return break_characters.contains(c)
|
2019-08-31 20:24:33 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) move_cursor_word_left() {
|
2020-08-27 11:20:31 +02:00
|
|
|
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-- {
|
|
|
|
}
|
|
|
|
r.refresh_line()
|
|
|
|
}
|
2019-08-31 20:24:33 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) move_cursor_word_right() {
|
2020-08-27 11:20:31 +02:00
|
|
|
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++ {
|
|
|
|
}
|
|
|
|
r.refresh_line()
|
|
|
|
}
|
2019-08-31 20:24:33 +02:00
|
|
|
}
|
2019-09-02 13:14:40 +02:00
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) switch_overwrite() {
|
2020-08-27 11:20:31 +02:00
|
|
|
r.overwrite = !r.overwrite
|
2019-09-04 20:08:41 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) clear_screen() {
|
2020-10-21 14:24:45 +02:00
|
|
|
term.set_cursor_position({
|
|
|
|
x: 1
|
|
|
|
y: 1
|
|
|
|
})
|
2020-08-27 11:20:31 +02:00
|
|
|
term.erase_clear()
|
|
|
|
r.refresh_line()
|
2019-09-02 13:14:40 +02:00
|
|
|
}
|
2019-09-03 11:52:00 +02:00
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) history_previous() {
|
2020-08-27 11:20:31 +02:00
|
|
|
if r.search_index + 2 >= r.previous_lines.len {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.search_index == 0 {
|
|
|
|
r.previous_lines[0] = r.current
|
|
|
|
}
|
|
|
|
r.search_index++
|
|
|
|
r.current = r.previous_lines[r.search_index]
|
|
|
|
r.cursor = r.current.len
|
|
|
|
r.refresh_line()
|
2019-09-03 11:52:00 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) history_next() {
|
2020-08-27 11:20:31 +02:00
|
|
|
if r.search_index <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.search_index--
|
|
|
|
r.current = r.previous_lines[r.search_index]
|
|
|
|
r.cursor = r.current.len
|
|
|
|
r.refresh_line()
|
2019-09-03 11:52:00 +02:00
|
|
|
}
|
2019-09-05 13:55:28 +02:00
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
fn (mut r Readline) suspend() {
|
2020-09-18 15:34:00 +02:00
|
|
|
is_standalone := os.getenv('VCHILD') != 'true'
|
|
|
|
r.disable_raw_mode()
|
|
|
|
if !is_standalone {
|
|
|
|
// We have to SIGSTOP the parent v process
|
|
|
|
ppid := C.getppid()
|
|
|
|
C.kill(ppid, C.SIGSTOP)
|
|
|
|
}
|
2020-08-27 11:20:31 +02:00
|
|
|
C.raise(C.SIGSTOP)
|
2020-09-18 15:34:00 +02:00
|
|
|
r.enable_raw_mode()
|
2020-08-27 11:20:31 +02:00
|
|
|
r.refresh_line()
|
2020-09-18 15:34:00 +02:00
|
|
|
if r.is_tty {
|
|
|
|
r.refresh_line()
|
|
|
|
}
|
2019-09-05 13:55:28 +02:00
|
|
|
}
|