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.
this(Conduit conduit, bool redirected) [private] #
Attach console input to the provided device
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() [final] #
Return the associated stream
bool redirected() [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) [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() [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) [final] #
Divert input to an alternate source
class Output #
Console output accepts utf8 only
this(Conduit conduit, bool redirected) [private] #
Attach console output to the provided device
Output append(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

Params:

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() [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() [final] #
Return the associated stream
bool redirected() [final] #
Is this device redirected?

Returns:

True if redirected, false otherwise.

Remarks:

Reflects the console redirection status
Output redirected(bool yes) [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() [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) [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.
char[] toString() [override] #
Return the name of this conduit
this(size_t handle) #
Associate this device with a given handle.
This is strictly for adapting existing devices such as Stdout and friends
void reopen(size_t handle_) [private] #
Gain access to the standard IO handles
Console.Input Cin [static] #
Globals representing Console IO
the standard input stream
Console.Output Cout [static] #
Console.Output Cerr [static] #
the standard error stream
static this() #
Instantiate Console access
static ~this() #
Flush outputs before we exit
(good idea from Frits Van Bommel)