comparison dynamin/core/windows_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.windows_console;
27
28 public import dynamin.c.windows;
29 public import dynamin.core.string;
30
31 public import tango.io.Stdout;
32 public import tango.stdc.stdlib;
33
34 template ConsoleBackend() {
35 uint inputCP;
36 uint outputCP;
37 static this() {
38 // calling GetConsoleOutputCP() takes twice as long as
39 // the entire backend_Write() function...it is slow!
40 //inputCP = GetConsoleCP();
41 //outputCP = GetConsoleOutputCP();
42 }
43 bool buffered = false;
44 void backend_buffered(bool b) {
45 buffered = b;
46 }
47 void backend_write(string s) {
48 // the reasons for this function being slower than writef():
49 // - partly the conversion overhead (UTF-8 -> UTF16 -> CP)
50 // - partly because it is not buffered
51 /+
52 // make sure printf/writef output appears in right order
53 fflush(stdout);
54
55 auto wbuffer = ToUtf16(s);
56 scope(exit) delete wbuffer;
57 auto needed = WideCharToMultiByte(outputCP, 0, wbuffer.ptr, wbuffer.length, null, 0, null, null);
58 // faster than: auto buffer = new char[needed];
59 auto buffer = (cast(char*)alloca(needed))[0..needed];
60 scope(exit) delete buffer;
61 WideCharToMultiByte(outputCP, 0, wbuffer.ptr, wbuffer.length, buffer.ptr, buffer.length, null, null);
62
63 auto stdOut = GetStdHandle(-11);
64 DWORD numWritten;
65 if(!WriteFile(stdOut, buffer.ptr, buffer.length, &numWritten, null))
66 printf("WriteFile() failed, error %d\n", GetLastError());+/
67
68 }
69 string backend_readLineRaw() {/+
70 auto stdIn = GetStdHandle(-10);
71 // TODO: does not work if input is from a file!
72 // if reading from a file, a line can be very long...
73 // and you do not want to read farther than that.
74 char[4096] buffer = void;
75 DWORD numRead;
76 if(!ReadFile(stdIn, buffer.ptr, buffer.length, &numRead, null))
77 printf("ReadFile() failed, error %d\n", GetLastError());
78
79 auto needed = MultiByteToWideChar(inputCP, 0, buffer.ptr, numRead, null, 0);
80 //faster than: auto wbuffer = new wchar[needed];
81 auto wbuffer = (cast(wchar*)alloca(needed*2))[0..needed];
82 scope(exit) delete wbuffer;
83 auto numUsed = MultiByteToWideChar(inputCP, 0, buffer.ptr, numRead, wbuffer.ptr, wbuffer.length);
84 return ToUtf8(wbuffer[0..numUsed]);+/
85 return null;
86 }
87 string backend_read() {
88 return null;
89 }
90 string backend_readLineHidden() {
91 return null;
92 }
93 string backend_readHidden() {
94 return null;
95 }
96 void backend_clear() {
97 system("cls");
98 }
99 uint backend_getColorFlags(ConsoleColor c, bool fore) {
100 uint i = fore ? FOREGROUND_INTENSITY : BACKGROUND_INTENSITY;
101 uint r = fore ? FOREGROUND_RED : BACKGROUND_RED;
102 uint g = fore ? FOREGROUND_GREEN : BACKGROUND_GREEN;
103 uint b = fore ? FOREGROUND_BLUE : BACKGROUND_BLUE;
104 switch(c) {
105 case c.Black: return 0;
106 case c.Silver: return r | g | b;
107 case c.Maroon: return r;
108 case c.DarkBlue: return b;
109 case c.Green: return g;
110 case c.Purple: return r | b;
111 case c.DarkYellow: return r | g;
112 case c.Teal: return g | b;
113 case c.Gray: return i;
114 case c.White: return r | g | b | i;
115 case c.Red: return r | i;
116 case c.Blue: return b | i;
117 case c.LightGreen: return g | i;
118 case c.Pink: return r | b | i;
119 case c.Yellow: return r | g | i;
120 case c.Cyan: return g | b | i;
121 default: assert(0);
122 }
123 }
124 void backend_foreColor(ConsoleColor color) {
125 CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
126 if(!GetConsoleScreenBufferInfo(GetStdHandle(-11), &bufferInfo))
127 return;
128 bufferInfo.wAttributes &= ~0b1111; // clear the 4 foreground bits
129 SetConsoleTextAttribute(GetStdHandle(-11),
130 bufferInfo.wAttributes | backend_getColorFlags(color, true));
131 }
132 void backend_backColor(ConsoleColor color) {
133 CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
134 if(!GetConsoleScreenBufferInfo(GetStdHandle(-11), &bufferInfo))
135 return;
136 bufferInfo.wAttributes &= ~0b11110000; // clear the 4 background bits
137 SetConsoleTextAttribute(GetStdHandle(-11),
138 bufferInfo.wAttributes | backend_getColorFlags(color, false));
139 }
140 void backend_resetColors() {
141 }
142 void backend_bold(bool b) { }
143 void backend_italic(bool b) { }
144 void backend_underline(bool b) { }
145 void backend_strikethrough(bool b) { }
146 }
147