readline: allow writing without tty + small fixes
parent
1d7b9a3b0b
commit
04ff9ede9f
|
@ -42,6 +42,7 @@ mut:
|
||||||
prompt string
|
prompt string
|
||||||
previous_lines []string
|
previous_lines []string
|
||||||
search_index int
|
search_index int
|
||||||
|
is_tty bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -69,7 +70,8 @@ enum Action {
|
||||||
// Toggle raw mode of the terminal by changing its attributes
|
// Toggle raw mode of the terminal by changing its attributes
|
||||||
pub fn (r mut Readline) enable_raw_mode() {
|
pub fn (r mut Readline) enable_raw_mode() {
|
||||||
if ( C.tcgetattr(0, &r.orig_termios) == -1 ) {
|
if ( C.tcgetattr(0, &r.orig_termios) == -1 ) {
|
||||||
panic('No tty')
|
r.is_tty = false
|
||||||
|
return
|
||||||
}
|
}
|
||||||
mut raw := r.orig_termios
|
mut raw := r.orig_termios
|
||||||
raw.c_iflag &= ~( C.BRKINT | C.ICRNL | C.INPCK | C.ISTRIP | C.IXON )
|
raw.c_iflag &= ~( C.BRKINT | C.ICRNL | C.INPCK | C.ISTRIP | C.IXON )
|
||||||
|
@ -79,12 +81,14 @@ pub fn (r mut Readline) enable_raw_mode() {
|
||||||
raw.c_cc[C.VTIME] = 0
|
raw.c_cc[C.VTIME] = 0
|
||||||
C.tcsetattr(0, C.TCSADRAIN, &raw)
|
C.tcsetattr(0, C.TCSADRAIN, &raw)
|
||||||
r.is_raw = true
|
r.is_raw = true
|
||||||
|
r.is_tty = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not catching the SIGUSER (CTRL+C) Signal
|
// Not catching the SIGUSER (CTRL+C) Signal
|
||||||
pub fn (r Readline) enable_raw_mode2() {
|
pub fn (r mut Readline) enable_raw_mode2() {
|
||||||
if ( C.tcgetattr(0, &r.orig_termios) == -1 ) {
|
if ( C.tcgetattr(0, &r.orig_termios) == -1 ) {
|
||||||
panic('No tty')
|
r.is_tty = false
|
||||||
|
return
|
||||||
}
|
}
|
||||||
mut raw := r.orig_termios
|
mut raw := r.orig_termios
|
||||||
raw.c_iflag &= ~( C.BRKINT | C.ICRNL | C.INPCK | C.ISTRIP | C.IXON )
|
raw.c_iflag &= ~( C.BRKINT | C.ICRNL | C.INPCK | C.ISTRIP | C.IXON )
|
||||||
|
@ -93,6 +97,8 @@ pub fn (r Readline) enable_raw_mode2() {
|
||||||
raw.c_cc[C.VMIN] = 1
|
raw.c_cc[C.VMIN] = 1
|
||||||
raw.c_cc[C.VTIME] = 0
|
raw.c_cc[C.VTIME] = 0
|
||||||
C.tcsetattr(0, C.TCSADRAIN, &raw)
|
C.tcsetattr(0, C.TCSADRAIN, &raw)
|
||||||
|
r.is_raw = true
|
||||||
|
r.is_tty = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset back the terminal to its default value
|
// Reset back the terminal to its default value
|
||||||
|
@ -224,8 +230,8 @@ fn (r mut Readline) execute(a Action, c byte) bool {
|
||||||
|
|
||||||
fn get_screen_columns() int {
|
fn get_screen_columns() int {
|
||||||
ws := winsize{}
|
ws := winsize{}
|
||||||
cols := if C.ioctl(1, C.TIOCGWINSZ, &ws) == -1 { 80 } else { ws.ws_col }
|
cols := if C.ioctl(1, C.TIOCGWINSZ, &ws) == -1 { 80 } else { int(ws.ws_col) }
|
||||||
return int(cols)
|
return cols
|
||||||
}
|
}
|
||||||
|
|
||||||
fn shift_cursor(xpos int, yoffset int) {
|
fn shift_cursor(xpos int, yoffset int) {
|
||||||
|
@ -299,8 +305,10 @@ fn (r mut Readline) insert_character(c byte) {
|
||||||
}
|
}
|
||||||
r.cursor++
|
r.cursor++
|
||||||
// Refresh the line to add the new character
|
// Refresh the line to add the new character
|
||||||
|
if r.is_tty {
|
||||||
r.refresh_line()
|
r.refresh_line()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Removes the character behind cursor.
|
// Removes the character behind cursor.
|
||||||
fn (r mut Readline) delete_character() {
|
fn (r mut Readline) delete_character() {
|
||||||
|
@ -325,7 +333,9 @@ fn (r mut Readline) suppr_character() {
|
||||||
fn (r mut Readline) commit_line() bool {
|
fn (r mut Readline) commit_line() bool {
|
||||||
r.previous_lines.insert(1, r.current)
|
r.previous_lines.insert(1, r.current)
|
||||||
r.current = r.current + '\n'
|
r.current = r.current + '\n'
|
||||||
|
if r.is_tty {
|
||||||
println('')
|
println('')
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue