From b8ca1098d8a57ff299a1ae151f6db6836fa74279 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20Vachon?= Date: Sat, 14 May 2016 12:21:07 -0400 Subject: [PATCH] lib: Add a tiny GNU readline wrapper MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Frédéric Vachon --- lib/readline.ini | 11 ++++++++ lib/readline.nit | 58 +++++++++++++++++++++++++++++++++++++++++++ tests/sav/test_readline.res | 9 +++++++ tests/test_readline.inputs | 4 +++ tests/test_readline.nit | 21 ++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 lib/readline.ini create mode 100644 lib/readline.nit create mode 100644 tests/sav/test_readline.res create mode 100644 tests/test_readline.inputs create mode 100644 tests/test_readline.nit diff --git a/lib/readline.ini b/lib/readline.ini new file mode 100644 index 0000000..c7e2ee9 --- /dev/null +++ b/lib/readline.ini @@ -0,0 +1,11 @@ +[package] +name=readline +tags=lib +maintainer=Frédéric Vachon +license=Apache-2.0 +[upstream] +browse=https://github.com/nitlang/nit/tree/master/lib/readline.nit +git=https://github.com/nitlang/nit.git +git.directory=lib/readline.nit +homepage=http://nitlanguage.org +issues=https://github.com/nitlang/nit/issues diff --git a/lib/readline.nit b/lib/readline.nit new file mode 100644 index 0000000..783e4e4 --- /dev/null +++ b/lib/readline.nit @@ -0,0 +1,58 @@ +# This file is part of NIT (http://www.nitlanguage.org). +# +# Copyright 2016 Frédéric Vachon +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# GNU readline library wrapper +module readline is ldflags "-lreadline" + +in "C" `{ + #include + #include +`} + +private fun native_readline(prompt: NativeString): NativeString `{ + return readline(prompt); +`} + +private fun native_add_history(data: NativeString) `{ + if (data == NULL) return; + add_history(data); +`} + +# Set emacs keybindings mode +fun set_vi_mode `{ rl_editing_mode = 0; `} + +# Set emacs keybindings mode +fun set_emacs_mode `{ rl_editing_mode = 1; `} + +# Use the GNU Library readline function +# Returns `null` if EOF is read +# If `with_history` is true, it will save all commands in the history except +# empty strings and white characters strings +fun readline(message: String, with_history: nullable Bool): nullable String do + var line = native_readline(message.to_cstring) + if line.address_is_null then return null + + var nit_str = line.to_s + + if with_history != null and with_history then + if nit_str.trim != "" then native_add_history(line) + end + + return nit_str +end + +# Adds the data String to the history no matter what it contains +fun add_history(data: String) do native_add_history data.to_cstring diff --git a/tests/sav/test_readline.res b/tests/sav/test_readline.res new file mode 100644 index 0000000..666312a --- /dev/null +++ b/tests/sav/test_readline.res @@ -0,0 +1,9 @@ +prompt>line 1 +line 1 +prompt>line 2 +line 2 +prompt>line 2bis +line 2bis +prompt>line 3  ine   3  +line 3 +prompt> \ No newline at end of file diff --git a/tests/test_readline.inputs b/tests/test_readline.inputs new file mode 100644 index 0000000..59f6fe7 --- /dev/null +++ b/tests/test_readline.inputs @@ -0,0 +1,4 @@ +line 1 +line 2 +line 2bis +line 3 ine3 diff --git a/tests/test_readline.nit b/tests/test_readline.nit new file mode 100644 index 0000000..c254b24 --- /dev/null +++ b/tests/test_readline.nit @@ -0,0 +1,21 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import readline + +loop + var line = readline("prompt>", true) + if line == null then break + print line +end -- 1.7.9.5