00001 /******************************************************************************* 00002 00003 @file DisplayWriter.d 00004 00005 Copyright (c) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version; March 2004 00034 Rewritten to support format tags; March 2005 00035 00036 @author Kris 00037 00038 00039 *******************************************************************************/ 00040 00041 module mango.io.DisplayWriter; 00042 00043 public import mango.io.Writer; 00044 00045 private import mango.format.Format, 00046 mango.format.Double; 00047 00048 /******************************************************************************* 00049 00050 Format output suitable for presentation. DisplayWriter provide 00051 the means to append formatted data to an IBuffer, and exposes 00052 a convenient method of handling a variety of data types. 00053 00054 DisplayWriter supports the usual printf() format specifiers & flags, 00055 and extends the notion to operate with one dimensional arrays. For 00056 instance, this code 00057 00058 @code 00059 static int x = [1, 2, 3, 4, 5, 6, 7, 8]; 00060 00061 Stdout.print ("%@04b, ", x); 00062 @endcode 00063 00064 results in the following output: 00065 00066 @code 00067 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 00068 @endcode 00069 00070 Note that DisplayWriter itself is a wrapper around the mango.format 00071 package, which can be used directly as desired (Int, Long, Double, 00072 DGDouble, etc). The latter classes are home to a set of static 00073 formatting-methods, making them convenient for ad-hoc application. 00074 00075 Note also that Mango provides both a printf() and writef() wrapper 00076 for compatability purposes ~ see module TextFormat. 00077 00078 *******************************************************************************/ 00079 00080 class DisplayWriter : Writer 00081 { 00082 private Format.Style style; 00083 private char[80] workspace; 00084 00085 /*********************************************************************** 00086 00087 Construct a DisplayWriter upon the specified IBuffer. 00088 One can override the default floating-point formatting 00089 by providing an appropriate handler to this constructor. 00090 For example, one might configure the DGDouble.format() 00091 function instead. 00092 00093 ***********************************************************************/ 00094 00095 this (IBuffer buffer, char[] workspace = null, Format.DblFormat df = &Double.format) 00096 { 00097 super (buffer); 00098 00099 // disable array length output (for strings and so on) 00100 enableArrayPrefix (false); 00101 00102 // configure style output-handler, workspace, and the 00103 // floating-point formatter 00104 if (workspace == null) 00105 workspace = this.workspace; 00106 style (&utf8, workspace, df); 00107 } 00108 00109 /*********************************************************************** 00110 00111 Construct a DisplayWriter upon the specified IConduit 00112 00113 ***********************************************************************/ 00114 00115 this (IConduit conduit) 00116 { 00117 this (conduit.createBuffer); 00118 } 00119 00120 /*********************************************************************** 00121 00122 Intercept discrete output and convert it to printable form 00123 00124 ***********************************************************************/ 00125 00126 override IWriter write (void* src, uint bytes, int type) 00127 { 00128 Format.print (style, src, bytes, type); 00129 return this; 00130 } 00131 00132 /*********************************************************************** 00133 00134 Format a set of arguments a la printf(). Please see module 00135 mango.format.Format for details 00136 00137 ***********************************************************************/ 00138 00139 int print (char[] s, TypeInfo[] ti, void* args) 00140 { 00141 style.setFormat (s); 00142 return Format.print (style, ti, args); 00143 } 00144 00145 /*********************************************************************** 00146 00147 Format a set of arguments a la printf(). Please see module 00148 mango.format.Format for details 00149 00150 ***********************************************************************/ 00151 00152 DisplayWriter print (char[] s, ...) 00153 { 00154 print (s, _arguments, _argptr); 00155 return this; 00156 } 00157 00158 /*********************************************************************** 00159 00160 Format a set of arguments a la printf(). Please see module 00161 mango.format.Format for details 00162 00163 ***********************************************************************/ 00164 00165 DisplayWriter println (char[] s, ...) 00166 { 00167 print (s, _arguments, _argptr); 00168 put (CR); 00169 return this; 00170 } 00171 00172 /*********************************************************************** 00173 00174 Utf8 handler provided to the formatting package, which 00175 redirects the output via the normal char[] processing. 00176 00177 ***********************************************************************/ 00178 00179 private final int utf8 (char[] x) 00180 { 00181 put (x); 00182 return x.length; 00183 } 00184 }