Partially implement coloured write/writef/...

Partially implement windows terminal emulation.
This commit is contained in:
ponce 2014-07-28 23:55:16 +02:00
parent 5caf15bd46
commit 1361fca5d8
3 changed files with 102 additions and 2 deletions

56
source/colorize/cwrite.d Normal file
View file

@ -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);
}

View file

@ -7,4 +7,4 @@
module colorize;
public import colorize.colorize;
public import colorize.winterm;
public import colorize.cwrite;

View file

@ -4,4 +4,48 @@
* License: Licensed under the MIT license. See LICENSE for more information
* Version: 0.1.0
*/
module colorize.winterm;
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;
}
}