A Cocoa interface element which implements the interface for a Read-Eval-Print Loop (REPL).
REPLView is a Cocoa View for macOS which presents a scrolling text window with an editable input line at the bottom. The user can type text into the input line; when they press Enter, the text is passed to a provided evaluator function, which executes, evaluates or otherwise processes it, potentially producing output and/or error messages, which are then appended to the scrolling text window.
REPLView has the following features:
- Colour-coded output, with distinct colours for output, errors, echoed user input and previous sessions' restored output
- User input echoing may be configured with customisable formatting or disabled altogether
- Multi-line input history, which can be navigated with the up/down arrow keys
- Output may be synchronous (returned by the evaluator function) and/or asynchronous (written to the REPL window at any time)
The REPLView
code is entirely contained in the source file REPLView.swift
. REPLView
is a subclass of NSView
, and can be inserted into an app's view hierarchy either in a Storyboard/NIB, or programmatically. A REPLView instance has the following instance variables which may be set:
evaluator
— a function (closure) which is used to evaluate the text the user submits; code using REPLView should provide this, setting REPLView'sevaluator
variable to it. This function is to take one argument: aString
containing the line submitted (without the trailing newline character); it returns an Optional value which, if present, is aREPLView.EvalResponse
type, representing a result. This may be either.output(String)
, representing a line of ordinary output, or.error(String)
, representing an error message. If it isnil
, the evaluator function does not return anything to display.backgroundColor
— aNSColor
value containing the colour of the REPL window backgroundoutputColor
— aNSColor
value containing the colour of ordinary output text displayed, as well as the user's inputerrorColor
— aNSColor
value containing the colour of error text displayedechoColor
— aNSColor
value containing the colour that the user's input is displayed in if it is echoed to the scrolling windowechoFormatter
— a function which, given a line the user just entered, returns an optional line to append to the scrolling window; the default implementation provided returns the input line prefixed with">>> "
. To disable echoing, replaceechoFormatter
with a function that returnsnil
, i.e.,{ _ in nil }
.maxHistoryLines
— the maximum number of lines of history to keep internally.
To print a line of (ordinary, non error) output to the REPLView's window, call its printOutputLn(:)
method, i.e.,
myRepl.printOutputLn("Hello world")
To print an error message, use the printErrorLn(:)
method in the same way. (Alternatively, you can use the println(response:)
method, passing a REPLView.EvalResponse
value, if that is more convenient.)
The provided REPLViewExample
project builds a basic example application, which reads integers on the REPL's input and prints their hexadecimal representations (or error messages if non-integer inputs are given). This demonstrates the basic operation of REPLView
. Note that in this example, the REPL's colour scheme is defined in the Storyboard.
REPLView is written in Swift 4. Due to its use of Swift's type system, it probably won't ever be compatible with Objective C, but if you're writing new code in Objective C, you should probably ask yourself why.
- Andrew Bulhak - GitHub/Technical blog
REPLView
is licensed under the MIT License.