From 04ff9ede9fb04f9ed51ee3d9fd4e1e5e72705919 Mon Sep 17 00:00:00 2001 From: Henrixounez <30901439+Henrixounez@users.noreply.github.com> Date: Sun, 15 Sep 2019 03:33:31 +0200 Subject: [PATCH] readline: allow writing without tty + small fixes --- vlib/readline/readline_lin.v | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/vlib/readline/readline_lin.v b/vlib/readline/readline_lin.v index 6bfb320484..bb5214cd9f 100644 --- a/vlib/readline/readline_lin.v +++ b/vlib/readline/readline_lin.v @@ -42,6 +42,7 @@ mut: prompt string previous_lines []string search_index int + is_tty bool } @@ -69,7 +70,8 @@ enum Action { // Toggle raw mode of the terminal by changing its attributes pub fn (r mut Readline) enable_raw_mode() { if ( C.tcgetattr(0, &r.orig_termios) == -1 ) { - panic('No tty') + r.is_tty = false + return } mut raw := r.orig_termios 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 C.tcsetattr(0, C.TCSADRAIN, &raw) r.is_raw = true + r.is_tty = true } // 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 ) { - panic('No tty') + r.is_tty = false + return } mut raw := r.orig_termios 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.VTIME] = 0 C.tcsetattr(0, C.TCSADRAIN, &raw) + r.is_raw = true + r.is_tty = true } // 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 { ws := winsize{} - cols := if C.ioctl(1, C.TIOCGWINSZ, &ws) == -1 { 80 } else { ws.ws_col } - return int(cols) + cols := if C.ioctl(1, C.TIOCGWINSZ, &ws) == -1 { 80 } else { int(ws.ws_col) } + return cols } fn shift_cursor(xpos int, yoffset int) { @@ -299,7 +305,9 @@ fn (r mut Readline) insert_character(c byte) { } r.cursor++ // Refresh the line to add the new character - r.refresh_line() + if r.is_tty { + r.refresh_line() + } } // Removes the character behind cursor. @@ -325,7 +333,9 @@ fn (r mut Readline) suppr_character() { fn (r mut Readline) commit_line() bool { r.previous_lines.insert(1, r.current) r.current = r.current + '\n' - println('') + if r.is_tty { + println('') + } return true }