#include <nglConsole.h>
Inheritance diagram for nglConsole:

The console represents a simple terminal connnected to the current application, allowing text ouput and user input. An application can have only one console at once, or no console at all. To ensure there is only one instance at once, the reference to the console object is held in the application instance. It means that instantiating a new console object will automatically a) delete any previous instance and b) attach the new console to the application. Exemple :
new nglConsole(); nglConsole& con = App->GetConsole(); con.UseHistory(); con.UseCompletion(); con.Show();
You can fetch the reference to the current console object with App->GetConsole(). NGL does not automatically instantiate a console object at application startup. However, if the user did not provided a console and App->GetConsole() is called, the default console (of type nglConsole) is immediatly built and used. It means that the example above still works the same if the first line is removed.
The default console is implemented in different ways, depending on the platform :
class MyConsole : public nglConsole { public: MyConsole() : nglConsole (true) // Show on creation { UseHistory(); UseCompletion(); } virtual void OnInput (const nglString& rLine) { if (!rLine.Compare("exit")) Quit (0); else NGL_OUT ("sorry, unknown command\n"); } }; MyApplication::OnInit() { new MyConsole(); // And voila ! }
This code does the same job as the first example : it creates a console, activates history and completion and shows it at startup. But here you can overload the OnInput() and OnCompletion() callbacks and program an interactive console. Since the application console is used as default output for the log facility (see nglLog), it is a good idea to instantiate your console as soon as possible (there is no sooner than the first line of application's OnInit() !).
class GLConsole : public nglConsole { nglString mBacklog; public: GLConsole() : nglConsole (false) {} // Hide default console virtual void OnOutput (const nglString& rLine) { mBacklog += rLine; Draw(); } void Draw() { // Here some GL code to display mBacklog content } };
History is not automatically available, you must call UseHistory() first. When enabled, a call to AddToHistory() will insert a new entry into history, and the user will be able navigate through the last entries with the up and down cursor keys. Usually AddToHistory() is called from the OnInput() callback. The history buffer can be limited by line count and/or total chars count, or not limited at all, see SetHistory().
You can also enable history expansion, which is a facility inspired from GNU readline. The user can begin a line with the '!' (exclamation) character, and type the first few letters of a recent command. If expansion is enabled, the console will automatically search (from more recent to older entries) an entry in history matching these first letters when the user strikes 'enter'. Matching case sensitiveness is controled via SetHistoryCase(). The '!!' expression is interpreted as 'repeat last command'. If completion is enabled, it is also possible to complete against history (see completion below).
Completion is not automatically available, you must call UseCompletion() first. When enabled, the user can trigger a completion request by hitting the Tab key. The OnCompletion() callback is triggered, and you are required to give a list of matching items from a context (current line and word to complete). If only one item (one match) is returned, the current word is immediatly replaced with this item. If several matches are returned, the current word is replaced with the least common denominator of all matches : these are the first similar characters of all matches, 'similarity' case sensitiveness being controlled by SetCompletionCase(). When no further completion is possible, hitting Tab twice display all possible matches. See OnCompletion() documentation for more informations.
If both history expansion and completion are enabled, a completion request on the first word (beginning of the line) and with a heading '!' is automatically completed against history. The OnCompletion() callback is not triggered in this case, since it is internally handled by NGL.
Public Member Functions | |
Life cycle | |
| nglConsole (bool IsVisible=false) | |
| virtual | ~nglConsole () |
Output | |
| void | Output (const nglChar *pFormat,...) |
| void | Outputv (const nglChar *pFormat, va_list Args) |
| void | Output (const nglString &rText) |
| void | Show (bool IsVisible=true) |
History | |
| std::list< nglString * > & | GetHistory () |
| int | GetHistory (std::list< const nglString * > &rMatches, nglString &rFilter, bool IsCaseSensitive=false) |
| nglString | GetHistory (uint Line) |
| void | UseHistory (bool Use=true, bool UseExpansion=true) |
| void | SetHistory (uint LineMax=100, uint CharMax=0) |
| void | SetHistoryCase (bool IsCaseSensitive=false) |
| void | AddToHistory (const nglString &rLine) |
| void | ClearHistory () |
Completion | |
| void | UseCompletion (bool Use=true) |
| void | SetCompletionCase (bool IsCaseSensitive=false) |
User callbacks | |
| virtual void | OnOutput (const nglString &rText) |
| virtual void | OnInput (const nglString &rLine) |
| virtual std::list< nglString > & | OnCompletion (nglString &rLine, uint Start, uint End, std::list< nglString > &rAppendTo) |
|
|
Create a console.
|
|
|
Destroy the console. An application can have no console at all, however all nglLog objects using the application console as their only output will be muted, and their text data will be sent into deep space. |
|
|
Add an entry into history.
|
|
|
Remove all entries from history.
|
|
|
Return the Line-nth entry (0 is the more recent one).
|
|
||||||||||||||||
|
Return all history entries whose first characters match a filter
|
|
|
Return all history entries |
|
||||||||||||||||||||
|
Completion request.
std::list<nglString>& MyConsole::OnCompletion (nglString& rLine, uint Start, uint End, std::list<nglString>& rAppendTo) { char* commands[] = {"cd", "ls", "echo", "exit", NULL}; char* args[] = {"toto.jpg", "README", "test.xml", NULL}; int i; if (Start == 0) // first word, this is a command { // End == 0 : no text entered, propose all commands for (i = 0; commands[i]; i++) if ((End == 0) || (!rLine.Compare (commands[i], Start, End-Start))) rAppendTo.push_front (nglString (commands[i])); } else // this is a command argument { // Start == End : no text entered, propose all args for (i = 0; args[i]; i++) if ((Start == End) || (!rLine.Compare (args[i], Start, End-Start))) rAppendTo.push_front (nglString (args[i])); } return rAppendTo; } The current word is completed with the least common denominator of the returned list, if non void. The identification of the first common letters of the matches is controlled by the SetCompletionCase() method. The list is also sorted and displayed when the user hits the Tab key twice. When the line is empty, expect both Start and End to be zero. In any case, check the case where the word to complete is empty (End == Start) is properly handled. |
|
|
A line has been entered in the console (the user pressed Enter).
|
|
|
|
|
|
|
|
||||||||||||
|
|
|
||||||||||||
|
|
|
|
Case sensitiveness for least common denominator of multiple matches, see OnCompletion().
|
|
||||||||||||
|
Set history buffer limits.
|
|
|
Case sensitiveness for history expansion.
|
|
|
Display or hide the console. On Windows, display or hide the custom console window. On Unix and assimilated (BeOS, MacOSX, etc), simply show or hide the prompt. |
|
|
Activate completion.
|
|
||||||||||||
|
Activate history.
|
1.4.1