1 /**
2  * IO related functions
3  */
4 
5 module unit_threaded.io;
6 
7 import unit_threaded.writer_thread;
8 
9 
10 private shared(bool) _debugOutput = false; ///whether or not to print debug messages
11 
12 
13 package void enableDebugOutput() {
14     synchronized {
15         _debugOutput = true;
16     }
17 }
18 
19 package bool isDebugOutputEnabled() {
20     synchronized {
21         return _debugOutput;
22     }
23 }
24 
25 void addToOutput(ref string output, in string msg) {
26     if(_debugOutput) {
27         writeln(msg);
28     } else {
29         output ~= msg;
30     }
31 }
32 
33 /**
34  * Write if debug output was enabled. Not thread-safe in the sense that it
35  * will get printed out immediately and may overlap with other output.
36  */
37 void writelnUt(T...)(T args) {
38     if(_debugOutput) WriterThread.get().writeln(args);
39 }
40 
41 /**
42  * Generates coloured output on POSIX systems
43  */
44 version(Posix) {
45     import std.stdio;
46 
47     private bool _useEscCodes;
48     private string[string] _escCodes;
49     static this() {
50         import core.sys.posix.unistd;
51         _useEscCodes = isatty(stdout.fileno()) != 0;
52         _escCodes = [ "red": "\033[31;1m",
53                       "green": "\033[32;1m",
54                       "cancel": "\033[0;;m" ];
55     }
56 
57     string green(in string msg) {
58         return _escCodes["green"] ~ msg ~ _escCodes["cancel"];
59     }
60 
61     string red(in string msg) {
62         return _escCodes["red"] ~ msg ~ _escCodes["cancel"];
63     }
64 
65 } else {
66     string green(in string msg) { return msg; }
67     string red(in string msg) { return msg; }
68 }