3
3
using System . Collections . ObjectModel ;
4
4
using System . ComponentModel ;
5
5
using System . Diagnostics ;
6
+ using System . Diagnostics . CodeAnalysis ;
6
7
using System . Globalization ;
7
8
using System . Management . Automation ;
8
9
using System . Management . Automation . Host ;
@@ -182,11 +183,12 @@ public void Prompt(string prompt)
182
183
{
183
184
if ( Next != null )
184
185
{
185
- Run insert = new Run ( prompt ) ;
186
- insert . Background = Background ;
187
- insert . Foreground = Foreground ;
186
+ var insert = new Run ( prompt )
187
+ {
188
+ Background = Background ,
189
+ Foreground = Foreground
190
+ } ;
188
191
Next . Inlines . Add ( insert ) ;
189
-
190
192
SetPrompt ( ) ;
191
193
}
192
194
} ) ;
@@ -237,14 +239,7 @@ private void ShouldEcho(char ch, bool echo)
237
239
if ( ch == cmd [ 0 ] )
238
240
{
239
241
// emulate NoEcho by UN-echoing...
240
- if ( cmd . Length > 1 )
241
- {
242
- CurrentCommand = cmd . Substring ( 1 ) ;
243
- }
244
- else
245
- {
246
- CurrentCommand = "" ;
247
- }
242
+ CurrentCommand = cmd . Length > 1 ? cmd . Substring ( 1 ) : "" ;
248
243
// if we're NOT NoEcho, then re-echo it:
249
244
if ( echo )
250
245
{
@@ -354,17 +349,12 @@ public ConsoleColor BackgroundColor
354
349
private static void BackgroundColorPropertyChanged ( DependencyObject depObj , DependencyPropertyChangedEventArgs e )
355
350
{
356
351
// put code here to handle the property changed for BackgroundColor
357
- ConsoleControl consoleControlObj = depObj as ConsoleControl ;
352
+ var consoleControlObj = depObj as ConsoleControl ;
358
353
if ( consoleControlObj != null )
359
354
{
360
- if ( e . NewValue != DependencyProperty . UnsetValue )
361
- {
362
- consoleControlObj . Background = consoleControlObj . _brushes . BrushFromConsoleColor ( ( ConsoleColor ) e . NewValue ) ;
363
- }
364
- else
365
- {
366
- consoleControlObj . Background = consoleControlObj . _brushes . DefaultBackground ;
367
- }
355
+ consoleControlObj . Background = e . NewValue != DependencyProperty . UnsetValue
356
+ ? consoleControlObj . _brushes . BrushFromConsoleColor ( ( ConsoleColor ) e . NewValue )
357
+ : consoleControlObj . _brushes . DefaultBackground ;
368
358
}
369
359
}
370
360
@@ -394,17 +384,12 @@ private static void ForegroundColorPropertyChanged(DependencyObject depObj, Depe
394
384
{
395
385
396
386
// put code here to handle the property changed for ForegroundColor
397
- ConsoleControl ConsoleControlObj = depObj as ConsoleControl ;
387
+ var ConsoleControlObj = depObj as ConsoleControl ;
398
388
if ( ConsoleControlObj != null )
399
389
{
400
- if ( e . NewValue != DependencyProperty . UnsetValue )
401
- {
402
- ConsoleControlObj . Foreground = ConsoleControlObj . _brushes . BrushFromConsoleColor ( ( ConsoleColor ) e . NewValue ) ;
403
- }
404
- else
405
- {
406
- ConsoleControlObj . Foreground = ConsoleControlObj . _brushes . DefaultForeground ;
407
- }
390
+ ConsoleControlObj . Foreground = e . NewValue != DependencyProperty . UnsetValue
391
+ ? ConsoleControlObj . _brushes . BrushFromConsoleColor ( ( ConsoleColor ) e . NewValue )
392
+ : ConsoleControlObj . _brushes . DefaultForeground ;
408
393
}
409
394
}
410
395
@@ -418,6 +403,8 @@ public void Write(string message, Block target = null)
418
403
Write ( null , null , message , target ) ;
419
404
}
420
405
406
+ [ SuppressMessage ( "RefactoringEssentials" , "RECS0026" , Justification = "Creating a Run with the target set puts it in the document automatically" ) ]
407
+ [ SuppressMessage ( "ReSharper" , "ObjectCreationAsStatement" ) ]
421
408
public void Write ( Brush foreground , Brush background , string text , Block target = null )
422
409
{
423
410
//if (target == null) target = Current;
@@ -431,6 +418,7 @@ public void Write(Brush foreground, Brush background, string text, Block target
431
418
432
419
if ( target == null ) target = Current ;
433
420
421
+ // Creating the run with the target set puts it in the document automatically.
434
422
new Run ( text , target . ContentEnd )
435
423
{
436
424
Background = background ,
@@ -441,12 +429,15 @@ public void Write(Brush foreground, Brush background, string text, Block target
441
429
} ) ;
442
430
}
443
431
432
+ [ SuppressMessage ( "RefactoringEssentials" , "RECS0026" , Justification = "Creating a Run with the target set puts it in the document automatically" ) ]
433
+ [ SuppressMessage ( "ReSharper" , "ObjectCreationAsStatement" ) ]
444
434
public void Write ( ConsoleColor foreground , ConsoleColor background , string text , Block target = null )
445
435
{
446
436
if ( target == null ) target = Current ;
447
437
448
438
Dispatcher . BeginInvoke ( DispatcherPriority . Render , ( Action ) delegate
449
439
{
440
+ // Creating the run with the target set puts it in the document automatically.
450
441
new Run ( text , target . ContentEnd )
451
442
{
452
443
Background = ( this . BackgroundColor == background ) ? this . Brushes . Transparent : this . _brushes . BrushFromConsoleColor ( background ) ,
@@ -569,16 +560,29 @@ protected virtual void OnCommand(CommandEventArgs command)
569
560
_cmdHistory . Add ( command . Command . TrimEnd ( ) ) ;
570
561
Trace . WriteLine ( "OnCommand, clearing KeyInfo queue." ) ;
571
562
}
563
+
564
+ public int CurrentCommandCursorPos
565
+ {
566
+ get
567
+ {
568
+ return _commandBox . CaretPosition . GetTextRunLength ( LogicalDirection . Backward ) ;
569
+ }
570
+ set
571
+ {
572
+ _commandBox . CaretPosition = _commandBox . Document . ContentStart . GetPositionAtOffset ( value ) ;
573
+ }
574
+ }
575
+
572
576
public string CurrentCommandPreCursor
573
577
{
574
578
get
575
579
{
576
- TextRange preCursor = new TextRange ( _commandBox . Document . ContentStart , _commandBox . CaretPosition ) ;
580
+ var preCursor = new TextRange ( _commandBox . Document . ContentStart , _commandBox . CaretPosition ) ;
577
581
return preCursor . Text . TrimEnd ( '\n ' , '\r ' ) ;
578
582
}
579
583
set
580
584
{
581
- TextRange preCursor = new TextRange ( _commandBox . Document . ContentStart , _commandBox . CaretPosition ) ;
585
+ var preCursor = new TextRange ( _commandBox . Document . ContentStart , _commandBox . CaretPosition ) ;
582
586
// TODO: re-parse and syntax highlight
583
587
preCursor . Text = value . TrimEnd ( '\n ' , '\r ' ) ;
584
588
}
@@ -587,12 +591,12 @@ public string CurrentCommandPostCursor
587
591
{
588
592
get
589
593
{
590
- TextRange postCursor = new TextRange ( _commandBox . CaretPosition , _commandBox . Document . ContentEnd ) ;
594
+ var postCursor = new TextRange ( _commandBox . CaretPosition , _commandBox . Document . ContentEnd ) ;
591
595
return postCursor . Text . TrimEnd ( '\n ' , '\r ' ) ;
592
596
}
593
597
set
594
598
{
595
- TextRange postCursor = new TextRange ( _commandBox . CaretPosition , _commandBox . Document . ContentEnd ) ;
599
+ var postCursor = new TextRange ( _commandBox . CaretPosition , _commandBox . Document . ContentEnd ) ;
596
600
// TODO: re-parse and syntax highlight
597
601
postCursor . Text = value . TrimEnd ( '\n ' , '\r ' ) ;
598
602
@@ -603,8 +607,8 @@ public string CurrentCommand
603
607
{
604
608
get
605
609
{
606
- TextRange all = new TextRange ( _commandBox . Document . ContentStart , _commandBox . Document . ContentEnd ) ;
607
- return all . Text ;
610
+ var all = new TextRange ( _commandBox . Document . ContentStart , _commandBox . Document . ContentEnd ) ;
611
+ return all . Text . TrimEnd ( ' \n ' , ' \r ' ) ;
608
612
}
609
613
set
610
614
{
@@ -1119,14 +1123,18 @@ public static IEnumerable<TextRange> FindText(FlowDocument document, String inpu
1119
1123
1120
1124
public void Dispose ( )
1121
1125
{
1122
- if ( _gotInputKey != null )
1126
+ Dispose ( true ) ;
1127
+ GC . SuppressFinalize ( this ) ;
1128
+ }
1129
+ protected virtual void Dispose ( bool disposing )
1130
+ {
1131
+ if ( disposing )
1123
1132
{
1133
+ // free managed resources
1124
1134
_gotInputKey . Dispose ( ) ;
1125
- }
1126
- if ( _gotInputLine != null )
1127
- {
1128
1135
_gotInputLine . Dispose ( ) ;
1129
1136
}
1137
+ // free native resources (if there are any)
1130
1138
}
1131
1139
}
1132
1140
}
0 commit comments