comparison dynamin/core/unix_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 c9a4850926d9
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) 2007-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.unix_console;
27
28 public import tango.stdc.stdlib;
29 //public import tango.stdc.string;
30 public import dynamin.core.string;
31
32 /*
33 Can get width of console with $COLUMNS
34 Can get height of console with $LINES
35 */
36 template ConsoleBackend() {
37 static colorsUsed = false;
38 static ~this() {
39 if(colorsUsed)
40 backend_resetColors();
41 }
42 bool buffered = false;
43 void backend_buffered(bool b) {
44 buffered = b;
45 }
46 void backend_write(string s) {
47 /*fwrite(s.ptr, 1, s.length, stdout);
48 if(!buffered)
49 fflush(stdout);*/
50 }
51 string backend_readLineRaw() {
52 /*uint size;
53 char* line;
54 auto numRead = getline(&line, &size, stdin);
55 scope(exit) free(line);
56 string str = new char[numRead];
57 str[] = line[0..numRead];
58 return str;*/
59 return null;
60 }
61 string backend_read() {
62 return null;
63 }
64 // TODO: use this
65 //termios ts;
66 //tcgetattr(filedes, &ts);
67 //ts.c_lflag |= ICANON; // turns on waiting for a whole lines
68 //ts.c_lflag |= ECHO; // turns on echoing
69 //tcsetattr(filedes, TCSAFLUSH, &ts);
70 string backend_readLineHidden() {
71 system("stty -echo");
72 auto line = readLine();
73 system("stty echo");
74 return line;
75 }
76 string backend_readHidden() {
77 return null;
78 }
79 void backend_clear() {
80 system("clear");
81 }
82 string backend_getColorStr(ConsoleColor c, bool fore) {
83 switch(c) {
84 case c.Black: return fore ? "\x1b[30m" : "\x1b[40m";
85 case c.Silver: return fore ? "\x1b[37m" : "\x1b[47m";
86 case c.Maroon: return fore ? "\x1b[31m" : "\x1b[41m";
87 case c.DarkBlue: return fore ? "\x1b[34m" : "\x1b[44m";
88 case c.Green: return fore ? "\x1b[32m" : "\x1b[42m";
89 case c.Purple: return fore ? "\x1b[35m" : "\x1b[45m";
90 case c.DarkYellow: return fore ? "\x1b[33m" : "\x1b[43m";
91 case c.Teal: return fore ? "\x1b[36m" : "\x1b[46m";
92 case c.Gray: return fore ? "\x1b[90m" : "\x1b[100m";
93 case c.White: return fore ? "\x1b[97m" : "\x1b[107m";
94 case c.Red: return fore ? "\x1b[91m" : "\x1b[101m";
95 case c.Blue: return fore ? "\x1b[94m" : "\x1b[104m";
96 case c.LightGreen: return fore ? "\x1b[92m" : "\x1b[102m";
97 case c.Pink: return fore ? "\x1b[95m" : "\x1b[105m";
98 case c.Yellow: return fore ? "\x1b[93m" : "\x1b[103m";
99 case c.Cyan: return fore ? "\x1b[96m" : "\x1b[106m";
100 default: assert(0);
101 }
102 }
103 void backend_foreColor(ConsoleColor color) {
104 colorsUsed = true;
105 backend_write(backend_getColorStr(color, true));
106 }
107 void backend_backColor(ConsoleColor color) {
108 colorsUsed = true;
109 backend_write(backend_getColorStr(color, false));
110 }
111 void backend_resetColors() {
112 backend_write("\x1b[39;49m");
113 }
114 void backend_bold(bool b) {
115 backend_write(b ? "\x1b[1m" : "\x1b[22m");
116 }
117 void backend_italic(bool b) {
118 backend_write(b ? "\x1b[3m" : "\x1b[23m");
119 }
120 void backend_underline(bool b) {
121 backend_write(b ? "\x1b[4m" : "\x1b[24m");
122 }
123 void backend_strikethrough(bool b) {
124 backend_write(b ? "\x1b[9m" : "\x1b[29m");
125 }
126 }
127