From bb1eca3564a2d4ae4fa2c5f0bed649bd5f8afb28 Mon Sep 17 00:00:00 2001 From: Henrixounez <30901439+Henrixounez@users.noreply.github.com> Date: Tue, 15 Oct 2019 08:04:22 +0200 Subject: [PATCH] readline: basic mac and windows version using os.getline --- vlib/readline/readline.v | 42 +++++++++++++++++++++++ vlib/readline/readline_lin.v | 35 +------------------ vlib/readline/readline_mac.v | 66 ++++++++++++++++++++++++++++++++++++ vlib/readline/readline_win.v | 66 ++++++++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+), 34 deletions(-) create mode 100644 vlib/readline/readline.v create mode 100644 vlib/readline/readline_mac.v create mode 100644 vlib/readline/readline_win.v diff --git a/vlib/readline/readline.v b/vlib/readline/readline.v new file mode 100644 index 0000000000..6e1ed2cf4d --- /dev/null +++ b/vlib/readline/readline.v @@ -0,0 +1,42 @@ +// 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. + +// Will serve as more advanced input method +// Based on the work of https://github.com/AmokHuginnsson/replxx + +module readline + +// Linux +// Used to change the terminal options +struct Termios { +mut: + c_iflag int + c_oflag int + c_cflag int + c_lflag int + c_cc [12]int //NCCS == 12. Cant use the defined value here +} + +// Linux +// Used to collect the screen information +struct Winsize { + ws_row u16 + ws_col u16 + ws_xpixel u16 + ws_ypixel u16 +} + +struct Readline { +mut: + is_raw bool + orig_termios Termios // Linux + current ustring // Line being edited + cursor int // Cursor position + overwrite bool + cursor_row_offset int + prompt string + previous_lines []ustring + search_index int + is_tty bool +} \ No newline at end of file diff --git a/vlib/readline/readline_lin.v b/vlib/readline/readline_lin.v index dbd7f92f0d..591211e23e 100644 --- a/vlib/readline/readline_lin.v +++ b/vlib/readline/readline_lin.v @@ -13,39 +13,6 @@ import term #include #include -// Used to change the terminal options -struct Termios { -mut: - c_iflag int - c_oflag int - c_cflag int - c_lflag int - c_cc [12]int //NCCS == 12. Cant use the defined value here -} - -// Used to collect the screen information -struct Winsize { - ws_row u16 - ws_col u16 - ws_xpixel u16 - ws_ypixel u16 -} - -struct Readline { -mut: - is_raw bool - orig_termios Termios - current ustring // Line being edited - cursor int // Cursor position - overwrite bool - cursor_row_offset int - prompt string - previous_lines []ustring - search_index int - is_tty bool -} - - // Defines actions to execute enum Action { eof @@ -104,7 +71,7 @@ fn (r mut Readline) enable_raw_mode2() { } // Reset back the terminal to its default value -pub fn (r mut Readline) disable_raw_mode() { +fn (r mut Readline) disable_raw_mode() { if r.is_raw { C.tcsetattr(0, C.TCSADRAIN, &r.orig_termios) r.is_raw = false diff --git a/vlib/readline/readline_mac.v b/vlib/readline/readline_mac.v new file mode 100644 index 0000000000..f177876132 --- /dev/null +++ b/vlib/readline/readline_mac.v @@ -0,0 +1,66 @@ +// 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. + +// Mac version +// Need to be implemented +// Will serve as more advanced input method +// Based on the work of https://github.com/AmokHuginnsson/replxx + +module readline + +import os + +// Only use standard os.get_line +// Need implementation for readline capabilities +pub fn (r mut Readline) read_line_utf8(prompt string) ?ustring { + r.current = ''.ustring() + r.cursor = 0 + r.prompt = prompt + r.search_index = 0 + if r.previous_lines.len <= 1 { + r.previous_lines << ''.ustring() + r.previous_lines << ''.ustring() + } + else { + r.previous_lines[0] = ''.ustring() + } + + print(r.prompt) + r.current = os.get_line().ustring() + + r.previous_lines[0] = ''.ustring() + r.search_index = 0 + if r.current.s == '' { + return error('empty line') + } + return r.current +} + +// 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 +} diff --git a/vlib/readline/readline_win.v b/vlib/readline/readline_win.v new file mode 100644 index 0000000000..9eda679d52 --- /dev/null +++ b/vlib/readline/readline_win.v @@ -0,0 +1,66 @@ +// 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. + +// Windows version +// Need to be implemented +// Will serve as more advanced input method +// Based on the work of https://github.com/AmokHuginnsson/replxx + +module readline + +import os + +// Only use standard os.get_line +// Need implementation for readline capabilities +pub fn (r mut Readline) read_line_utf8(prompt string) ?ustring { + r.current = ''.ustring() + r.cursor = 0 + r.prompt = prompt + r.search_index = 0 + if r.previous_lines.len <= 1 { + r.previous_lines << ''.ustring() + r.previous_lines << ''.ustring() + } + else { + r.previous_lines[0] = ''.ustring() + } + + print(r.prompt) + r.current = os.get_line().ustring() + + r.previous_lines[0] = ''.ustring() + r.search_index = 0 + if r.current.s == '' { + return error('empty line') + } + return r.current +} + +// 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 +}