From 8ff025230e7e25b3bb1566844c2482a7fa01df90 Mon Sep 17 00:00:00 2001 From: ponce Date: Tue, 29 Jul 2014 23:36:46 +0200 Subject: [PATCH] fg and bg colours working. There was a problem with changing colors before stdout was ever flushed. --- source/colorize/cwrite.d | 21 +++++++++++++++++---- source/colorize/winterm.d | 26 +++++++++++++++++--------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/source/colorize/cwrite.d b/source/colorize/cwrite.d index 5ccfe36..cd4e7fb 100644 --- a/source/colorize/cwrite.d +++ b/source/colorize/cwrite.d @@ -53,11 +53,24 @@ void cwrite(S...)(File f, S args) version(Windows) { - auto winterm = WinTermEmulation(); - foreach(c ; s) + WinTermEmulation winterm; + winterm.initialize(); + foreach(dchar c ; s) { - if (winterm.feed(c)) - f.write(c); + auto charAction = winterm.feed(c); + final switch(charAction) with (WinTermEmulation.CharAction) + { + case drop: + break; + + case write: + f.write(c); + break; + + case flush: + f.flush(); + break; + } } } else diff --git a/source/colorize/winterm.d b/source/colorize/winterm.d index a2c7479..7406d1a 100644 --- a/source/colorize/winterm.d +++ b/source/colorize/winterm.d @@ -17,7 +17,7 @@ version(Windows) struct WinTermEmulation { public: - this(bool workardound = true) nothrow @nogc + void initialize() nothrow @nogc { // saves console attributes _console = GetStdHandle(STD_OUTPUT_HANDLE); @@ -36,8 +36,16 @@ version(Windows) } } - // Eat one character and update color state accordingly. Return true if this character should be displayed. - bool feed(dchar d) nothrow @nogc + enum CharAction + { + write, + drop, + flush + } + + // Eat one character and update color state accordingly. + // What to do with the fed character. + CharAction feed(dchar d) nothrow @nogc { final switch(_state) with (State) { @@ -45,7 +53,7 @@ version(Windows) if (d == '\x1B') { _state = escaped; - return false; + return CharAction.flush; } break; @@ -54,7 +62,7 @@ version(Windows) { _state = readingAttribute; _parsedAttr = 0; - return false; + return CharAction.drop; } break; @@ -63,23 +71,23 @@ version(Windows) if (d >= '0' && d <= '9') { _parsedAttr = _parsedAttr * 10 + (d - '0'); - return false; + return CharAction.drop; } else if (d == ';') { executeAttribute(_parsedAttr); _parsedAttr = 0; - return false; + return CharAction.drop; } else if (d == 'm') { executeAttribute(_parsedAttr); _state = State.initial; - return false; + return CharAction.drop; } break; } - return true; + return CharAction.write; } private: