mirror of
https://github.com/yamadapc/d-colorize
synced 2024-11-14 21:14:00 +01:00
Partially implement coloured write/writef/...
Partially implement windows terminal emulation.
This commit is contained in:
parent
5caf15bd46
commit
1361fca5d8
56
source/colorize/cwrite.d
Normal file
56
source/colorize/cwrite.d
Normal 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);
|
||||
}
|
||||
|
|
@ -7,4 +7,4 @@
|
|||
module colorize;
|
||||
|
||||
public import colorize.colorize;
|
||||
public import colorize.winterm;
|
||||
public import colorize.cwrite;
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue