2019-08-30 21:57:54 +03:00
|
|
|
// Copyright (c) 2019 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
|
2019-10-01 06:28:06 +03:00
|
|
|
// Based on the work of https://github.com/AmokHuginnsson/replxx
|
2019-08-30 21:57:54 +03:00
|
|
|
|
|
|
|
module readline
|
|
|
|
|
2019-08-31 20:09:02 +03:00
|
|
|
import term
|
|
|
|
|
2019-08-30 21:57:54 +03:00
|
|
|
#include <termios.h>
|
2019-09-01 20:00:14 +03:00
|
|
|
#include <sys/ioctl.h>
|
2019-08-30 21:57:54 +03:00
|
|
|
|
2019-08-31 20:09:02 +03:00
|
|
|
// Defines actions to execute
|
|
|
|
enum Action {
|
|
|
|
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
|
2019-09-02 14:14:40 +03:00
|
|
|
clear_screen
|
2019-09-05 14:55:28 +03:00
|
|
|
suspend
|
2019-08-30 21:57:54 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 19:00:56 +03:00
|
|
|
fn C.tcgetattr() int
|
|
|
|
fn C.tcsetattr() int
|
|
|
|
fn C.ioctl() int
|
|
|
|
fn C.raise()
|
|
|
|
|
2019-08-30 21:57:54 +03:00
|
|
|
// Toggle raw mode of the terminal by changing its attributes
|
2019-11-04 05:05:12 +03:00
|
|
|
// Catches SIGUSER (CTRL+C) Signal to reset tty
|
2019-10-06 16:28:41 +03:00
|
|
|
fn (r mut Readline) enable_raw_mode() {
|
2019-11-22 19:00:56 +03:00
|
|
|
if C.tcgetattr(0, &r.orig_termios) == -1 {
|
2019-09-15 04:33:31 +03:00
|
|
|
r.is_tty = false
|
2019-10-06 16:28:41 +03:00
|
|
|
r.is_raw = false
|
2019-09-15 04:33:31 +03:00
|
|
|
return
|
2019-08-30 21:57:54 +03:00
|
|
|
}
|
|
|
|
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
|
2019-09-15 04:33:31 +03:00
|
|
|
r.is_tty = true
|
2019-08-30 21:57:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not catching the SIGUSER (CTRL+C) Signal
|
2019-11-04 05:05:12 +03:00
|
|
|
fn (r mut Readline) enable_raw_mode_nosig() {
|
2019-08-30 21:57:54 +03:00
|
|
|
if ( C.tcgetattr(0, &r.orig_termios) == -1 ) {
|
2019-09-15 04:33:31 +03:00
|
|
|
r.is_tty = false
|
2019-10-06 16:28:41 +03:00
|
|
|
r.is_raw = false
|
2019-09-15 04:33:31 +03:00
|
|
|
return
|
2019-08-30 21:57:54 +03:00
|
|
|
}
|
|
|
|
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)
|
2019-09-15 04:33:31 +03:00
|
|
|
r.is_raw = true
|
|
|
|
r.is_tty = true
|
2019-08-30 21:57:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset back the terminal to its default value
|
2019-10-15 09:04:22 +03:00
|
|
|
fn (r mut Readline) disable_raw_mode() {
|
2019-08-30 21:57:54 +03:00
|
|
|
if r.is_raw {
|
|
|
|
C.tcsetattr(0, C.TCSADRAIN, &r.orig_termios)
|
2019-10-06 16:28:41 +03:00
|
|
|
r.is_raw = false
|
2019-08-30 21:57:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read single char
|
2019-09-23 13:43:24 +03:00
|
|
|
fn (r Readline) read_char() int {
|
|
|
|
return utf8_getchar()
|
2019-08-30 21:57:54 +03:00
|
|
|
}
|
|
|
|
|
2019-08-31 20:09:02 +03:00
|
|
|
// Main function of the readline module
|
|
|
|
// Will loop and ingest characters until EOF or Enter
|
2019-10-06 16:28:41 +03:00
|
|
|
// Returns the completed line as utf8 ustring
|
|
|
|
// Will return an error if line is empty
|
|
|
|
pub fn (r mut Readline) read_line_utf8(prompt string) ?ustring {
|
2019-09-23 13:43:24 +03:00
|
|
|
r.current = ''.ustring()
|
2019-08-31 20:09:02 +03:00
|
|
|
r.cursor = 0
|
2019-09-02 16:29:40 +03:00
|
|
|
r.prompt = prompt
|
2019-09-03 12:52:00 +03:00
|
|
|
r.search_index = 0
|
2019-11-10 19:33:21 +03:00
|
|
|
r.prompt_offset = get_prompt_offset(prompt)
|
2019-09-03 12:52:00 +03:00
|
|
|
if r.previous_lines.len <= 1 {
|
2019-09-23 13:43:24 +03:00
|
|
|
r.previous_lines << ''.ustring()
|
|
|
|
r.previous_lines << ''.ustring()
|
2019-09-03 12:52:00 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-09-23 13:43:24 +03:00
|
|
|
r.previous_lines[0] = ''.ustring()
|
2019-09-03 12:52:00 +03:00
|
|
|
}
|
2019-10-06 16:28:41 +03:00
|
|
|
if !r.is_raw {
|
2019-11-04 05:05:12 +03:00
|
|
|
r.enable_raw_mode()
|
2019-10-06 16:28:41 +03:00
|
|
|
}
|
2019-08-30 21:57:54 +03:00
|
|
|
|
2019-09-02 16:29:40 +03:00
|
|
|
print(r.prompt)
|
2019-08-30 21:57:54 +03:00
|
|
|
for {
|
|
|
|
c := r.read_char()
|
2019-08-31 20:09:02 +03:00
|
|
|
a := r.analyse(c)
|
|
|
|
if r.execute(a, c) {
|
2019-08-30 21:57:54 +03:00
|
|
|
break
|
|
|
|
}
|
2019-08-31 20:09:02 +03:00
|
|
|
}
|
2019-10-06 16:28:41 +03:00
|
|
|
|
2019-09-23 13:43:24 +03:00
|
|
|
r.previous_lines[0] = ''.ustring()
|
2019-09-03 12:52:00 +03:00
|
|
|
r.search_index = 0
|
2019-10-06 16:28:41 +03:00
|
|
|
r.disable_raw_mode()
|
|
|
|
if r.current.s == '' {
|
|
|
|
return error('empty line')
|
|
|
|
}
|
2019-08-31 20:09:02 +03:00
|
|
|
return r.current
|
|
|
|
}
|
|
|
|
|
2019-10-06 16:28:41 +03:00
|
|
|
// Returns the string from the utf8 ustring
|
|
|
|
pub fn (r mut Readline) read_line(prompt string) ?string {
|
|
|
|
s := r.read_line_utf8(prompt) or {
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
return s.s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Standalone function without persistent functionnalities (eg: history)
|
|
|
|
// Returns utf8 based ustring
|
|
|
|
pub fn read_line_utf8(prompt string) ?ustring {
|
|
|
|
mut r := Readline{}
|
|
|
|
s := r.read_line_utf8(prompt) or {
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Standalone function without persistent functionnalities (eg: history)
|
|
|
|
// Return string from utf8 ustring
|
|
|
|
pub fn read_line(prompt string) ?string {
|
|
|
|
mut r := Readline{}
|
|
|
|
s := r.read_line(prompt) or {
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
return s
|
2019-09-23 13:43:24 +03:00
|
|
|
}
|
|
|
|
|
2019-11-10 19:33:21 +03:00
|
|
|
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++ {}
|
|
|
|
} else {
|
|
|
|
len = len + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prompt.len - len
|
|
|
|
}
|
|
|
|
|
2019-10-16 12:46:24 +03:00
|
|
|
fn (r Readline) analyse(c int) Action {
|
2019-10-27 12:38:36 +03:00
|
|
|
match c {
|
2019-10-31 14:06:48 +03:00
|
|
|
`\0` { return .eof }
|
|
|
|
0x3 { return .eof } // End of Text
|
|
|
|
0x4 { return .eof } // End of Transmission
|
|
|
|
255 { return .eof }
|
|
|
|
`\n` { return .commit_line }
|
|
|
|
`\r` { return .commit_line }
|
|
|
|
`\f` { return .clear_screen } // CTRL + L
|
|
|
|
`\b` { return .delete_left } // Backspace
|
|
|
|
127 { return .delete_left } // DEL
|
2019-10-27 12:38:36 +03:00
|
|
|
27 { return r.analyse_control() } // ESC
|
2019-10-31 14:06:48 +03:00
|
|
|
1 { return .move_cursor_begining } // ^A
|
|
|
|
5 { return .move_cursor_end } // ^E
|
|
|
|
26 { return .suspend } // CTRL + Z, SUB
|
2019-10-31 19:01:04 +03:00
|
|
|
else { return if c >= ` ` { Action.insert_character } else { Action.nothing } }
|
2019-08-31 20:09:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (r Readline) analyse_control() Action {
|
|
|
|
c := r.read_char()
|
2019-10-27 12:38:36 +03:00
|
|
|
match c {
|
|
|
|
`[` {
|
2019-08-31 20:09:02 +03:00
|
|
|
sequence := r.read_char()
|
2019-10-27 12:38:36 +03:00
|
|
|
match sequence {
|
2019-10-31 19:01:04 +03:00
|
|
|
`C` { return .move_cursor_right }
|
|
|
|
`D` { return .move_cursor_left }
|
|
|
|
`B` { return .history_next }
|
|
|
|
`A` { return .history_previous }
|
2019-10-27 12:38:36 +03:00
|
|
|
`1` { return r.analyse_extended_control() }
|
|
|
|
`2` { return r.analyse_extended_control_no_eat(sequence) }
|
|
|
|
`3` { return r.analyse_extended_control_no_eat(sequence) }
|
2019-08-31 21:24:33 +03:00
|
|
|
}
|
2019-10-27 12:38:36 +03:00
|
|
|
}
|
2019-08-31 21:24:33 +03:00
|
|
|
}
|
2019-10-31 19:01:04 +03:00
|
|
|
return .nothing
|
2019-08-31 21:24:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (r Readline) analyse_extended_control() Action {
|
|
|
|
r.read_char() // Removes ;
|
|
|
|
c := r.read_char()
|
2019-10-27 12:38:36 +03:00
|
|
|
match c {
|
|
|
|
`5` {
|
2019-08-31 21:24:33 +03:00
|
|
|
direction := r.read_char()
|
2019-10-27 12:38:36 +03:00
|
|
|
match direction {
|
2019-10-31 19:01:04 +03:00
|
|
|
`C` { return .move_cursor_word_right }
|
|
|
|
`D` { return .move_cursor_word_left }
|
2019-08-30 21:57:54 +03:00
|
|
|
}
|
2019-10-27 12:38:36 +03:00
|
|
|
}
|
2019-08-30 21:57:54 +03:00
|
|
|
}
|
2019-10-31 19:01:04 +03:00
|
|
|
return .nothing
|
2019-08-31 20:09:02 +03:00
|
|
|
}
|
|
|
|
|
2019-09-04 21:08:41 +03:00
|
|
|
fn (r Readline) analyse_extended_control_no_eat(last_c byte) Action {
|
2019-09-02 13:14:46 +03:00
|
|
|
c := r.read_char()
|
2019-10-27 12:38:36 +03:00
|
|
|
match c {
|
|
|
|
`~` {
|
|
|
|
match last_c {
|
2019-10-31 19:01:04 +03:00
|
|
|
`3` { return .delete_right } // Suppr key
|
|
|
|
`2` { return .overwrite }
|
2019-09-04 21:08:41 +03:00
|
|
|
}
|
2019-10-27 12:38:36 +03:00
|
|
|
}
|
2019-09-02 13:14:46 +03:00
|
|
|
}
|
2019-10-31 19:01:04 +03:00
|
|
|
return .nothing
|
2019-09-02 13:14:46 +03:00
|
|
|
}
|
|
|
|
|
2019-09-23 13:43:24 +03:00
|
|
|
fn (r mut Readline) execute(a Action, c int) bool {
|
2019-10-27 12:38:36 +03:00
|
|
|
match a {
|
2019-10-31 19:01:04 +03:00
|
|
|
.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() }
|
2019-08-31 20:09:02 +03:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-09-01 20:00:14 +03:00
|
|
|
fn get_screen_columns() int {
|
2019-10-01 06:28:06 +03:00
|
|
|
ws := Winsize{}
|
2019-09-15 04:33:31 +03:00
|
|
|
cols := if C.ioctl(1, C.TIOCGWINSZ, &ws) == -1 { 80 } else { int(ws.ws_col) }
|
|
|
|
return cols
|
2019-09-01 20:00:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn shift_cursor(xpos int, yoffset int) {
|
|
|
|
if yoffset != 0 {
|
|
|
|
if yoffset > 0 {
|
|
|
|
term.cursor_down(yoffset)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
term.cursor_up(- yoffset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Absolute X position
|
|
|
|
print('\x1b[${xpos + 1}G')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn calculate_screen_position(x_in int, y_in int, screen_columns int, char_count int, out mut []int) {
|
|
|
|
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]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 20:09:02 +03:00
|
|
|
// Will redraw the line
|
2019-09-01 20:00:14 +03:00
|
|
|
fn (r mut Readline) refresh_line() {
|
|
|
|
mut end_of_input := [0, 0]
|
2019-09-02 16:29:40 +03:00
|
|
|
calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.current.len, mut end_of_input)
|
2019-09-23 13:43:24 +03:00
|
|
|
end_of_input[1] += r.current.count('\n'.ustring())
|
2019-09-01 20:00:14 +03:00
|
|
|
mut cursor_pos := [0, 0]
|
2019-09-02 16:29:40 +03:00
|
|
|
calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.cursor, mut cursor_pos)
|
2019-09-01 20:00:14 +03:00
|
|
|
|
|
|
|
shift_cursor(0, -r.cursor_row_offset)
|
2019-08-31 20:09:02 +03:00
|
|
|
term.erase_toend()
|
2019-09-02 16:29:40 +03:00
|
|
|
print(r.prompt)
|
2019-08-31 20:09:02 +03:00
|
|
|
print(r.current)
|
2019-09-01 20:00:14 +03:00
|
|
|
if end_of_input[0] == 0 && end_of_input[1] > 0 {
|
|
|
|
print('\n')
|
|
|
|
}
|
2019-11-10 19:33:21 +03:00
|
|
|
shift_cursor(cursor_pos[0] - r.prompt_offset, - (end_of_input[1] - cursor_pos[1]))
|
2019-09-01 20:00:14 +03:00
|
|
|
r.cursor_row_offset = cursor_pos[1]
|
2019-08-31 20:09:02 +03:00
|
|
|
}
|
|
|
|
|
2019-09-04 21:08:41 +03:00
|
|
|
// End the line without a newline
|
|
|
|
fn (r mut Readline) eof() bool {
|
|
|
|
r.previous_lines.insert(1, r.current)
|
2019-09-23 13:43:24 +03:00
|
|
|
r.cursor = r.current.len
|
2019-10-16 12:46:24 +03:00
|
|
|
if r.is_tty {
|
|
|
|
r.refresh_line()
|
|
|
|
}
|
2019-09-04 21:08:41 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-09-23 13:43:24 +03:00
|
|
|
fn (r mut Readline) insert_character(c int) {
|
2019-09-04 21:08:41 +03:00
|
|
|
if !r.overwrite || r.cursor == r.current.len {
|
2019-09-23 13:43:24 +03:00
|
|
|
r.current = r.current.left(r.cursor).ustring() + utf32_to_str(u32(c)).ustring() + r.current.right(r.cursor).ustring()
|
2019-08-31 20:09:02 +03:00
|
|
|
} else {
|
2019-09-23 13:43:24 +03:00
|
|
|
r.current = r.current.left(r.cursor).ustring() + utf32_to_str(u32(c)).ustring() + r.current.right(r.cursor + 1).ustring()
|
2019-08-31 20:09:02 +03:00
|
|
|
}
|
|
|
|
r.cursor++
|
2019-09-02 16:29:40 +03:00
|
|
|
// Refresh the line to add the new character
|
2019-09-15 04:33:31 +03:00
|
|
|
if r.is_tty {
|
|
|
|
r.refresh_line()
|
|
|
|
}
|
2019-08-31 20:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes the character behind cursor.
|
|
|
|
fn (r mut Readline) delete_character() {
|
|
|
|
if r.cursor <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.cursor--
|
2019-09-23 13:43:24 +03:00
|
|
|
r.current = r.current.left(r.cursor).ustring() + r.current.right(r.cursor + 1).ustring()
|
2019-09-01 20:00:14 +03:00
|
|
|
r.refresh_line()
|
2019-08-31 20:09:02 +03:00
|
|
|
}
|
|
|
|
|
2019-09-02 13:14:46 +03:00
|
|
|
// Removes the character in front of cursor.
|
|
|
|
fn (r mut Readline) suppr_character() {
|
|
|
|
if r.cursor > r.current.len {
|
|
|
|
return
|
|
|
|
}
|
2019-09-23 13:43:24 +03:00
|
|
|
r.current = r.current.left(r.cursor).ustring() + r.current.right(r.cursor + 1).ustring()
|
2019-09-02 13:14:46 +03:00
|
|
|
r.refresh_line()
|
|
|
|
}
|
|
|
|
|
2019-08-31 20:09:02 +03:00
|
|
|
// Add a line break then stops the main loop
|
|
|
|
fn (r mut Readline) commit_line() bool {
|
2019-09-03 12:52:00 +03:00
|
|
|
r.previous_lines.insert(1, r.current)
|
2019-09-23 13:43:24 +03:00
|
|
|
a := '\n'.ustring()
|
|
|
|
r.current = r.current + a
|
|
|
|
r.cursor = r.current.len
|
2019-09-15 04:33:31 +03:00
|
|
|
if r.is_tty {
|
2019-10-16 12:46:24 +03:00
|
|
|
r.refresh_line()
|
2019-09-15 04:33:31 +03:00
|
|
|
println('')
|
|
|
|
}
|
2019-08-31 20:09:02 +03:00
|
|
|
return true
|
|
|
|
}
|
2019-08-31 21:24:33 +03:00
|
|
|
|
|
|
|
fn (r mut Readline) move_cursor_left() {
|
|
|
|
if r.cursor > 0 {
|
|
|
|
r.cursor--
|
2019-09-01 20:00:14 +03:00
|
|
|
r.refresh_line()
|
2019-08-31 21:24:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (r mut Readline) move_cursor_right() {
|
|
|
|
if r.cursor < r.current.len {
|
|
|
|
r.cursor++
|
2019-09-01 20:00:14 +03:00
|
|
|
r.refresh_line()
|
2019-08-31 21:24:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (r mut Readline) move_cursor_begining() {
|
|
|
|
r.cursor = 0
|
2019-09-01 20:00:14 +03:00
|
|
|
r.refresh_line()
|
2019-08-31 21:24:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (r mut Readline) move_cursor_end() {
|
|
|
|
r.cursor = r.current.len
|
2019-09-01 20:00:14 +03:00
|
|
|
r.refresh_line()
|
2019-08-31 21:24:33 +03:00
|
|
|
}
|
|
|
|
|
2019-09-01 20:00:14 +03:00
|
|
|
// Check if the character is considered as a word-breaking character
|
2019-09-23 13:43:24 +03:00
|
|
|
fn (r Readline) is_break_character(c string) bool {
|
2019-08-31 21:24:33 +03:00
|
|
|
break_characters := ' \t\v\f\a\b\r\n`~!@#$%^&*()-=+[{]}\\|;:\'",<.>/?'
|
2019-09-23 13:43:24 +03:00
|
|
|
return break_characters.contains(c)
|
2019-08-31 21:24:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (r mut Readline) move_cursor_word_left() {
|
|
|
|
if r.cursor > 0 {
|
2019-09-23 13:43:24 +03:00
|
|
|
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-- {}
|
2019-09-01 20:00:14 +03:00
|
|
|
r.refresh_line()
|
2019-08-31 21:24:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (r mut Readline) move_cursor_word_right() {
|
|
|
|
if r.cursor < r.current.len {
|
2019-09-23 13:43:24 +03:00
|
|
|
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++ {}
|
2019-09-01 20:00:14 +03:00
|
|
|
r.refresh_line()
|
2019-08-31 21:24:33 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-02 14:14:40 +03:00
|
|
|
|
2019-09-04 21:08:41 +03:00
|
|
|
fn (r mut Readline) switch_overwrite() {
|
|
|
|
r.overwrite = !r.overwrite
|
|
|
|
}
|
|
|
|
|
2019-09-02 14:14:40 +03:00
|
|
|
fn (r mut Readline) clear_screen() {
|
|
|
|
term.set_cursor_position(1, 1)
|
|
|
|
term.erase_clear()
|
|
|
|
r.refresh_line()
|
|
|
|
}
|
2019-09-03 12:52:00 +03:00
|
|
|
|
|
|
|
fn (r mut Readline) history_previous() {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (r mut Readline) history_next() {
|
|
|
|
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-05 14:55:28 +03:00
|
|
|
|
|
|
|
fn (r mut Readline) suspend() {
|
|
|
|
C.raise(C.SIGSTOP)
|
|
|
|
r.refresh_line()
|
|
|
|
}
|