~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins.Setup/Mono.Addins.Setup.ProgressMonitoring/ConsoleProgressMonitor.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ConsoleProgressMonitor.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using System.IO;
 
31
 
 
32
namespace Mono.Addins.Setup.ProgressMonitoring
 
33
{
 
34
        internal class ConsoleProgressMonitor: NullProgressMonitor
 
35
        {
 
36
                int columns = 80;
 
37
                bool indent = true;
 
38
                bool wrap = true;
 
39
                int ilevel = 0;
 
40
                int isize = 3;
 
41
                int col = -1;
 
42
                int logLevel;
 
43
                LogTextWriter logger;
 
44
                
 
45
                public ConsoleProgressMonitor (): this (1)
 
46
                {
 
47
                }
 
48
                
 
49
                public ConsoleProgressMonitor (int logLevel)
 
50
                {
 
51
                        this.logLevel = logLevel;
 
52
                        logger = new LogTextWriter ();
 
53
                        logger.TextWritten += new LogTextEventHandler (WriteLog);
 
54
                }
 
55
                
 
56
                public bool WrapText {
 
57
                        get { return wrap; }
 
58
                        set { wrap = value; }
 
59
                }
 
60
                
 
61
                public int WrapColumns {
 
62
                        get { return columns; }
 
63
                        set { columns = value; }
 
64
                }
 
65
                
 
66
                public bool IndentTasks {
 
67
                        get { return indent; }
 
68
                        set { indent = value; }
 
69
                }
 
70
                
 
71
                public override int LogLevel {
 
72
                        get { return logLevel; }
 
73
                }
 
74
                
 
75
                public override void BeginTask (string name, int totalWork)
 
76
                {
 
77
                        WriteText (name);
 
78
                        Indent ();
 
79
                }
 
80
                
 
81
                public override void BeginStepTask (string name, int totalWork, int stepSize)
 
82
                {
 
83
                        BeginTask (name, totalWork);
 
84
                }
 
85
                
 
86
                public override void EndTask ()
 
87
                {
 
88
                        Unindent ();
 
89
                }
 
90
                
 
91
                void WriteLog (string text)
 
92
                {
 
93
                        WriteText (text);
 
94
                }
 
95
                
 
96
                public override TextWriter Log {
 
97
                        get { return logger; }
 
98
                }
 
99
                
 
100
                public override void ReportSuccess (string message)
 
101
                {
 
102
                        WriteText (message);
 
103
                }
 
104
                
 
105
                public override void ReportWarning (string message)
 
106
                {
 
107
                        if (logLevel != 0)
 
108
                                WriteText ("WARNING: " + message + "\n");
 
109
                }
 
110
                
 
111
                public override void ReportError (string message, Exception ex)
 
112
                {
 
113
                        if (logLevel == 0)
 
114
                                return;
 
115
                        
 
116
                        if (message != null && ex != null) {
 
117
                                WriteText ("ERROR: " + message + "\n");
 
118
                                if (logLevel > 1)
 
119
                                        WriteText (ex + "\n");
 
120
                        }
 
121
                        if (message != null)
 
122
                                WriteText ("ERROR: " + message + "\n");
 
123
                        else if (ex != null) {
 
124
                                if (logLevel > 1)
 
125
                                        WriteText ("ERROR: " + ex + "\n");
 
126
                                else
 
127
                                        WriteText ("ERROR: " + ex.Message + "\n");
 
128
                        }
 
129
                }
 
130
                
 
131
                void WriteText (string text)
 
132
                {
 
133
                        if (indent)
 
134
                                WriteText (text, ilevel);
 
135
                        else
 
136
                                WriteText (text, 0);
 
137
                }
 
138
                
 
139
                void WriteText (string text, int leftMargin)
 
140
                {
 
141
                        if (text == null || text.Length == 0)
 
142
                                return;
 
143
 
 
144
                        int n = 0;
 
145
                        int maxCols = wrap ? columns : int.MaxValue;
 
146
 
 
147
                        while (n < text.Length)
 
148
                        {
 
149
                                if (col == -1) {
 
150
                                        Console.Write (new String (' ', leftMargin));
 
151
                                        col = leftMargin;
 
152
                                }
 
153
                                
 
154
                                int lastWhite = -1;
 
155
                                int sn = n;
 
156
                                bool eol = false;
 
157
                                
 
158
                                while (col < maxCols && n < text.Length) {
 
159
                                        char c = text [n];
 
160
                                        if (c == '\r') {
 
161
                                                n++;
 
162
                                                continue;
 
163
                                        }
 
164
                                        if (c == '\n') {
 
165
                                                eol = true;
 
166
                                                break;
 
167
                                        }
 
168
                                        if (char.IsWhiteSpace (c))
 
169
                                                lastWhite = n;
 
170
                                        col++;
 
171
                                        n++;
 
172
                                }
 
173
                                
 
174
                                if (lastWhite == -1 || col < maxCols)
 
175
                                        lastWhite = n;
 
176
                                else if (col >= maxCols)
 
177
                                        n = lastWhite + 1;
 
178
                                
 
179
                                Console.Write (text.Substring (sn, lastWhite - sn));
 
180
                                
 
181
                                if (eol || col >= maxCols) {
 
182
                                        col = -1;
 
183
                                        Console.WriteLine ();
 
184
                                        if (eol) n++;
 
185
                                }
 
186
                        }
 
187
                }
 
188
                
 
189
                void Indent ()
 
190
                {
 
191
                        ilevel += isize;
 
192
                        if (col != -1) {
 
193
                                Console.WriteLine ();
 
194
                                col = -1;
 
195
                        }
 
196
                }
 
197
                
 
198
                void Unindent ()
 
199
                {
 
200
                        ilevel -= isize;
 
201
                        if (ilevel < 0) ilevel = 0;
 
202
                        if (col != -1) {
 
203
                                Console.WriteLine ();
 
204
                                col = -1;
 
205
                        }
 
206
                }
 
207
        }
 
208
}