1 // NAnt - A .NET build tool
\r
2 // Copyright (C) 2001 Gerry Shaw
\r
4 // This program is free software; you can redistribute it and/or modify
\r
5 // it under the terms of the GNU General Public License as published by
\r
6 // the Free Software Foundation; either version 2 of the License, or
\r
7 // (at your option) any later version.
\r
9 // This program is distributed in the hope that it will be useful,
\r
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 // GNU General Public License for more details.
\r
14 // You should have received a copy of the GNU General Public License
\r
15 // along with this program; if not, write to the Free Software
\r
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
18 // Gerry Shaw (gerry_shaw@yahoo.com)
\r
20 namespace SourceForge.NAnt {
\r
23 using System.Collections;
\r
27 public class TextWriterCollection : ArrayList {
\r
32 static bool _autoFlush = false;
\r
33 static int _indentLevel = 0;
\r
34 static int _indentSize = 4;
\r
36 static TextWriterCollection _listeners;
\r
42 // make sure we release any open file handles
\r
46 public static bool AutoFlush {
\r
47 get { return _autoFlush; }
\r
48 set { _autoFlush = value; }
\r
51 public static int IndentLevel {
\r
52 get { return _indentLevel; }
\r
53 set { _indentLevel = value; }
\r
56 public static int IndentSize {
\r
57 get { return _indentSize; }
\r
58 set { _indentSize = value; }
\r
61 public static TextWriterCollection Listeners {
\r
63 if (_listeners == null) {
\r
64 _listeners = new TextWriterCollection();
\r
65 _listeners.Add(Console.Out);
\r
71 public static void Close() {
\r
72 foreach (TextWriter writer in Listeners) {
\r
73 // never close the Console.Out writer
\r
74 if (writer != Console.Out) {
\r
80 public static void Flush() {
\r
81 foreach (TextWriter writer in Listeners) {
\r
86 public static void Indent() {
\r
90 public static void Unindent() {
\r
91 if (IndentLevel <= 0) {
\r
92 throw new InvalidOperationException("IndentLevel must be greater than zero before calling Unindent()");
\r
98 /// Flag to indicate next string will start on a new line so that it can be indented.
\r
100 private static bool _newline = true;
\r
102 private static void PreprocessValue(ref string value) {
\r
103 // if we are starting a new line then first indent the string
\r
105 if (IndentLevel > 0) {
\r
106 StringBuilder sb = new StringBuilder(value);
\r
107 sb.Insert(0, " ", IndentLevel * IndentSize);
\r
108 value = sb.ToString();
\r
114 public static void Write(string value) {
\r
115 PreprocessValue(ref value);
\r
116 foreach (TextWriter writer in Listeners) {
\r
117 writer.Write(value);
\r
121 foreach (TextWriter writer in Listeners) {
\r
127 public static void WriteLine() {
\r
128 WriteLine(String.Empty);
\r
131 public static void WriteLine(string value) {
\r
132 PreprocessValue(ref value);
\r
133 foreach (TextWriter writer in Listeners) {
\r
134 writer.WriteLine(value);
\r
138 foreach (TextWriter writer in Listeners) {
\r
143 // make sure we indent the next line
\r
147 public static void Write(string format, params object[] arg) {
\r
148 Write(String.Format(format, arg));
\r
151 public static void WriteLine(string format, params object[] arg) {
\r
152 WriteLine(String.Format(format, arg));
\r