tango.io.stream.Format

License:

BSD style: see license.txt

Version:

Initial release: Oct 2007

Author:

Kris
class FormatOutput(T) : OutputFilter #
A bridge between a Layout instance and a stream. This is used for the Stdout & Stderr globals, but can be used for general purpose buffer-formatting as desired. The Template type 'T' dictates the text arrangement within the target buffer ~ one of char, wchar or dchar (utf8, utf16, or utf32). FormatOutput exposes this style of usage:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
auto print = new FormatOutput!(char) (...);

print ("hello");                        => hello
print (1);                              => 1
print (3.14);                           => 3.14
print ('b');                            => b
print (1, 2, 3);                        => 1, 2, 3         
print ("abc", 1, 2, 3);                 => abc, 1, 2, 3        
print ("abc", 1, 2) ("foo");            => abc, 1, 2foo        
print ("abc") ("def") (3.14);           => abcdef3.14

print.format ("abc {}", 1);             => abc 1
print.format ("abc {}:{}", 1, 2);       => abc 1:2
print.format ("abc {1}:{0}", 1, 2);     => abc 2:1
print.format ("abc ", 1);               => abc
Note that the last example does not throw an exception. There are several use-cases where dropping an argument is legitimate, so we're currently not enforcing any particular trap mechanism.

Flushing the output is achieved through the flush() method, or via an empty pair of parens:

1
2
3
4
5
print ("hello world") ();
print ("hello world").flush;

print.format ("hello {}", "world") ();
print.format ("hello {}", "world").flush;
Special character sequences, such as "\n", are written directly to the output without any translation (though an output-filter could be inserted to perform translation as required). Platform-specific newlines are generated instead via the newline() method, which also flushes the output when configured to do so:
1
2
3
print ("hello ") ("world").newline;
print.format ("hello {}", "world").newline;
print.formatln ("hello {}", "world");

The format() method supports the range of formatting options exposed by tango.text.convert.Layout and extensions thereof; including the full I18N extensions where configured in that manner. To create a French instance of FormatOutput:

1
2
3
4
import tango.text.locale.Locale;

auto locale = new Locale (Culture.getCulture ("fr-FR"));
auto print = new FormatOutput!(char) (locale, ...);

Note that FormatOutput is *not* intended to be thread-safe

this(OutputStream output, T[] eol = Eol) #
Construct a FormatOutput instance, tying the provided stream to a layout formatter
this(Layout!(T) convert, OutputStream output, T[] eol = Eol) #
Construct a FormatOutput instance, tying the provided stream to a layout formatter
FormatOutput format(T[] fmt, ...) [final] #
Layout using the provided formatting specification
FormatOutput formatln(T[] fmt, ...) [final] #
Layout using the provided formatting specification
FormatOutput print(...) [final] #
Unformatted layout, with commas inserted between args. Currently supports a maximum of 24 arguments
FormatOutput newline() [final] #
Output a newline and optionally flush
FormatOutput flush(bool yes) [final] #
Control implicit flushing of newline(), where true enables flushing. An explicit flush() will always flush the output.
OutputStream stream() [final] #
Return the associated output stream
FormatOutput stream(OutputStream output) [final] #
Set the associated output stream
Layout!(T) layout() [final] #
Return the associated Layout
FormatOutput layout(Layout!(T) layout) [final] #
Set the associated Layout
uint emit(T[] s) [private, final] #
Sink for passing to the formatter