Sluift Interactive Console

Sluift, our Swiften-based XMPP script tool, comes with an interactive mode that lets you type your commands directly in a console. Until now, this console was actually the standard one that comes with the Lua distribution, a very bare bones “REPL” loop. However, we recently replaced this simple implementation with our own, which allowed us to do some usability enhancements for making it easier to execute commands, play around with Sluift, and help you writing Sluift scripts. In this post, we describe these new improvements in more detail.

If you want to try any of these new Sluift features yourself, just check out a development version from the Swift Git repository.

Tab Completion

The first improvement is that the console now supports tab completion. This not only makes it easy to quickly type long commands such as sluift.new_client, but it also helps to quickly discover which commands or fields are available on a given object:

Tab Completion

Print and store command results

A second improvement is that we print the results of each command invoked in the console. You therefore no longer have to remember to put = in front of a command to actually see the result:

Apart from printing each result, the console also stores each result of each command in the variables _1, _2, …, allowing you to reuse results easily in subsequent commands:

Integrated Help

The console now comes with integrated help functionality. If you want help for any object, just call help(), and pass the object or function you want help for. For example, getting help for a function:

Getting help for an object:

In case you don’t have an object, but want to see the methods available on a class of objects, you can pass a string to help:

All the help provided in the interactive console is now also available on-line.

Changing Context

inally, we made it easier to do multiple operations on an object, without each time having to specify the object on which to call the operation. You can now change the “context” of the console by calling withthe target object as an argument. Any global function or field requested will first be searched in the target object, after which it falls back to the normal behavior. Moreover, any function called and found in the target object will be passed self implicitly. For example, suppose you want to do multiple operations on a client:

Notice that the prompt of the console reflects the current context.

Resetting the context can be done by calling with():

The with function is also available for scripts to use (as sluift.with). You can pass a dynamic scope to only temporarily change the context. For example, a variant of the EchoBot that uses with would look like this:

Leave a Reply

Your email address will not be published. Required fields are marked *