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