Merge: Intro and use `prompt`, an alternative to `readline`
authorJean Privat <jean@pryen.org>
Fri, 16 Feb 2018 16:45:44 +0000 (11:45 -0500)
committerJean Privat <jean@pryen.org>
Fri, 16 Feb 2018 16:45:44 +0000 (11:45 -0500)
The new package `prompt` offers two basic services to create a shell interface. It does not depend on external libraries and it is licensed under Apache 2.0.

The `prompt` API is optionally implemented by the `readline` package which can be used as a drop-in replacement.

This PR uses `prompt` in nitx to remove the dependency on readline. This dependency would prevent us from compiling Nit on platforms that do not offer the native library, and someone could argue that there are possible legal ramifications in which I would not want to ensnare the Nit project. See the past discussion on GNU readline: #2083

Anyone can still mixin readline with nitx using: `nitc nitx.nit -m readline`

This will also fix the Nit Docker image failing to build: https://hub.docker.com/r/nitlang/nit/builds/

Pull-Request: #2613

contrib/nitin/Makefile
contrib/nitin/nitin.nit
contrib/nitin/nitin_readline.nit [deleted file]
lib/prompt.ini [new file with mode: 0644]
lib/prompt.nit [new file with mode: 0644]
lib/readline.nit
src/nitx.nit

index c505bcd..940cee6 100644 (file)
@@ -1,3 +1,3 @@
 all:
        mkdir -p bin
-       nitc --semi-global nitin_readline.nit -o bin/nitin
+       nitc --semi-global nitin.nit -m readline -o bin/nitin
index 2497411..6651636 100644 (file)
@@ -19,6 +19,8 @@
 # The Nit interactive interpreter
 module nitin
 
+import prompt
+
 import nitc::interpreter
 import nitc::frontend
 import nitc::parser_util
@@ -56,21 +58,6 @@ redef class ToolContext
                return tree.n_base.as(not null)
        end
 
-       # Read an user-line with a given `prompt`
-       #
-       # Return `null` if end of file
-       fun readline(prompt: String): nullable String do
-               printn prompt
-               var res = stdin.read_line
-               if res == "" and stdin.eof then return null
-               return res
-       end
-
-       # Add `text` in the history for `readline`.
-       #
-       # With the default implementation, the history is dropped
-       fun readline_add_history(text: String) do end
-
        # The last line number read by `i_parse`
        var last_line = 0
 
@@ -85,7 +72,7 @@ redef class ToolContext
                                s = stdin.read_line
                                if s == "" and stdin.eof then s = null
                        else
-                               s = readline(prompt)
+                               s = sys.prompt(prompt)
                        end
                        if s == null then return null
                        if s == "" then
@@ -115,7 +102,7 @@ redef class ToolContext
                        end
 
                        last_line = n.location.file.line_starts.length - 1
-                       readline_add_history(text.chomp)
+                       prompt_add_history(text.chomp)
                        return n
                end
        end
diff --git a/contrib/nitin/nitin_readline.nit b/contrib/nitin/nitin_readline.nit
deleted file mode 100644 (file)
index d193059..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-# This file is part of NIT ( http://www.nitlanguage.org ).
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-# Add GNU readline capabilities to nitin
-module nitin_readline
-
-import nitin
-import readline
-
-redef class ToolContext
-       redef fun readline(prompt) do return sys.readline(prompt)
-       redef fun readline_add_history(text) do sys.add_history(text)
-end
diff --git a/lib/prompt.ini b/lib/prompt.ini
new file mode 100644 (file)
index 0000000..815bd3b
--- /dev/null
@@ -0,0 +1,11 @@
+[package]
+name=prompt
+tags=console,lib
+maintainer=Alexis Laferrière <alexis.laf@xymus.net>
+license=Apache-2.0
+[upstream]
+browse=https://github.com/nitlang/nit/tree/master/lib/prompt.nit
+git=https://github.com/nitlang/nit.git
+git.directory=lib/prompt.nit
+homepage=http://nitlanguage.org
+issues=https://github.com/nitlang/nit/issues
diff --git a/lib/prompt.nit b/lib/prompt.nit
new file mode 100644 (file)
index 0000000..56c8c2d
--- /dev/null
@@ -0,0 +1,34 @@
+# 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.
+
+# Basic services to display a prompt
+module prompt
+
+# Display `prompt` in the console and wait for a response
+#
+# Return the user response, or `null` if EOF.
+#
+# If `add_history`, the user response is added to the history.
+fun prompt(prompt: Text, add_history: nullable Bool): nullable String
+do
+       printn prompt
+       var res = stdin.read_line
+       if res == "" and stdin.eof then return null
+       return res
+end
+
+# Add `line` to the shell history
+#
+# The default implementation is a noop, but other packages can refine it.
+fun prompt_add_history(line: String) do end
index 00f920c..6532cb4 100644 (file)
@@ -17,6 +17,8 @@
 # GNU readline library wrapper
 module readline is ldflags "-lreadline"
 
+import prompt
+
 in "C" `{
        #include <readline/readline.h>
        #include <readline/history.h>
@@ -56,3 +58,7 @@ 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
+
+redef fun prompt(prompt, add_history) do return readline(prompt.to_s, add_history)
+
+redef fun prompt_add_history(line) do native_add_history(line.to_cstring)
index 061b44e..8442ff7 100644 (file)
@@ -25,7 +25,7 @@ module nitx
 
 import frontend
 import doc::term
-import readline
+import prompt
 
 redef class ToolContext
 
@@ -90,9 +90,12 @@ class Nitx
 
        # Prompts the user for a query.
        fun prompt do
-               var line = readline(">> ", true)
+               var line = sys.prompt(">> ", true)
                if line != null then
                        do_command(line)
+               else
+                       # EOF
+                       exit 0
                end
                prompt
        end