Mercurial > projects > dynamin
annotate dynamin/core/unix_console.d @ 106:acdbb30fee7e
Port to D2.
Most of the effort was dealing with immutable and const.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Mon, 17 Dec 2012 23:41:50 -0600 |
parents | 73060bc3f004 |
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:
77
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:
77
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:
77
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:
77
diff
changeset
|
7 * http://www.boost.org/LICENSE_1_0.txt) |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.core.unix_console; | |
12 | |
13 public import tango.stdc.stdlib; | |
14 //public import tango.stdc.string; | |
15 public import dynamin.core.string; | |
16 | |
17 /* | |
18 Can get width of console with $COLUMNS | |
19 Can get height of console with $LINES | |
20 */ | |
77
c9a4850926d9
Remove a couple not helpful comments (because moving memory does not save it) and add a helpful one.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
21 // The control codes are defined in ECMA-48. |
0 | 22 template ConsoleBackend() { |
23 static colorsUsed = false; | |
24 static ~this() { | |
25 if(colorsUsed) | |
26 backend_resetColors(); | |
27 } | |
28 bool buffered = false; | |
29 void backend_buffered(bool b) { | |
30 buffered = b; | |
31 } | |
106 | 32 void backend_write(mstring s) { |
0 | 33 /*fwrite(s.ptr, 1, s.length, stdout); |
34 if(!buffered) | |
35 fflush(stdout);*/ | |
36 } | |
106 | 37 mstring backend_readLineRaw() { |
0 | 38 /*uint size; |
39 char* line; | |
40 auto numRead = getline(&line, &size, stdin); | |
41 scope(exit) free(line); | |
106 | 42 mstring str = new char[numRead]; |
0 | 43 str[] = line[0..numRead]; |
44 return str;*/ | |
45 return null; | |
46 } | |
106 | 47 mstring backend_read() { |
0 | 48 return null; |
49 } | |
50 // TODO: use this | |
51 //termios ts; | |
52 //tcgetattr(filedes, &ts); | |
53 //ts.c_lflag |= ICANON; // turns on waiting for a whole lines | |
54 //ts.c_lflag |= ECHO; // turns on echoing | |
55 //tcsetattr(filedes, TCSAFLUSH, &ts); | |
106 | 56 mstring backend_readLineHidden() { |
0 | 57 system("stty -echo"); |
58 auto line = readLine(); | |
59 system("stty echo"); | |
60 return line; | |
61 } | |
106 | 62 mstring backend_readHidden() { |
0 | 63 return null; |
64 } | |
65 void backend_clear() { | |
66 system("clear"); | |
67 } | |
106 | 68 mstring backend_getColorStr(ConsoleColor c, bool fore) { |
69 final switch(c) { | |
0 | 70 case c.Black: return fore ? "\x1b[30m" : "\x1b[40m"; |
71 case c.Silver: return fore ? "\x1b[37m" : "\x1b[47m"; | |
72 case c.Maroon: return fore ? "\x1b[31m" : "\x1b[41m"; | |
73 case c.DarkBlue: return fore ? "\x1b[34m" : "\x1b[44m"; | |
74 case c.Green: return fore ? "\x1b[32m" : "\x1b[42m"; | |
75 case c.Purple: return fore ? "\x1b[35m" : "\x1b[45m"; | |
76 case c.DarkYellow: return fore ? "\x1b[33m" : "\x1b[43m"; | |
77 case c.Teal: return fore ? "\x1b[36m" : "\x1b[46m"; | |
78 case c.Gray: return fore ? "\x1b[90m" : "\x1b[100m"; | |
79 case c.White: return fore ? "\x1b[97m" : "\x1b[107m"; | |
80 case c.Red: return fore ? "\x1b[91m" : "\x1b[101m"; | |
81 case c.Blue: return fore ? "\x1b[94m" : "\x1b[104m"; | |
82 case c.LightGreen: return fore ? "\x1b[92m" : "\x1b[102m"; | |
83 case c.Pink: return fore ? "\x1b[95m" : "\x1b[105m"; | |
84 case c.Yellow: return fore ? "\x1b[93m" : "\x1b[103m"; | |
85 case c.Cyan: return fore ? "\x1b[96m" : "\x1b[106m"; | |
86 } | |
87 } | |
88 void backend_foreColor(ConsoleColor color) { | |
89 colorsUsed = true; | |
90 backend_write(backend_getColorStr(color, true)); | |
91 } | |
92 void backend_backColor(ConsoleColor color) { | |
93 colorsUsed = true; | |
94 backend_write(backend_getColorStr(color, false)); | |
95 } | |
96 void backend_resetColors() { | |
97 backend_write("\x1b[39;49m"); | |
98 } | |
99 void backend_bold(bool b) { | |
100 backend_write(b ? "\x1b[1m" : "\x1b[22m"); | |
101 } | |
102 void backend_italic(bool b) { | |
103 backend_write(b ? "\x1b[3m" : "\x1b[23m"); | |
104 } | |
105 void backend_underline(bool b) { | |
106 backend_write(b ? "\x1b[4m" : "\x1b[24m"); | |
107 } | |
108 void backend_strikethrough(bool b) { | |
109 backend_write(b ? "\x1b[9m" : "\x1b[29m"); | |
110 } | |
111 } | |
112 |