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
all:
mkdir -p bin
- nitc --semi-global nitin_readline.nit -o bin/nitin
+ nitc --semi-global nitin.nit -m readline -o bin/nitin
# The Nit interactive interpreter
module nitin
+import prompt
+
import nitc::interpreter
import nitc::frontend
import nitc::parser_util
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
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
end
last_line = n.location.file.line_starts.length - 1
- readline_add_history(text.chomp)
+ prompt_add_history(text.chomp)
return n
end
end
+++ /dev/null
-# 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
--- /dev/null
+[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
--- /dev/null
+# 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
# GNU readline library wrapper
module readline is ldflags "-lreadline"
+import prompt
+
in "C" `{
#include <readline/readline.h>
#include <readline/history.h>
# 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)
import frontend
import doc::term
-import readline
+import prompt
redef class ToolContext
# 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