/*========================================================================== * flatio.d * Written in the D Programming Language (http://www.digitalmars.com/d) */ /*************************************************************************** * Function-style wrappers around Tango's Stdout. * * Stdout is fine, but for quick coding having to type Stdout.formatln * everywhere gets tedious. Furthermore since Stdout is a dynamic * object you can't make an alias of it's methods, nor can you make a * module-level delegate that points to it. * * If you long for the simplicity of good ole Phobos * std.stdio.writef/writefln, then this module is for you! This * module defines the following plain functions: * * print,format,formatln,flush, and newline * * which use Stdout under the hood to do the equivalent of * Stdout.print,Stdout.format, etc. * * Since these functions use the global singleton Stdout object for * output, so any changes made to Stdout (e.g. changing the underlying * output stream it uses or setting Stdout.flush = true ) will be * reflected in the behavior of these functions. * * Note that these functions all return Stdout, and so calls can be * chained just as when using Stdout directly, e.g. --- print("foo")("Bar")().newline.flush; --- * * Authors: William V. Baxter III * Date: 14 Feb 2008 * Copyright: (C) 2008 William Baxter * License: Public Domain where permitted by law, ZLIB/PNG elsewhere. */ //=========================================================================== module flatio; import tango.io.Stdout; /********************************************************************** Layout using the provided formatting specification **********************************************************************/ typeof(Stdout) format(char[] fmt, ...) { Stdout.layout()(&Stdout.sink, _arguments, _argptr, fmt); return Stdout; } /********************************************************************** Layout using the provided formatting specification, and finish with a newline. **********************************************************************/ typeof(Stdout) formatln(char[] fmt, ...) { Stdout.layout()(&Stdout.sink, _arguments, _argptr, fmt); return Stdout.newline; } /********************************************************************** Unformatted layout, with commas inserted between args. Currently supports a maximum of 24 arguments print() with no arguments always flushes the stream. **********************************************************************/ typeof(Stdout) print (...) { static char[] slice = "{}, {}, {}, {}, {}, {}, {}, {}, " "{}, {}, {}, {}, {}, {}, {}, {}, " "{}, {}, {}, {}, {}, {}, {}, {}, "; assert (_arguments.length <= slice.length/4, "Print :: too many arguments"); if (_arguments.length is 0) Stdout.flush; else Stdout.layout()(&Stdout.sink, _arguments, _argptr, slice[0 .. _arguments.length * 4 - 2]); return Stdout; } /********************************************************************** Flush the output stream **********************************************************************/ typeof(Stdout) flush () { Stdout.flush(); return Stdout; } //--- Emacs setup --- // Local Variables: // c-basic-offset: 4 // indent-tabs-mode: nil // End: