1 /** 2 * Defines the static Output class, which handles all output to the console window. 3 */ 4 module utility.output; 5 import utility.config; 6 import std.stdio; 7 8 /** 9 * The types of output. 10 */ 11 enum OutputType 12 { 13 /// Purely informational. 14 Info, 15 /// Something went wrong, but it's recoverable. 16 Warning, 17 /// The ship is sinking. 18 Error 19 } 20 21 /** 22 * The levels of output available. 23 */ 24 enum Verbosity 25 { 26 /// Show me everything. 27 High, 28 /// Show me just warnings and errors. 29 Medium, 30 /// I only care about things gone horribly wrong. 31 Low, 32 /// I like to live on the edge. 33 Off 34 } 35 36 /// Alias for Output.printMessage 37 alias Output.printMessage log; 38 /// Alias for Output.printValue 39 alias Output.printValue logValue; 40 41 /** 42 * Static class for handling interactions with the console. 43 */ 44 final abstract class Output 45 { 46 public static: 47 /** 48 * Initialize the controller. 49 */ 50 final void initialize() 51 { 52 verbosity = Config.get!Verbosity( "Game.Verbosity" ); 53 } 54 55 /** 56 * Print a generic message to the console. 57 */ 58 final void printMessage( A... )( OutputType type, A messages ) 59 { 60 if( shouldPrint( type ) ) 61 { 62 write( getHeader( type ) ); 63 64 foreach( msg; messages ) 65 write( msg ); 66 67 writeln(); 68 } 69 } 70 71 /** 72 * Print the value of a variable. 73 */ 74 final void printValue( T )( OutputType type, string varName, T value ) 75 { 76 if( shouldPrint( type ) ) 77 writefln( "%s %s: %s", getHeader( type ), varName, value ); 78 } 79 80 private: 81 /** 82 * Caches the verbosity set in the config. 83 */ 84 Verbosity verbosity; 85 86 /** 87 * Gets the header for the given output type. 88 */ 89 final string getHeader( OutputType type ) 90 { 91 switch( type ) 92 { 93 case OutputType.Info: 94 //SetConsoleTextAttribute( hConsole, 15 ); 95 return "[INFO] "; 96 case OutputType.Warning: 97 //SetConsoleTextAttribute( hConsole, 14 ); 98 return "[WARNING]"; 99 case OutputType.Error: 100 //SetConsoleTextAttribute( hConsole, 12 ); 101 return "[ERROR] "; 102 default: 103 return " "; 104 } 105 } 106 107 final bool shouldPrint( OutputType type ) 108 { 109 return type >= verbosity; 110 } 111 }