1
- // ReSharper disable UnusedMember.Global
1
+ // ReSharper disable UnusedMember.Global
2
2
// ReSharper disable UnusedMethodReturnValue.Global
3
3
4
4
namespace PSLogging
@@ -39,7 +39,7 @@ private HostIOInterceptor()
39
39
#endregion
40
40
41
41
#region Properties
42
-
42
+
43
43
public bool Paused
44
44
{
45
45
get { return paused ; }
@@ -152,9 +152,9 @@ public override Dictionary<string, PSObject> Prompt(string caption,
152
152
string message ,
153
153
Collection < FieldDescription > descriptions )
154
154
{
155
- if ( externalUI = = null )
155
+ if ( externalUI ! = null )
156
156
{
157
- throw new InvalidOperationException ( ) ;
157
+ throw new InvalidOperationException ( "Unable to prompt user in headless session" ) ;
158
158
}
159
159
160
160
Dictionary < string , PSObject > result = externalUI . Prompt ( caption , message , descriptions ) ;
@@ -169,9 +169,9 @@ public override int PromptForChoice(string caption,
169
169
Collection < ChoiceDescription > choices ,
170
170
int defaultChoice )
171
171
{
172
- if ( externalUI = = null )
172
+ if ( externalUI ! = null )
173
173
{
174
- throw new InvalidOperationException ( ) ;
174
+ throw new InvalidOperationException ( "Unable to prompt user for choice in headless session" ) ;
175
175
}
176
176
177
177
int result = externalUI . PromptForChoice ( caption , message , choices , defaultChoice ) ;
@@ -186,11 +186,11 @@ public override PSCredential PromptForCredential(string caption,
186
186
string userName ,
187
187
string targetName )
188
188
{
189
- if ( externalUI = = null )
189
+ if ( externalUI ! = null )
190
190
{
191
- throw new InvalidOperationException ( ) ;
191
+ throw new InvalidOperationException ( "Unable to prompt user for credential in headless session" ) ;
192
192
}
193
-
193
+
194
194
PSCredential result = externalUI . PromptForCredential ( caption , message , userName , targetName ) ;
195
195
196
196
SendToSubscribers ( s => s . CredentialPrompt ( result ) ) ;
@@ -205,9 +205,9 @@ public override PSCredential PromptForCredential(string caption,
205
205
PSCredentialTypes allowedCredentialTypes ,
206
206
PSCredentialUIOptions options )
207
207
{
208
- if ( externalUI = = null )
208
+ if ( externalUI ! = null )
209
209
{
210
- throw new InvalidOperationException ( ) ;
210
+ throw new InvalidOperationException ( "Unable to prompt user for credential in headless session" ) ;
211
211
}
212
212
213
213
PSCredential result = externalUI . PromptForCredential ( caption ,
@@ -224,9 +224,9 @@ public override PSCredential PromptForCredential(string caption,
224
224
225
225
public override string ReadLine ( )
226
226
{
227
- if ( externalUI = = null )
227
+ if ( externalUI ! = null )
228
228
{
229
- throw new InvalidOperationException ( ) ;
229
+ throw new InvalidOperationException ( "Unable to ReadLine from host in headless session" ) ;
230
230
}
231
231
232
232
string result = externalUI . ReadLine ( ) ;
@@ -238,11 +238,12 @@ public override string ReadLine()
238
238
239
239
public override SecureString ReadLineAsSecureString ( )
240
240
{
241
- if ( externalUI = = null )
241
+ if ( externalUI ! = null )
242
242
{
243
- throw new InvalidOperationException ( ) ;
243
+ throw new InvalidOperationException ( "Unable to ReadLineAsSecureString from host in headless session" ) ;
244
244
}
245
245
246
+
246
247
return externalUI . ReadLineAsSecureString ( ) ;
247
248
}
248
249
@@ -271,13 +272,11 @@ public void RemoveSubscriber(IHostIOSubscriber subscriber)
271
272
272
273
public override void Write ( string value )
273
274
{
274
- if ( externalUI = = null )
275
+ if ( externalUI ! = null )
275
276
{
276
- throw new InvalidOperationException ( ) ;
277
+ externalUI . Write ( value ) ;
277
278
}
278
279
279
- externalUI . Write ( value ) ;
280
-
281
280
if ( ! paused )
282
281
{
283
282
writeCache . Append ( value ) ;
@@ -286,13 +285,11 @@ public override void Write(string value)
286
285
287
286
public override void Write ( ConsoleColor foregroundColor , ConsoleColor backgroundColor , string value )
288
287
{
289
- if ( externalUI = = null )
288
+ if ( externalUI ! = null )
290
289
{
291
- throw new InvalidOperationException ( ) ;
290
+ externalUI . Write ( foregroundColor , backgroundColor , value ) ;
292
291
}
293
292
294
- externalUI . Write ( foregroundColor , backgroundColor , value ) ;
295
-
296
293
if ( ! paused )
297
294
{
298
295
writeCache . Append ( value ) ;
@@ -301,44 +298,40 @@ public override void Write(ConsoleColor foregroundColor, ConsoleColor background
301
298
302
299
public override void WriteDebugLine ( string message )
303
300
{
304
- if ( externalUI == null )
305
- {
306
- throw new InvalidOperationException ( ) ;
307
- }
308
-
309
301
string [ ] lines = message . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
310
302
foreach ( string line in lines )
311
303
{
312
304
string temp = line ;
313
305
SendToSubscribers ( s => s . WriteDebug ( temp . TrimEnd ( ) + "\r \n " ) ) ;
314
306
}
315
307
316
- externalUI . WriteDebugLine ( message ) ;
308
+ if ( externalUI != null )
309
+ {
310
+ externalUI . WriteDebugLine ( message ) ;
311
+ }
317
312
}
318
313
319
314
public override void WriteErrorLine ( string message )
320
315
{
321
- if ( externalUI == null )
322
- {
323
- throw new InvalidOperationException ( ) ;
324
- }
325
-
326
316
string [ ] lines = message . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
327
317
foreach ( string line in lines )
328
318
{
329
319
string temp = line ;
330
320
SendToSubscribers ( s => s . WriteError ( temp . TrimEnd ( ) + "\r \n " ) ) ;
331
321
}
332
322
333
- externalUI . WriteErrorLine ( message ) ;
323
+ if ( externalUI != null )
324
+ {
325
+ externalUI . WriteErrorLine ( message ) ;
326
+ }
334
327
}
335
328
336
329
public override void WriteLine ( )
337
330
{
338
- if ( externalUI == null )
339
- {
340
- throw new InvalidOperationException ( ) ;
341
- }
331
+ // if (externalUI == null)
332
+ // {
333
+ // throw new InvalidOperationException();
334
+ // }
342
335
343
336
string [ ] lines = writeCache . ToString ( ) . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
344
337
foreach ( string line in lines )
@@ -348,15 +341,17 @@ public override void WriteLine()
348
341
}
349
342
350
343
writeCache . Length = 0 ;
351
- externalUI . WriteLine ( ) ;
344
+ if ( externalUI != null ) {
345
+ externalUI . WriteLine ( ) ;
346
+ }
352
347
}
353
348
354
349
public override void WriteLine ( string value )
355
350
{
356
- if ( externalUI == null )
357
- {
358
- throw new InvalidOperationException ( ) ;
359
- }
351
+ // if (externalUI == null)
352
+ // {
353
+ // throw new InvalidOperationException();
354
+ // }
360
355
361
356
string [ ] lines = ( writeCache + value ) . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
362
357
foreach ( string line in lines )
@@ -366,15 +361,17 @@ public override void WriteLine(string value)
366
361
}
367
362
368
363
writeCache . Length = 0 ;
369
- externalUI . WriteLine ( value ) ;
364
+ if ( externalUI != null ) {
365
+ externalUI . WriteLine ( value ) ;
366
+ }
370
367
}
371
368
372
369
public override void WriteLine ( ConsoleColor foregroundColor , ConsoleColor backgroundColor , string value )
373
370
{
374
- if ( externalUI == null )
375
- {
376
- throw new InvalidOperationException ( ) ;
377
- }
371
+ // if (externalUI == null)
372
+ // {
373
+ // throw new InvalidOperationException();
374
+ // }
378
375
379
376
string [ ] lines = ( writeCache + value ) . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
380
377
foreach ( string line in lines )
@@ -384,53 +381,49 @@ public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgr
384
381
}
385
382
386
383
writeCache . Length = 0 ;
387
- externalUI . WriteLine ( foregroundColor , backgroundColor , value ) ;
384
+ if ( externalUI != null ) {
385
+ externalUI . WriteLine ( foregroundColor , backgroundColor , value ) ;
386
+ }
388
387
}
389
388
390
389
public override void WriteProgress ( long sourceId , ProgressRecord record )
391
390
{
392
- if ( externalUI == null )
393
- {
394
- throw new InvalidOperationException ( ) ;
395
- }
396
-
397
391
SendToSubscribers ( s => s . WriteProgress ( sourceId , record ) ) ;
398
392
399
- externalUI . WriteProgress ( sourceId , record ) ;
393
+ if ( externalUI != null )
394
+ {
395
+ externalUI . WriteProgress ( sourceId , record ) ;
396
+ }
400
397
}
401
398
402
399
public override void WriteVerboseLine ( string message )
403
400
{
404
- if ( externalUI == null )
405
- {
406
- throw new InvalidOperationException ( ) ;
407
- }
408
-
409
401
string [ ] lines = message . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
410
402
foreach ( string line in lines )
411
403
{
412
404
string temp = line ;
413
405
SendToSubscribers ( s => s . WriteVerbose ( temp . TrimEnd ( ) + "\r \n " ) ) ;
414
406
}
415
407
416
- externalUI . WriteVerboseLine ( message ) ;
408
+ if ( externalUI != null )
409
+ {
410
+ externalUI . WriteVerboseLine ( message ) ;
411
+ }
417
412
}
418
413
419
414
public override void WriteWarningLine ( string message )
420
415
{
421
- if ( externalUI == null )
422
- {
423
- throw new InvalidOperationException ( ) ;
424
- }
425
-
426
416
string [ ] lines = message . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
427
417
foreach ( string line in lines )
428
418
{
429
419
string temp = line ;
430
420
SendToSubscribers ( s => s . WriteWarning ( temp . TrimEnd ( ) + "\r \n " ) ) ;
431
421
}
432
422
433
- externalUI . WriteWarningLine ( message ) ;
423
+ if ( externalUI != null )
424
+ {
425
+ externalUI . WriteWarningLine ( message ) ;
426
+ }
434
427
}
435
428
436
429
#endregion
0 commit comments