From 1361fca5d8f4ae2b12c1a5f36151d61209c1221e Mon Sep 17 00:00:00 2001 From: ponce Date: Mon, 28 Jul 2014 23:55:16 +0200 Subject: [PATCH] Partially implement coloured write/writef/... Partially implement windows terminal emulation. --- source/colorize/cwrite.d | 56 +++++++++++++++++++++++++++++++++++++++ source/colorize/package.d | 2 +- source/colorize/winterm.d | 46 +++++++++++++++++++++++++++++++- 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 source/colorize/cwrite.d 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; + } +} + + +