Created
March 12, 2012 23:46
-
-
Save anonymous/2025473 to your computer and use it in GitHub Desktop.
dragonov's IO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Console | |
* | |
* Little extension for input/ouput through console. | |
* | |
* Authors: $(WEB dzfl.pl, Damian "nazriel" Ziemba) | |
* Copyright: 2012, Damian Ziemba | |
* License: BSD | |
* | |
* Example: | |
* ---- | |
* | |
* import dragonov.io.Console; | |
* | |
* Console.FgColor = Console.Color.Red; | |
* Console.BgColor = Console.Color.Blue; | |
* Console("Hi! I will write those message with ") | |
* ("Blue background and Red foreground until ") | |
* ("you decide otherwise").Nl(); | |
* | |
* Console.FgColor = Console.Color.Default; | |
* Console.BgColor = Console.Color.Default; | |
* Console("Back to old grey world! :( Press ANY key") | |
* ("To Continue").Nl(); | |
* Console.ReadKey(); | |
* | |
* ---- | |
*/ | |
module dragonov.io.Console; | |
static import std.stdio; | |
import std.conv: to; | |
version(Posix) | |
{ | |
import core.sys.posix.termios; | |
import core.stdc.stdio; | |
} | |
else version(Windows) | |
{ | |
import core.sys.windows.windows; | |
extern (C) int _getch(); | |
} | |
struct IOConsole | |
{ | |
version(Windows) | |
{ | |
public enum Font | |
{ | |
Normal = 0, | |
Bold = 0, | |
Underline = 0x8000, | |
Flash = 0, | |
Reverse = 0x4000, | |
} | |
public enum Color | |
{ | |
Default = 0x0000, | |
Blue = 0x0001, | |
Green = 0x0002, | |
Aqua = 0x0003, | |
Red = 0x0004, | |
Purple= 0x0005, | |
Yellow= 0x0006, | |
Gray = 0x0008, | |
LightBlue = 0x0009, | |
LightGreen = 0x000A, | |
LightAqua = 0x000B, | |
LightRed = 0x000C, | |
LightPurple= 0x000D, | |
} | |
HANDLE _consoleHandle; | |
static this() | |
{ | |
_consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); | |
} | |
} | |
else | |
{ | |
public enum Color | |
{ | |
Default = 0, | |
Blue = 34, | |
Green = 32, | |
Aqua = 36, | |
Red = 31, | |
Purple= 35, | |
Yellow= 93, | |
Gray = 37, | |
LightBlue = 94, | |
LightGreen = 92, | |
LightAqua = 96, | |
LightRed = 91, | |
LightPurple= 95, | |
} | |
public enum Font | |
{ | |
Normal = 0, | |
Bold = 1, | |
Underline = 4, | |
Flash = 5, | |
Reverse = 7, | |
} | |
} | |
private int _backgroundColor = Color.Default; | |
private int _foregroundColor = Color.Default; | |
private Font _fontStyle = Font.Normal; | |
alias Write opCall; | |
IOConsole Format(T...)(string format, T args) | |
{ // TODO: add colors&etc support | |
std.stdio.writef(format, args); | |
return this; | |
} | |
IOConsole Formatln(T...)(string format, T args) | |
{ // TODO: add colors&etc support | |
std.stdio.writefln(format, args); | |
return this; | |
} | |
IOConsole Nl() | |
{ | |
std.stdio.writeln(); | |
return this; | |
} | |
alias Nl Newline; | |
IOConsole Write(T...)(T args) | |
{ | |
version (Posix) | |
{ | |
string output; | |
if (_fontStyle != Font.Normal || _foregroundColor != Color.Default || _backgroundColor != Color.Default) | |
{ | |
output ~= "\033["; | |
if (_fontStyle != Font.Normal) | |
{ | |
output ~= to!string(_fontStyle) ~ ";"; | |
} | |
if (_foregroundColor != Color.Default) | |
{ | |
output ~= to!string(_foregroundColor) ~ ";"; | |
} | |
if (_backgroundColor != Color.Default) | |
{ | |
output ~= to!string(_backgroundColor) ~ ";"; | |
} | |
output = output[0..$-1] ~ "m"; | |
std.stdio.write(output, args, "\033[0m"); | |
} | |
else | |
std.stdio.write(args); | |
} | |
else version(Windows) | |
{ | |
int consoleColor; | |
if (_fontStyle != Font.Normal || _foregroundColor != Color.Default || _backgroundColor != Color.Default) | |
{ | |
if (_foregroundColor != Color.Default) | |
{ | |
consoleColor += _foregroundColor; | |
} | |
if (_backgroundColor != Color.Default) | |
{ | |
consoleColor += _backgroundColor * 16; | |
} | |
SetConsoleTextAttribute(_consoleHandle, consoleColor); | |
} | |
std.stdio.write(args); | |
} | |
else | |
static assert(0, "Not supported platform"); | |
return this; | |
} | |
string ReadLine() | |
{ | |
import std.exception; | |
auto buff = std.stdio.readln(); | |
return assumeUnique(buff); | |
} | |
char ReadKey() | |
{ | |
version(Posix) | |
{ | |
int getch() | |
{ | |
int ch; | |
termios oldt; | |
termios newt; | |
tcgetattr(0, &oldt); | |
newt = oldt; | |
newt.c_lflag &= ~(ICANON | ECHO); | |
tcsetattr(0, TCSANOW, &newt); | |
ch = getchar(); | |
tcsetattr(0, TCSANOW, &oldt); | |
return ch; | |
} | |
} | |
else version(Windows) | |
{ | |
alias _getch getch; | |
} | |
return cast(char) getch(); | |
} | |
@property void BgColor(Color color) | |
{ | |
_backgroundColor = color + 10; | |
} | |
@property Color BgColor() | |
{ | |
return cast(Color) (_backgroundColor - 10); | |
} | |
@property void FgColor(Color color) | |
{ | |
_foregroundColor = color; | |
} | |
@property Color FgColor() | |
{ | |
return cast(Color)_foregroundColor; | |
} | |
@property void FontStyle(Font fontStyle) | |
{ | |
_fontStyle = fontStyle; | |
} | |
@property Font FontStyle() | |
{ | |
return _fontStyle; | |
} | |
} | |
static __gshared IOConsole Console; | |
alias Console console; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment