tango.io.Console

License:

BSD style: see license.txt

Version:

Feb 2005: Initial release Nov 2005: Heavily revised for unicode Dec 2006: Outback release

Author:

Kris
struct Console
Low-level console IO support.
Note that for a while this was templated for each of char, wchar, and dchar. It became clear after some usage that the console is more useful if it sticks to UTF8 only. See Console.Conduit below for details.

Redirecting the standard IO handles (via a shell) operates as one would expect, though the redirected content should likely restrict itself to UTF8.
class Input
Model console input as a buffer. Note that we read UTF8 only.
char[] copyln(bool raw = false) [final]
Return the next line available from the console, or null when there is nothing available. The value returned is a duplicate of the buffer content (it has .dup applied).
Each line ending is removed unless parameter raw is set to true.
bool readln(ref char[] content, bool raw = false) [final]
Retreive a line of text from the console and map it to the given argument. The input is sliced, not copied, so use .dup appropriately. Each line ending is removed unless parameter raw is set to true.
Returns false when there is no more input.
InputStream stream() [@property, final]
Return the associated stream.
bool redirected() [@property, final]
Is this device redirected?

Returns:

True if redirected, false otherwise.

Remarks:

Reflects the console redirection status from when this module was instantiated.
Input redirected(bool yes) [@property, final]
Set redirection state to the provided boolean.

Remarks:

Configure the console redirection status, where a redirected console is more efficient (dictates whether newline() performs automatic flushing or not.)
InputStream input() [@property, final]
Returns the configured source

Remarks:

Provides access to the underlying mechanism for console input. Use this to retain prior state when temporarily switching inputs.
Input input(InputStream source) [@property, final]
Divert input to an alternate source.
class Output
Console output accepts UTF8 only.
Output append(const(char[]) x) [final]
Append to the console. We accept UTF8 only, so all other encodings should be handled via some higher level API.
Output append(Object other) [final]
Append content.

Parameters:

otherAn object with a useful toString() method.

Returns:

Returns a chaining reference if all content was written. Throws an IOException indicating Eof or Eob if not.

Remarks:

Append the result of other.toString() to the console.
Output newline() [@property, final]
Append a newline and flush the console buffer. If the output is redirected, flushing does not occur automatically.

Returns:

Returns a chaining reference if content was written. Throws an IOException indicating Eof or Eob if not.

Remarks:

Emit a newline into the buffer, and autoflush the current buffer content for an interactive console. Redirected consoles do not flush automatically on a newline.
Output flush() [final]
Explicitly flush console output.

Returns:

Returns a chaining reference if content was written. Throws an IOException indicating Eof or Eob if not.

Remarks:

Flushes the console buffer to attached conduit.
OutputStream stream() [@property, final]
Return the associated stream.
bool redirected() [@property, final]
Is this device redirected?

Returns:

True if redirected, false otherwise.

Remarks:

Reflects the console redirection status.
Output redirected(bool yes) [@property, final]
Set redirection state to the provided boolean.

Remarks:

Configure the console redirection status, where a redirected console is more efficient (dictates whether newline() performs automatic flushing or not.)
OutputStream output() [@property, final]
Returns the configured output sink.

Remarks:

Provides access to the underlying mechanism for console output. Use this to retain prior state when temporarily switching outputs.
Output output(OutputStream sink) [@property, final]
Divert output to an alternate sink.
class Conduit : Device
Conduit for specifically handling the console devices. This takes care of certain implementation details on the Win32 platform.
Note that the console is fixed at UTF8 for both linux and Win32. The latter is actually UTF16 native, but it's just too much hassle for a developer to handle the distinction when it really should be a no-brainer. In particular, the Win32 console functions don't work with redirection. This causes additional difficulties that can be ameliorated by asserting console I/O is always UTF8, in all modes.
immutable(char)[] toString() [override]
Return the name of this conduit.
Console.Input Cin [gshared, static]
Globals representing Console IO.
The standard input stream.
Console.Output Cout [gshared, static]
The standard output stream.
Console.Output Cerr [gshared, static]
The standard error stream.
static this() [shared]
Instantiate Console access.
static ~this()
Flush outputs before we exit.
(Good idea from Frits Van Bommel.)