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         import std.stdio;
28         writeln(msg);
29     } else {
30         output ~= msg;
31     }
32 }
33 
34 /**
35  * Write if debug output was enabled. Not thread-safe in the sense that it
36  * will get printed out immediately and may overlap with other output.
37  */
38 void writelnUt(T...)(T args) {
39     if(_debugOutput) WriterThread.get().writeln(args);
40 }
41 
42 /**
43  * Generates coloured output on POSIX systems
44  */
45 version(Posix) {
46     import std.stdio;
47 
48     private bool _useEscCodes;
49     private string[string] _escCodes;
50     static this() {
51         import core.sys.posix.unistd;
52         _useEscCodes = isatty(stdout.fileno()) != 0;
53         _escCodes = [ "red": "\033[31;1m",
54                       "green": "\033[32;1m",
55                       "cancel": "\033[0;;m" ];
56     }
57 
58     string green(in string msg) {
59         return _escCodes["green"] ~ msg ~ _escCodes["cancel"];
60     }
61 
62     string red(in string msg) {
63         return _escCodes["red"] ~ msg ~ _escCodes["cancel"];
64     }
65 
66 } else {
67     string green(in string msg) pure nothrow { return msg; }
68     string red(in string msg) pure nothrow { return msg; }
69 }