Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
lib:repl [2019/01/03 07:28]
sprowell User data section.
lib:repl [2019/01/03 07:30] (current)
sprowell
Line 94: Line 94:
 ===== Adding a New Command ===== ===== Adding a New Command =====
  
-We can add a new command to the REPL using the `add_commandmethod.  We need to provide the command name, the help text, and a callback for the command.  The callback has the same form as for the evaluation method.+We can add a new command to the REPL using the ''add_command'' method.  We need to provide the command name, the help text, and a callback for the command.  The callback has the same form as for the evaluation method.
  
-We will use our `echofunction for this, but we don't want to quit when the user enters `quit`, so we just modify the returned Boolean to always be `true`.  Again, we need to install this command after creating the REPL instance and before invoking `run`.  While we are at it, let's also install a "no-op" command that does nothing.  We could do this with a closure, as before: `|_,_| (None, true)`, but let's use a function pointer, instead.+We will use our ''echo'' function for this, but we don't want to quit when the user enters ''quit'', so we just modify the returned Boolean to always be ''true''.  Again, we need to install this command after creating the REPL instance and before invoking ''run''.  While we are at it, let's also install a "no-op" command that does nothing.  We could do this with a closure, as before: ''|_,_| (None, true)'', but let's use a function pointer, instead.
  
-We will also change the prompt, using `set_prompt`.+We will also change the prompt, using ''set_prompt''.
  
 <code Rust [enable_line_numbers="true"]> <code Rust [enable_line_numbers="true"]>
Line 160: Line 160:
 ===== User Data ===== ===== User Data =====
  
-Our functions will probably want to do something a little more sophisticated, so let's take a look at `ReplContext`. We get some important things packaged in this struct.+Our functions will probably want to do something a little more sophisticated, so let's take a look at ''ReplContext''. We get some important things packaged in this struct.
  
   * The line editor (provided by the [[https://crates.io/crates/rustyline|rustyline]] crate) being used by the REPL, which gives us access to line history as well as the ability to read lines ourselves and have those lines be part of the same history.   * The line editor (provided by the [[https://crates.io/crates/rustyline|rustyline]] crate) being used by the REPL, which gives us access to line history as well as the ability to read lines ourselves and have those lines be part of the same history.
-  * The terminal (provided by the [[https://crates.io/crates/console|console]] crate) that lets us provide consistent output, though this is not essential.+  * The terminal (provided by the [[https://crates.io/crates/console|console]] crate) that lets us provide consistent output, though this is not essential.
   * The current value of the "quiet" flag, that lets us decide whether we //should// provide output.  If we simply return some string, that string is always printed.   * The current value of the "quiet" flag, that lets us decide whether we //should// provide output.  If we simply return some string, that string is always printed.
   * Some user data, which can hold anything we want.   * Some user data, which can hold anything we want.