22using System . Collections . Generic ;
33using System . IO ;
44using System . Threading ;
5+ using System . Threading . Tasks ;
56
67namespace InEngine . Core . IO ;
78
89public class Write : IConsoleWrite
910{
10- static readonly Mutex consoleOutputLock = new Mutex ( ) ;
11- static readonly Mutex fileOutputLock = new Mutex ( ) ;
11+ private static readonly Mutex ConsoleOutputLock = new ( ) ;
12+ public static readonly Mutex FileOutputLock = new ( ) ;
1213
1314 public ConsoleColor InfoColor { get ; set ; } = ConsoleColor . Green ;
1415 public ConsoleColor WarningColor { get ; set ; } = ConsoleColor . Yellow ;
1516 public ConsoleColor ErrorColor { get ; set ; } = ConsoleColor . Red ;
1617 public ConsoleColor LineColor { get ; set ; } = ConsoleColor . White ;
17- public List < string > Buffer { get ; set ; } = new List < string > ( ) ;
18+ public List < string > Buffer { get ; set ; } = new ( ) ;
1819 public bool IsBufferEnabled { get ; set ; }
1920
2021 public Write ( ) : this ( true )
2122 {
2223 }
2324
24- public Write ( bool isBufferEnabled )
25- {
26- IsBufferEnabled = isBufferEnabled ;
27- }
25+ public Write ( bool isBufferEnabled ) => IsBufferEnabled = isBufferEnabled ;
26+
27+ #region Sync Methods
2828
2929 public IConsoleWrite Newline ( int count = 1 )
3030 {
@@ -33,77 +33,90 @@ public IConsoleWrite Newline(int count = 1)
3333 return this ;
3434 }
3535
36- public IConsoleWrite Info ( object val )
37- {
38- return ColoredLine ( val , InfoColor ) ;
39- }
36+ public IConsoleWrite Info ( object val ) => LineWithColor ( val , InfoColor ) ;
37+ public IConsoleWrite Error ( object val ) => LineWithColor ( val , ErrorColor ) ;
38+ public IConsoleWrite Warning ( object val ) => LineWithColor ( val , WarningColor ) ;
39+ public IConsoleWrite Line ( object val ) => LineWithColor ( val , LineColor ) ;
4040
41- public IConsoleWrite Error ( object val )
41+ public IConsoleWrite LineWithColor ( object val , ConsoleColor consoleColor )
4242 {
43- return ColoredLine ( val , ErrorColor ) ;
43+ TextWithColor ( val , consoleColor , true ) ;
44+ return this ;
4445 }
4546
46- public IConsoleWrite Warning ( object val )
47- {
48- return ColoredLine ( val , WarningColor ) ;
49- }
47+ public IConsoleWrite InfoText ( object val ) => TextWithColor ( val , InfoColor ) ;
48+ public IConsoleWrite ErrorText ( object val ) => TextWithColor ( val , ErrorColor ) ;
49+ public IConsoleWrite WarningText ( object val ) => TextWithColor ( val , WarningColor ) ;
50+ public IConsoleWrite Text ( object val ) => TextWithColor ( val , LineColor ) ;
5051
51- public IConsoleWrite Line ( object val )
52+ public IConsoleWrite TextWithColor ( object val , ConsoleColor consoleColor , bool writeLine = false )
5253 {
53- return ColoredLine ( val , LineColor ) ;
54- }
54+ var text = BeginWriting ( val , consoleColor ) ;
5555
56- public IConsoleWrite ColoredLine ( object val , ConsoleColor consoleColor )
57- {
58- WriteColoredLineOrText ( val , consoleColor , true ) ;
56+ if ( writeLine )
57+ Console . WriteLine ( val ) ;
58+ else
59+ Console . Write ( val ) ;
60+
61+ EndWriting ( text , writeLine ) ;
5962 return this ;
6063 }
6164
62- public IConsoleWrite InfoText ( object val )
63- {
64- return ColoredText ( val , InfoColor ) ;
65- }
65+ #endregion
6666
67- public IConsoleWrite ErrorText ( object val )
68- {
69- return ColoredText ( val , ErrorColor ) ;
70- }
67+ #region Async Methods
7168
72- public IConsoleWrite WarningText ( object val )
69+ public async Task NewlineAsync ( int count = 1 )
7370 {
74- return ColoredText ( val , WarningColor ) ;
71+ for ( var i = 0 ; i < count ; i ++ )
72+ await Console . Out . WriteLineAsync ( ) ;
7573 }
7674
77- public IConsoleWrite Text ( object val )
75+ public async Task InfoAsync ( object val ) => await LineWithColorAsync ( val , InfoColor ) ;
76+ public async Task ErrorAsync ( object val ) => await LineWithColorAsync ( val , ErrorColor ) ;
77+ public async Task WarningAsync ( object val ) => await LineWithColorAsync ( val , WarningColor ) ;
78+ public async Task LineAsync ( object val ) => await LineWithColorAsync ( val , LineColor ) ;
79+
80+ public async Task LineWithColorAsync ( object val , ConsoleColor consoleColor ) =>
81+ await TextWithColorAsync ( val , consoleColor , true ) ;
82+
83+ public async Task InfoTextAsync ( object val ) => await TextWithColorAsync ( val , InfoColor ) ;
84+ public async Task ErrorTextAsync ( object val ) => await TextWithColorAsync ( val , ErrorColor ) ;
85+ public async Task WarningTextAsync ( object val ) => await TextWithColorAsync ( val , WarningColor ) ;
86+ public async Task TextAsync ( object val ) => await TextWithColorAsync ( val , LineColor ) ;
87+
88+ public async Task TextWithColorAsync ( object val , ConsoleColor consoleColor , bool writeLine = false )
7889 {
79- return ColoredText ( val , LineColor ) ;
90+ var text = BeginWriting ( val , consoleColor ) ;
91+
92+ if ( writeLine )
93+ await Console . Out . WriteLineAsync ( text ) ;
94+ else
95+ await Console . Out . WriteAsync ( text ) ;
96+
97+ EndWriting ( text , writeLine ) ;
8098 }
8199
82- public IConsoleWrite ColoredText ( object val , ConsoleColor consoleColor )
100+ #endregion
101+
102+ protected string BeginWriting ( object val , ConsoleColor consoleColor )
83103 {
84- WriteColoredLineOrText ( val , consoleColor , false ) ;
85- return this ;
104+ ConsoleOutputLock . WaitOne ( ) ;
105+ Console . ForegroundColor = consoleColor ;
106+ return val ? . ToString ( ) ?? string . Empty ;
86107 }
87108
88- void WriteColoredLineOrText ( object val , ConsoleColor consoleColor , bool writeLine )
109+ protected void EndWriting ( string text , bool writeLine )
89110 {
90- if ( val == null )
91- val = String . Empty ;
92- consoleOutputLock . WaitOne ( ) ;
93- Console . ForegroundColor = consoleColor ;
94- if ( writeLine )
95- Console . WriteLine ( val ) ;
96- else
97- Console . Write ( val ) ;
98111 Console . ResetColor ( ) ;
99112 if ( IsBufferEnabled )
100113 {
101- Buffer . Add ( val . ToString ( ) ) ;
114+ Buffer . Add ( text ) ;
102115 if ( writeLine )
103116 Buffer . Add ( Environment . NewLine ) ;
104117 }
105118
106- consoleOutputLock . ReleaseMutex ( ) ;
119+ ConsoleOutputLock . ReleaseMutex ( ) ;
107120 }
108121
109122 public string FlushBuffer ( )
@@ -115,12 +128,12 @@ public string FlushBuffer()
115128
116129 public void ToFile ( string path , string text , bool shouldAppend = false )
117130 {
118- fileOutputLock . WaitOne ( ) ;
131+ FileOutputLock . WaitOne ( ) ;
119132 if ( shouldAppend )
120133 File . AppendAllText ( path , text ) ;
121134 else
122135 File . WriteAllText ( path , text ) ;
123136
124- fileOutputLock . ReleaseMutex ( ) ;
137+ FileOutputLock . ReleaseMutex ( ) ;
125138 }
126139}
0 commit comments