From 9d1365ec67262c545fcc87c758d0af70f5416df2 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 24 Dec 2020 12:39:11 +0200 Subject: [PATCH] readline: put duplicated code in readline_default.c.v --- vlib/readline/readline.js.v | 60 ----------------- vlib/readline/readline_darwin.c.v | 65 ------------------- ...ine_dragonfly.c.v => readline_default.c.v} | 0 vlib/readline/readline_freebsd.c.v | 65 ------------------- vlib/readline/readline_netbsd.c.v | 65 ------------------- vlib/readline/readline_openbsd.c.v | 65 ------------------- vlib/readline/readline_solaris.c.v | 65 ------------------- vlib/readline/readline_test.v | 20 ++++++ vlib/readline/readline_windows.c.v | 60 ----------------- 9 files changed, 20 insertions(+), 445 deletions(-) delete mode 100644 vlib/readline/readline.js.v delete mode 100644 vlib/readline/readline_darwin.c.v rename vlib/readline/{readline_dragonfly.c.v => readline_default.c.v} (100%) delete mode 100644 vlib/readline/readline_freebsd.c.v delete mode 100644 vlib/readline/readline_netbsd.c.v delete mode 100644 vlib/readline/readline_openbsd.c.v delete mode 100644 vlib/readline/readline_solaris.c.v create mode 100644 vlib/readline/readline_test.v delete mode 100644 vlib/readline/readline_windows.c.v diff --git a/vlib/readline/readline.js.v b/vlib/readline/readline.js.v deleted file mode 100644 index f8324bee37..0000000000 --- a/vlib/readline/readline.js.v +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. -// Use of this source code is governed by an MIT license -// that can be found in the LICENSE file. - -// JS 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 (mut r 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_raw_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 (mut r Readline) read_line(prompt string) ?string { - s := r.read_line_utf8(prompt)? - 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)? - 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)? - return s -} diff --git a/vlib/readline/readline_darwin.c.v b/vlib/readline/readline_darwin.c.v deleted file mode 100644 index 71b67b3791..0000000000 --- a/vlib/readline/readline_darwin.c.v +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2019-2020 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 - -#include - -// Only use standard os.get_line -// Need implementation for readline capabilities -pub fn (mut r 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) - line := os.get_raw_line() - - if line.len >= 0 { - r.current = 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 (mut r Readline) read_line(prompt string) ?string { - s := r.read_line_utf8(prompt)? - 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)? - 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)? - return s -} diff --git a/vlib/readline/readline_dragonfly.c.v b/vlib/readline/readline_default.c.v similarity index 100% rename from vlib/readline/readline_dragonfly.c.v rename to vlib/readline/readline_default.c.v diff --git a/vlib/readline/readline_freebsd.c.v b/vlib/readline/readline_freebsd.c.v deleted file mode 100644 index 71b67b3791..0000000000 --- a/vlib/readline/readline_freebsd.c.v +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2019-2020 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 - -#include - -// Only use standard os.get_line -// Need implementation for readline capabilities -pub fn (mut r 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) - line := os.get_raw_line() - - if line.len >= 0 { - r.current = 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 (mut r Readline) read_line(prompt string) ?string { - s := r.read_line_utf8(prompt)? - 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)? - 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)? - return s -} diff --git a/vlib/readline/readline_netbsd.c.v b/vlib/readline/readline_netbsd.c.v deleted file mode 100644 index 71b67b3791..0000000000 --- a/vlib/readline/readline_netbsd.c.v +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2019-2020 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 - -#include - -// Only use standard os.get_line -// Need implementation for readline capabilities -pub fn (mut r 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) - line := os.get_raw_line() - - if line.len >= 0 { - r.current = 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 (mut r Readline) read_line(prompt string) ?string { - s := r.read_line_utf8(prompt)? - 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)? - 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)? - return s -} diff --git a/vlib/readline/readline_openbsd.c.v b/vlib/readline/readline_openbsd.c.v deleted file mode 100644 index 71b67b3791..0000000000 --- a/vlib/readline/readline_openbsd.c.v +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2019-2020 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 - -#include - -// Only use standard os.get_line -// Need implementation for readline capabilities -pub fn (mut r 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) - line := os.get_raw_line() - - if line.len >= 0 { - r.current = 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 (mut r Readline) read_line(prompt string) ?string { - s := r.read_line_utf8(prompt)? - 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)? - 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)? - return s -} diff --git a/vlib/readline/readline_solaris.c.v b/vlib/readline/readline_solaris.c.v deleted file mode 100644 index 71b67b3791..0000000000 --- a/vlib/readline/readline_solaris.c.v +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2019-2020 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 - -#include - -// Only use standard os.get_line -// Need implementation for readline capabilities -pub fn (mut r 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) - line := os.get_raw_line() - - if line.len >= 0 { - r.current = 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 (mut r Readline) read_line(prompt string) ?string { - s := r.read_line_utf8(prompt)? - 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)? - 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)? - return s -} diff --git a/vlib/readline/readline_test.v b/vlib/readline/readline_test.v new file mode 100644 index 0000000000..ab5154f38d --- /dev/null +++ b/vlib/readline/readline_test.v @@ -0,0 +1,20 @@ +import readline { Readline } + +fn no_lines(s string) string { + return s.replace('\n', ' ') +} + +fn test_struct_readline() { + // mut rl := Readline{} + // eprintln('rl: $rl') + // line := rl.read_line('Please, enter your name: ') or { panic(err) } + // eprintln('line: $line') + mut methods := []string{} + $for method in Readline.methods { + // eprintln(' method: $method.name | ' + no_lines('$method')) + methods << method.name + } + // eprintln('methods: $methods') + assert 'read_line_utf8' in methods + assert 'read_line' in methods +} diff --git a/vlib/readline/readline_windows.c.v b/vlib/readline/readline_windows.c.v deleted file mode 100644 index 7540a4d367..0000000000 --- a/vlib/readline/readline_windows.c.v +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) 2019-2020 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 (mut r 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_raw_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 (mut r Readline) read_line(prompt string) ?string { - s := r.read_line_utf8(prompt)? - 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)? - 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)? - return s -}