String.Split(char[],int,StringSplitOptions) should remove empty entries while
[mono.git] / mcs / class / corlib / System / ConsoleDriver.cs
1 //
2 // System.ConsoleDriver
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 #if NET_2_0
31 using System.IO;
32 using System.Runtime.CompilerServices;
33
34 namespace System {
35         class ConsoleDriver {
36                 internal static IConsoleDriver driver;
37                 static bool is_console;
38                 static bool called_isatty;
39
40                 static ConsoleDriver ()
41                 {
42                         if (!IsConsole) {
43                                 driver = new NullConsoleDriver ();
44                         } else if (Environment.IsRunningOnWindows) {
45                                 driver = new WindowsConsoleDriver ();
46                         } else {
47                                 driver = new TermInfoDriver (Environment.GetEnvironmentVariable ("TERM"));
48                         }
49                 }
50
51                 public static bool Initialized {
52                         get { return driver.Initialized; }
53                 }
54
55                 public static ConsoleColor BackgroundColor {
56                         get { return driver.BackgroundColor; }
57                         set {
58                                 if (value < ConsoleColor.Black || value > ConsoleColor.White)
59                                         throw new ArgumentOutOfRangeException ("value", "Not a ConsoleColor value.");
60
61                                 driver.BackgroundColor = value;
62                         }
63                 }
64
65                 public static int BufferHeight {
66                         get { return driver.BufferHeight; }
67                         set { driver.BufferHeight = value; }
68                 }
69
70                 public static int BufferWidth {
71                         get { return driver.BufferWidth; }
72                         set { driver.BufferWidth = value; }
73                 }
74
75                 public static bool CapsLock {
76                         get { return driver.CapsLock; }
77                 }
78
79                 public static int CursorLeft {
80                         get { return driver.CursorLeft; }
81                         set { driver.CursorLeft = value; }
82                 }
83
84                 public static int CursorSize {
85                         get { return driver.CursorSize; }
86                         set { driver.CursorSize = value; }
87                 }
88
89                 public static int CursorTop {
90                         get { return driver.CursorTop; }
91                         set { driver.CursorTop = value; }
92                 } 
93                 
94                 public static bool CursorVisible {
95                         get { return driver.CursorVisible; }
96                         set { driver.CursorVisible = value; }
97                 }
98
99                 public static bool KeyAvailable {
100                         get { return driver.KeyAvailable; }
101                 }
102
103                 public static ConsoleColor ForegroundColor {
104                         get { return driver.ForegroundColor; }
105                         set {
106                                 if (value < ConsoleColor.Black || value > ConsoleColor.White)
107                                         throw new ArgumentOutOfRangeException ("value", "Not a ConsoleColor value.");
108
109                                 driver.ForegroundColor = value;
110                         }
111                 }
112
113                 public static int LargestWindowHeight {
114                         get { return driver.LargestWindowHeight; }
115                 }
116
117                 public static int LargestWindowWidth {
118                         get { return driver.LargestWindowWidth; }
119                 }
120
121                 public static bool NumberLock {
122                         get { return driver.NumberLock; }
123                 }
124
125                 public static string Title {
126                         get { return driver.Title; }
127                         set { driver.Title = value; }
128                 }
129
130                 public static bool TreatControlCAsInput {
131                         get { return driver.TreatControlCAsInput; }
132                         set { driver.TreatControlCAsInput = value; }
133                 } 
134
135                 public static int WindowHeight {
136                         get { return driver.WindowHeight; }
137                         set { driver.WindowHeight = value; }
138                 }
139
140                 public static int WindowLeft {
141                         get { return driver.WindowLeft; }
142                         set { driver.WindowLeft = value; }
143                 }
144
145                 public static int WindowTop {
146                         get { return driver.WindowTop; }
147                         set { driver.WindowTop = value; }
148                 }
149
150                 public static int WindowWidth {
151                         get { return driver.WindowWidth; }
152                         set { driver.WindowWidth = value; }
153                 }
154
155                 public static void Beep (int frequency, int duration)
156                 {
157                         driver.Beep (frequency, duration);
158                 }
159                 
160                 public static void Clear ()
161                 {
162                         driver.Clear ();
163                 }
164
165                 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
166                                         int targetLeft, int targetTop)
167                 {
168                         MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight,
169                                         targetLeft, targetTop, ' ', 0, 0);
170                 }
171
172                 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
173                                         int targetLeft, int targetTop, char sourceChar,
174                                         ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
175                 {
176                         driver.MoveBufferArea (sourceLeft, sourceTop, sourceWidth, sourceHeight,
177                                         targetLeft, targetTop, sourceChar, sourceForeColor, sourceBackColor);
178                 }
179
180                 public static void Init ()
181                 {
182                         driver.Init ();
183                 }
184
185                 public static int Read ()
186                 {
187                         return ReadKey (false).KeyChar;
188                 }
189
190                 public static string ReadLine ()
191                 {
192                         return driver.ReadLine ();
193                 }
194
195                 public static ConsoleKeyInfo ReadKey (bool intercept)
196                 {
197                         return driver.ReadKey (intercept);
198                 }
199
200                 public static void ResetColor ()
201                 {
202                         driver.ResetColor ();
203                 }
204
205                 public static void SetBufferSize (int width, int height)
206                 {
207                         driver.SetBufferSize (width, height);
208                 }
209
210                 public static void SetCursorPosition (int left, int top)
211                 {
212                         driver.SetCursorPosition (left, top);
213                 }
214
215                 public static void SetWindowPosition (int left, int top)
216                 {
217                         driver.SetWindowPosition (left, top);
218                 }
219
220                 public static void SetWindowSize (int width, int height)
221                 {
222                         driver.SetWindowSize (width, height);
223                 }
224
225                 public static bool IsConsole {
226                         get {
227                                 if (called_isatty)
228                                         return is_console;
229
230                                 is_console = (Isatty (MonoIO.ConsoleOutput) && Isatty (MonoIO.ConsoleInput));
231                                 called_isatty = true;
232                                 return is_console;
233                         }
234                 }
235
236                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
237                 static extern bool Isatty (IntPtr handle);
238
239                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
240                 internal static extern int InternalKeyAvailable (int ms_timeout);
241
242                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
243                 internal static extern bool TtySetup (string teardown, out byte verase, out byte vsusp, out byte intr);
244
245                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
246                 internal static extern bool SetEcho (bool wantEcho);
247
248                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
249                 internal static extern bool SetBreak (bool wantBreak);
250
251                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
252                 internal static extern bool GetTtySize (IntPtr handle, out int width, out int height);
253
254                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
255                 internal static extern void Suspend ();
256         }
257 }
258 #endif
259