1 /**
2  * Authors: ponce
3  * Date: July 28, 2014
4  * License: Licensed under the MIT license. See LICENSE for more information
5  * Version: 1.0.2
6  */
7 module colorize.cwrite;
8 
9 import std.stdio : File, stdout;
10 
11 import colorize.winterm;
12 
13 /// Coloured write.
14 void cwrite(T...)(T args) if (!is(T[0] : File))
15 {
16   stdout.cwrite(args);
17 }
18 
19 /// Coloured writef.
20 void cwritef(Char, T...)(in Char[] fmt, T args) if (!is(T[0] : File))
21 {
22   stdout.cwritef(fmt, args);
23 }
24 
25 /// Coloured writefln.
26 void cwritefln(Char, T...)(in Char[] fmt, T args)
27 {
28   stdout.cwritef(fmt ~ "\n", args);
29 }
30 
31 /// Coloured writeln.
32 void cwriteln(T...)(T args)
33 {
34   // Most general instance
35   stdout.cwrite(args, '\n');
36 }
37 
38 /// Coloured writef to a File.
39 void cwritef(Char, A...)(File f, in Char[] fmt, A args)
40 {
41   import std..string : format;
42   auto s = format(fmt, args);
43   f.cwrite(s);
44 }
45 
46 /// Coloured writef to a File.
47 void cwrite(S...)(File f, S args)
48 {
49   import std.conv : to;
50 
51   string s = "";
52   foreach(arg; args)
53     s ~= to!string(arg);
54 
55   version(Windows)
56   {
57     WinTermEmulation winterm;
58     winterm.initialize();
59     foreach(dchar c ; s)
60     {
61       auto charAction = winterm.feed(c);
62       final switch(charAction) with (WinTermEmulation.CharAction)
63       {
64         case drop: break;
65         case write: f.write(c); break;
66         case flush: f.flush(); break;
67       }
68     }
69   }
70   else
71   {
72     f.write(s);
73   }
74 }
75