Skip to content

Commit 7880aaf

Browse files
committed
Huge refactor. Fix SessionState, History, TabExpansion
1 parent a0aca93 commit 7880aaf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2127
-1940
lines changed

PoshCode.Controls/ConsoleControl/TabExpansion.cs

-118
This file was deleted.
File renamed without changes.

PoshCode.Controls/ConsoleBrushes.cs PoshCode.Controls/Controls/ConsoleBrushes.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.Windows.Media;
3-
using Colors = PoshCode.Controls.Properties.Colors;
3+
using Colors = PoshCode.Properties.Colors;
44

55
namespace PoshCode.Controls
66
{

PoshCode.Controls/ConsoleControl.cs PoshCode.Controls/Controls/ConsoleControl.cs

+48-40
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.ObjectModel;
44
using System.ComponentModel;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Globalization;
78
using System.Management.Automation;
89
using System.Management.Automation.Host;
@@ -182,11 +183,12 @@ public void Prompt(string prompt)
182183
{
183184
if (Next != null)
184185
{
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+
};
188191
Next.Inlines.Add(insert);
189-
190192
SetPrompt();
191193
}
192194
});
@@ -237,14 +239,7 @@ private void ShouldEcho(char ch, bool echo)
237239
if (ch == cmd[0])
238240
{
239241
// 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) : "";
248243
// if we're NOT NoEcho, then re-echo it:
249244
if (echo)
250245
{
@@ -354,17 +349,12 @@ public ConsoleColor BackgroundColor
354349
private static void BackgroundColorPropertyChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
355350
{
356351
// put code here to handle the property changed for BackgroundColor
357-
ConsoleControl consoleControlObj = depObj as ConsoleControl;
352+
var consoleControlObj = depObj as ConsoleControl;
358353
if (consoleControlObj != null)
359354
{
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;
368358
}
369359
}
370360

@@ -394,17 +384,12 @@ private static void ForegroundColorPropertyChanged(DependencyObject depObj, Depe
394384
{
395385

396386
// put code here to handle the property changed for ForegroundColor
397-
ConsoleControl ConsoleControlObj = depObj as ConsoleControl;
387+
var ConsoleControlObj = depObj as ConsoleControl;
398388
if (ConsoleControlObj != null)
399389
{
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;
408393
}
409394
}
410395

@@ -418,6 +403,8 @@ public void Write(string message, Block target = null)
418403
Write(null, null, message, target);
419404
}
420405

406+
[SuppressMessage("RefactoringEssentials", "RECS0026", Justification = "Creating a Run with the target set puts it in the document automatically")]
407+
[SuppressMessage("ReSharper", "ObjectCreationAsStatement")]
421408
public void Write(Brush foreground, Brush background, string text, Block target = null)
422409
{
423410
//if (target == null) target = Current;
@@ -431,6 +418,7 @@ public void Write(Brush foreground, Brush background, string text, Block target
431418

432419
if (target == null) target = Current;
433420

421+
// Creating the run with the target set puts it in the document automatically.
434422
new Run(text, target.ContentEnd)
435423
{
436424
Background = background,
@@ -441,12 +429,15 @@ public void Write(Brush foreground, Brush background, string text, Block target
441429
});
442430
}
443431

432+
[SuppressMessage("RefactoringEssentials", "RECS0026", Justification = "Creating a Run with the target set puts it in the document automatically")]
433+
[SuppressMessage("ReSharper", "ObjectCreationAsStatement")]
444434
public void Write(ConsoleColor foreground, ConsoleColor background, string text, Block target = null)
445435
{
446436
if (target == null) target = Current;
447437

448438
Dispatcher.BeginInvoke(DispatcherPriority.Render, (Action)delegate
449439
{
440+
// Creating the run with the target set puts it in the document automatically.
450441
new Run(text, target.ContentEnd)
451442
{
452443
Background = (this.BackgroundColor == background) ? this.Brushes.Transparent : this._brushes.BrushFromConsoleColor(background),
@@ -569,16 +560,29 @@ protected virtual void OnCommand(CommandEventArgs command)
569560
_cmdHistory.Add(command.Command.TrimEnd());
570561
Trace.WriteLine("OnCommand, clearing KeyInfo queue.");
571562
}
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+
572576
public string CurrentCommandPreCursor
573577
{
574578
get
575579
{
576-
TextRange preCursor = new TextRange(_commandBox.Document.ContentStart, _commandBox.CaretPosition);
580+
var preCursor = new TextRange(_commandBox.Document.ContentStart, _commandBox.CaretPosition);
577581
return preCursor.Text.TrimEnd('\n', '\r');
578582
}
579583
set
580584
{
581-
TextRange preCursor = new TextRange(_commandBox.Document.ContentStart, _commandBox.CaretPosition);
585+
var preCursor = new TextRange(_commandBox.Document.ContentStart, _commandBox.CaretPosition);
582586
// TODO: re-parse and syntax highlight
583587
preCursor.Text = value.TrimEnd('\n', '\r');
584588
}
@@ -587,12 +591,12 @@ public string CurrentCommandPostCursor
587591
{
588592
get
589593
{
590-
TextRange postCursor = new TextRange(_commandBox.CaretPosition, _commandBox.Document.ContentEnd);
594+
var postCursor = new TextRange(_commandBox.CaretPosition, _commandBox.Document.ContentEnd);
591595
return postCursor.Text.TrimEnd('\n', '\r');
592596
}
593597
set
594598
{
595-
TextRange postCursor = new TextRange(_commandBox.CaretPosition, _commandBox.Document.ContentEnd);
599+
var postCursor = new TextRange(_commandBox.CaretPosition, _commandBox.Document.ContentEnd);
596600
// TODO: re-parse and syntax highlight
597601
postCursor.Text = value.TrimEnd('\n', '\r');
598602

@@ -603,8 +607,8 @@ public string CurrentCommand
603607
{
604608
get
605609
{
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');
608612
}
609613
set
610614
{
@@ -1119,14 +1123,18 @@ public static IEnumerable<TextRange> FindText(FlowDocument document, String inpu
11191123

11201124
public void Dispose()
11211125
{
1122-
if (_gotInputKey != null)
1126+
Dispose(true);
1127+
GC.SuppressFinalize(this);
1128+
}
1129+
protected virtual void Dispose(bool disposing)
1130+
{
1131+
if (disposing)
11231132
{
1133+
// free managed resources
11241134
_gotInputKey.Dispose();
1125-
}
1126-
if (_gotInputLine != null)
1127-
{
11281135
_gotInputLine.Dispose();
11291136
}
1137+
// free native resources (if there are any)
11301138
}
11311139
}
11321140
}

PoshCode.Controls/ConsoleControl/ConsoleControl.BufferSize.cs PoshCode.Controls/Controls/ConsoleControl/ConsoleControl.BufferSize.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private void UpdateCharacterWidth()
210210
BufferCell[,] bufferCells = new BufferCell[(rectangle.Top - rectangle.Bottom), (rectangle.Right - rectangle.Left)];
211211
try
212212
{
213-
int cur =
213+
var cur =
214214
(int)
215215
((Next.ElementEnd.GetCharacterRect(LogicalDirection.Backward).Bottom +
216216
ScrollViewer.ContentVerticalOffset)/
@@ -271,8 +271,8 @@ public Coordinates WindowPosition
271271
{
272272
get
273273
{
274-
int x = 0;
275-
int y = (int)(ScrollViewer.ContentVerticalOffset / (Double.IsNaN(Document.LineHeight) ? Document.FontSize : Document.LineHeight));
274+
var x = 0;
275+
var y = (int)(ScrollViewer.ContentVerticalOffset / (Double.IsNaN(Document.LineHeight) ? Document.FontSize : Document.LineHeight));
276276
return new Coordinates(x, y);
277277
}
278278
set

PoshCode.Controls/ConsoleControl/ConsoleControl.CommandHandlers.cs PoshCode.Controls/Controls/ConsoleControl/ConsoleControl.CommandHandlers.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ partial class ConsoleControl
1818
/// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
1919
private static void OnApplicationStop(object sender, ExecutedRoutedEventArgs e)
2020
{
21-
ConsoleControl control = (ConsoleControl)sender;
21+
var control = (ConsoleControl)sender;
2222
//if (!control.IsRunning)
2323
//{
2424
// // TODO: Remove failed command from History ... control.History.ResetCurrentCommand();
@@ -57,7 +57,7 @@ private static void OnApplicationStop(object sender, ExecutedRoutedEventArgs e)
5757

5858
private static void OnCanExecuteCut(object target, CanExecuteRoutedEventArgs args)
5959
{
60-
ConsoleControl box = (ConsoleControl)(target);
60+
var box = (ConsoleControl)(target);
6161
args.CanExecute = box.IsEnabled && box.Selection != null && !box.Selection.IsEmpty;
6262
}
6363

@@ -96,17 +96,13 @@ private static void OnExecuteCut(object sender, ExecutedRoutedEventArgs e)
9696

9797
private static void OnExecutePaste(object sender, ExecutedRoutedEventArgs e)
9898
{
99-
RichTextBox box = ((ConsoleControl)sender)._commandContainer.Child as RichTextBox;
99+
var box = ((ConsoleControl)sender)._commandContainer.Child as RichTextBox;
100100
if(box != null)
101101
{
102102
box.Paste();
103103
} else
104104
{
105-
PasswordBox pw = ((ConsoleControl) sender)._commandContainer.Child as PasswordBox;
106-
if (pw != null)
107-
{
108-
pw.Paste();
109-
}
105+
(((ConsoleControl) sender)._commandContainer.Child as PasswordBox)?.Paste();
110106
}
111107
//if (Clipboard.ContainsText())
112108
//{

0 commit comments

Comments
 (0)