Nasal Console

From FlightGear wiki
Jump to: navigation, search

FlightGear provides a built-in text editor for writing short pieces of Nasal code, the so called "Nasal Console" is available in FlightGear's menu (Debug/Nasal Console): Menubar2.jpg

Debug menu items.png

Selecting this item opens a Nasal Console dialog.

The Nasal console is a complete Nasal scripting shell, where you can type and execute Nasal code. Keep in mind though, that it is really only intended for quick hacks, there's currently no way to save or load Nasal modules in $FG_ROOT directly via the console. You can however dump your code to the startup terminal window and paste it to a text file this way.

The startup/terminal window is also the place where you'll see all the console output created by your scripts (print() statements, logging, warnings/errors etc).

The Nasal console could for example be used to test aircraft functions without having to assign key bindings.

The Nasal console dialog supports several tabs, of which each can hold separate Nasal code snippets, all of which are saved on exit and reloaded next time. This is useful for little tests, or for executing code for which writing a key binding is just too much work, such as "props.dump(props.globals)". It's easy to add more "tabs" (= radio buttons). If you want to add more tabs to hold more code samples, just add more <code> nodes to autosave.xml

The Nasal console dialog supports resizing, Resizing is at the moment done like dragging, but with Ctrl-key pressed (at the beginning). Dialogs expand to North-East, which is a bit unusual and might be changed later. The dialog margins are also sensitive for resizing without pressing the Ctrl-key.

The standard Nasal scripting console in FlightGear

In FlightGear 3.0, the built-in Nasal Console was updated to also add native copy/paste support, a feature that got implemented as part of the ongoing Canvas effort (see Howto:Clipboard access using Nasal for details). This feature makes it now possible to easily copy/paste snippets of code FlightGear, without having to use the "dump" feature.

The Nasal console in FG 3.0 with copy/paste buttons


Discussed Enhancements

Note  The following ideas all predate Interactive Nasal Console, so better consider extending Interactive Nasal Console instead. Also see Nasal Browser.

The Nasal console is implemented as a standard XML dialog with some embedded Nasal sections, it is to be found in $FG_ROOT/gui/dialogs/nasal-console.xml: https://sourceforge.net/p/flightgear/fgdata/ci/next/tree/gui/dialogs/nasal-console.xml

We talked about improving the existing Nasal documentation [1] [2], and it seems that users would prefer examples to be self-contained, so that they can follow tutorials by using the Nasal console and running code there, so that FlightGear could be used as an IDE - this would require a bunch of enhancements, such as:

  • allow loading/saving Nasal files (probably in some custom sub directory, as in $FG_HOME/Nasal)
  • copy/paste support Done Done
  • move the tabs to the top of the Nasal console Done Done
  • history: undo/redo support (just requires keyboard hooks, to maintain an undo/redo stack of changes, so that the canvas buffer can be updated accordingly)
  • show file names instead of numbers for loaded files
  • make sure that Nasal errors contain the right line number (taking the offset into account)
  • provide an option to display line numbers next to the edit field
  • show the namespace of each tab
  • possibly provide an option to terminate all running code for the namespace ?
  • basic syntax highlighting? (seems a bit awkward to implement using just osgText and the canvas in its current form, it might be easier to register a list of properties, with colors that are then highlighted/formatted by the c++ code internally, need to talk with TheTom)
  • it might make sense to add a wrapper, so that Nasal output to STDOUT/STDERR (printf) could be copied to a property, so that it would be possible to show all output directly in FG, i.e. in some log/output field? Done Done (by Zakalawe, see loglist widget)
  • provide some "snippets" facility for frequently used code snippets
  • provide a template wizard, so that loops, functions and hashes can be created and customized using a GUI wizard
  • quick navigation: pulldown menu to jump to specific functions/hashes
  • option to show a static callgraph by parsing code and tracking callers/callees
  • search facility: find string/symbol/hash/vector/func
  • provide a list of examples that can be loaded, to demonstrate how FG scripting works (hello world, functions, loops etc)
  • turn the "nasal-console" into "nasal-editor" or even "nasal-ide" dialog
  • generalize things a little, so that also other plain text files can be edited, such as PropertyList XML and GLSL programs in $FG_ROOT (note $FG_ROOT is often assumed to be read-only and shared among users!)
  • use code from Nasal-standalone to make a REPL tab (we could also extend the telnet/props interface to expose a REPL via network)
  • be able to select namespace via drop-down for both REPL and tabs


As of 07/2012 there's a new GUI system in the works using the Canvas 2D rendering backend, this will make it possible to implement all of the discussed changes directly in Nasal space, see Canvas Widgets for further information.