diff --git a/source/colorize/cwrite.d b/source/colorize/cwrite.d new file mode 100644 index 0000000..5550a29 --- /dev/null +++ b/source/colorize/cwrite.d @@ -0,0 +1,56 @@ +/** + * Authors: ponce + * Date: July 28, 2014 + * License: Licensed under the MIT license. See LICENSE for more information + * Version: 0.1.0 + */ +module colorize.cwrite; + +import std.stdio : File, stdout; + +import colorize.winterm; + +/// Coloured write. +void cwrite(T...)(T args) if (!is(T[0] : File)) +{ + stdout.cwrite(args); +} + +/// Coloured writef. +void cwritef(T...)(T args) +{ + stdout.cwritef(args); +} + +/// Coloured writefln. +void cwritefln(T...)(T args) +{ + stdout.cwritefln(args); +} + +/// Coloured writeln. +void cwriteln(T...)(T args) +{ + // Most general instance + stdout.cwrite(args, '\n'); +} + +/// Coloured writef to a File. +void cwritef(File f, Char, A...)(in Char[] fmt, A args) +{ + auto s = format(fmt, args); + f.write(s); // TODO support colours +} + +/// Coloured writef to a File. +void cwrite(S...)(File f, S args) +{ + import std.conv : to; + + string s = ""; + foreach(arg; args) + s ~= to!string(arg); + + f.write(s); +} + diff --git a/source/colorize/package.d b/source/colorize/package.d index 0669be1..c4c07f5 100644 --- a/source/colorize/package.d +++ b/source/colorize/package.d @@ -7,4 +7,4 @@ module colorize; public import colorize.colorize; -public import colorize.winterm; \ No newline at end of file +public import colorize.cwrite; \ No newline at end of file diff --git a/source/colorize/winterm.d b/source/colorize/winterm.d index 93030d7..c0d76aa 100644 --- a/source/colorize/winterm.d +++ b/source/colorize/winterm.d @@ -4,4 +4,48 @@ * License: Licensed under the MIT license. See LICENSE for more information * Version: 0.1.0 */ -module colorize.winterm; \ No newline at end of file +module colorize.winterm; + + +version(Windows) +{ + import core.sys.windows.windows; + + // This is a state machine to enable terminal colors on Windows. + // Parses and interpret ANSI/VT100 Terminal Control Escape Sequences. + // Only supports fg and bg colors. + struct WinTermEmulation + { + public: + this(bool workardound = true) nothrow + { + _console = GetStdHandle(STD_OUTPUT_HANDLE); + + // saves console attributes + _savedInitialColor = (0 != GetConsoleScreenBufferInfo(_console, &consoleInfo)); + + } + + ~this() + { + if (_savedInitialColor) + { + SetConsoleTextAttribute(_console, consoleInfo.wAttributes); + _savedInitialColor = false; + } + } + + void feed(dchar d) nothrow + { + + } + + private: + HANDLE _console; + bool _savedInitialColor; + CONSOLE_SCREEN_BUFFER_INFO consoleInfo; + } +} + + +