Mercurial > projects > dynamin
comparison dynamin/core/console.d @ 0:aa4efef0f0b1
Initial commit of code.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Mon, 15 Jun 2009 22:10:48 -0500 |
parents | |
children | 73060bc3f004 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:aa4efef0f0b1 |
---|---|
1 // Written in the D programming language | |
2 // www.digitalmars.com/d/ | |
3 | |
4 /* | |
5 * The contents of this file are subject to the Mozilla Public License Version | |
6 * 1.1 (the "License"); you may not use this file except in compliance with | |
7 * the License. You may obtain a copy of the License at | |
8 * http://www.mozilla.org/MPL/ | |
9 * | |
10 * Software distributed under the License is distributed on an "AS IS" basis, | |
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 * for the specific language governing rights and limitations under the | |
13 * License. | |
14 * | |
15 * The Original Code is the Dynamin library. | |
16 * | |
17 * The Initial Developer of the Original Code is Jordan Miner. | |
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009 | |
19 * the Initial Developer. All Rights Reserved. | |
20 * | |
21 * Contributor(s): | |
22 * Jordan Miner <jminer7@gmail.com> | |
23 * | |
24 */ | |
25 | |
26 module dynamin.core.console; | |
27 | |
28 import dynamin.core.string; | |
29 import dynamin.core_backend; | |
30 import tango.io.Stdout; | |
31 import tango.io.Console; | |
32 | |
33 /// | |
34 enum ConsoleColor { | |
35 // Black, Silver, Maroon, DarkBlue, Green, Purple, DarkYellow, Teal, | |
36 // Gray, White, Red, Blue, LightGreen, Pink, Yellow, Cyan | |
37 Black, /// | |
38 Silver, /// | |
39 Maroon, /// | |
40 DarkBlue, /// | |
41 Green, /// | |
42 Purple, /// | |
43 DarkYellow, /// | |
44 Teal, /// | |
45 Gray, /// | |
46 White, /// | |
47 Red, /// | |
48 Blue, /// | |
49 LightGreen, /// | |
50 Pink, /// | |
51 Yellow, /// | |
52 Cyan /// | |
53 } | |
54 | |
55 /** | |
56 * This class allows programs to read from and write to the console. | |
57 * | |
58 * Programs can also take advantage of some more advanced console features, | |
59 * such as reading input without echoing it to the screen, clearing the screen, | |
60 * setting text foreground and background color, and setting text style. | |
61 * However, the more advanced features will not be available if the program | |
62 * is not actually writing to a console window, such as if its output has be | |
63 * redirected to a file or if another program is receiving its output. | |
64 * The only functions that will work corectly when output or input have been | |
65 * redirected are the following: | |
66 * $(UL | |
67 $(LI Write()) | |
68 $(LI WriteLine()) | |
69 $(LI ReadLine()) | |
70 * ) | |
71 * On Windows, when writing to a console window, all functionality is | |
72 * available except Bold, Italic, Underline, Strikethrough. Windows does | |
73 * not support these. | |
74 * | |
75 * On other systems, colors and styles are supported by writing | |
76 * out control codes. | |
77 */ | |
78 class Console { | |
79 static: | |
80 private: | |
81 mixin ConsoleBackend; | |
82 public: | |
83 //required features: | |
84 //- Easily reading a line at once...input is returned when user presses ENTER | |
85 //- Easily reading a key press from the user...input is returned as soon as it is available | |
86 //- Allow for typing a password and replacing the characters entered with * | |
87 // considering this would actually be fairly hard, I may not implement it... | |
88 // Instead could do as linux and not show anything... | |
89 //wished features: | |
90 //- Getting the screen size | |
91 //- Turning off echoing the key press that the user types when returning | |
92 // input without ENTER | |
93 //- Writing a string, such as "23%", and then changing it to like "24%". | |
94 // Console.WriteBuffer(40) writes 40 spaces and returns a ConsoleBuffer object | |
95 /** | |
96 * Sets whether or not writing to the standard output will be | |
97 * buffered. By default, this is false, meaning that no buffering will | |
98 * ever be done. Setting this to true will buffer output when the standard | |
99 * output is going to a file or stream, such as a text editor, but will | |
100 * not buffer output to a console window. | |
101 * TODO: not implemented on Windows | |
102 */ | |
103 void buffered(bool b) { | |
104 backend_buffered(b); | |
105 } | |
106 /** | |
107 * Writes the specified text string to the console. | |
108 */ | |
109 void write(string s, ...) { | |
110 Stdout.layout.convert(&Stdout.emit, _arguments, _argptr, s); | |
111 } | |
112 /** | |
113 * Writes the specified text string to the console, followed by a newline. | |
114 */ | |
115 void writeLine() { Stdout.newline; } | |
116 /// ditto | |
117 void writeLine(string s, ...) { | |
118 Stdout.layout.convert(&Stdout.emit, _arguments, _argptr, s); | |
119 Stdout.newline; | |
120 } | |
121 /** | |
122 * Reads a line of text from the console. The returned returned string | |
123 * will end in a newline, unless it was read from the last line in a text | |
124 * file. | |
125 */ | |
126 string readLineRaw() { return Cin.copyln(true); } | |
127 /** | |
128 * Reads a line of text from the console. The returned string does not | |
129 * contain a newline. | |
130 */ | |
131 string readLine() { return Cin.copyln(false); } | |
132 /// ditto | |
133 string readLine(string prompt, ...) { | |
134 Stdout.layout.convert(&Stdout.emit, _arguments, _argptr, prompt); | |
135 return readLine(); | |
136 } | |
137 /** | |
138 * reads a character, echoing it to the screen | |
139 * TODO: not implemented | |
140 */ | |
141 string read() { return backend_read(); } | |
142 /// ditto | |
143 string read(string prompt) { | |
144 write(prompt); | |
145 return backend_read(); | |
146 } | |
147 /** | |
148 * reads a line without showing that line | |
149 * TODO: not implemented | |
150 */ | |
151 string readLineHidden() { return backend_readLineHidden(); } | |
152 /// ditto | |
153 string readLineHidden(string prompt) { | |
154 write(prompt); | |
155 return backend_readLineHidden(); | |
156 } | |
157 /** | |
158 * reads a character without showing it | |
159 * TODO: not implemented | |
160 */ | |
161 string readHidden() { return backend_readHidden(); } | |
162 /// ditto | |
163 string readHidden(string prompt) { | |
164 write(prompt); | |
165 return backend_readHidden(); | |
166 } | |
167 /** | |
168 * Clears the text that has been written to the console. | |
169 */ | |
170 void clear() { | |
171 backend_clear(); | |
172 } | |
173 /** | |
174 * Sets the foreground color of text written to the console. | |
175 */ | |
176 void foreColor(ConsoleColor color) { | |
177 backend_foreColor = color; | |
178 } | |
179 /** | |
180 * Sets the background color of text written to the console. | |
181 */ | |
182 void backColor(ConsoleColor color) { | |
183 backend_backColor = color; | |
184 } | |
185 /** | |
186 * Resets the foreground and background colors of text written to the | |
187 * console to the defaults. | |
188 */ | |
189 void resetColors() { | |
190 backend_resetColors(); | |
191 } | |
192 /** | |
193 * Sets whether text written to the console is bold. | |
194 * Has no effect on Windows. | |
195 */ | |
196 void bold(bool b) { | |
197 backend_bold = b; | |
198 } | |
199 /** | |
200 * Sets whether text written to the console is italic. | |
201 * Has no effect on Windows. | |
202 */ | |
203 void italic(bool b) { | |
204 backend_italic = b; | |
205 } | |
206 /** | |
207 * Sets whether text written to the console is underlined. | |
208 * Has no effect on Windows. | |
209 */ | |
210 void underline(bool b) { | |
211 backend_underline = b; | |
212 } | |
213 /** | |
214 * Sets whether text written to the console is strikethrough. | |
215 * Has no effect on Windows. | |
216 */ | |
217 void strikethrough(bool b) { | |
218 backend_strikethrough = b; | |
219 } | |
220 } | |
221 /* "\x1b[9;31mThis is red and strikethrough\x1b[0m" | |
222 */ | |
223 | |
224 /// | |
225 alias Console.readLine readLine; | |
226 /// | |
227 alias Console.writeLine writeLine; | |
228 | |
229 /* unittest { | |
230 Console.foreColor = ConsoleColor.Black; | |
231 writeLine("Black"); | |
232 Console.foreColor = ConsoleColor.Gray; | |
233 writeLine("Gray"); | |
234 Console.foreColor = ConsoleColor.Silver; | |
235 writeLine("Silver"); | |
236 Console.foreColor = ConsoleColor.Red; | |
237 writeLine("Red"); | |
238 Console.foreColor = ConsoleColor.Blue; | |
239 writeLine("Blue"); | |
240 Console.foreColor = ConsoleColor.LightGreen; | |
241 writeLine("LightGreen"); | |
242 Console.foreColor = ConsoleColor.Green; | |
243 writeLine("Green"); | |
244 Console.foreColor = ConsoleColor.Teal; | |
245 writeLine("Teal"); | |
246 Console.foreColor = ConsoleColor.Yellow; | |
247 writeLine("Yellow"); | |
248 Console.foreColor = ConsoleColor.Purple; | |
249 writeLine("Purple"); | |
250 Console.foreColor = ConsoleColor.Pink; | |
251 writeLine("Pink"); | |
252 Console.foreColor = ConsoleColor.DarkYellow; | |
253 writeLine("DarkYellow"); | |
254 Console.foreColor = ConsoleColor.Maroon; | |
255 writeLine("Maroon"); | |
256 Console.foreColor = ConsoleColor.DarkBlue; | |
257 writeLine("DarkBlue"); | |
258 Console.foreColor = ConsoleColor.Cyan; | |
259 writeLine("Cyan"); | |
260 Console.foreColor = ConsoleColor.White; | |
261 writeLine("White"); | |
262 } */ | |
263 |