Skip to content

Commit

Permalink
Added a status bar to the PowerShell console that's used to show prog…
Browse files Browse the repository at this point in the history
…ress records from PowerShell.
  • Loading branch information
lstratman committed May 29, 2013
1 parent e6705a2 commit d73b5e4
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 9 deletions.
50 changes: 46 additions & 4 deletions Protocols/PowerShell/PowerShellConnectionForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Protocols/PowerShell/PowerShellConnectionForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public override void Connect()
{
GEnv.Options.Font = Connection.Font;

_progressBar.Value = 0;
_progressLabel.Text = "";

// This is not strictly a network connection: we're relaying information that we receive from the runspace to the terminal over a local stream
// (a StreamConnection in this case)
TerminalParam terminalParam = TCPTerminalParam.Fake;
Expand All @@ -147,7 +150,7 @@ public override void Connect()
_terminal.TerminalPane.SendShiftTab = true;
_terminal.SetPaneColors(Connection.TextColor, Connection.BackgroundColor);

_powerShellHost = new PowerShellHost(this, _terminal, ExecuteQuiet);
_powerShellHost = new PowerShellHost(this, _terminal, ExecuteQuiet, _progressBar, _progressLabel);

// Create the host and runspace instances for this interpreter. If we're connecting to the local host, don't bother with the connection info.
// ReSharper disable StringCompareIsCultureSpecific.3
Expand Down
3 changes: 3 additions & 0 deletions Protocols/PowerShell/PowerShellConnectionForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="_statusStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>
7 changes: 5 additions & 2 deletions Protocols/PowerShell/PowerShellHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using WalburySoftware;

namespace EasyConnect.Protocols.PowerShell
Expand Down Expand Up @@ -51,10 +52,12 @@ public class PowerShellHost : PSHost, IHostSupportsInteractiveSession
/// <param name="parentForm">The connection form that will display the PowerShell console.</param>
/// <param name="terminal">Terminal control that will display the PowerShell console.</param>
/// <param name="executeHelper">Method used to execute PowerShell commands within the current session.</param>
public PowerShellHost(PowerShellConnectionForm parentForm, TerminalControl terminal, Func<string, Collection<PSObject>> executeHelper)
/// <param name="progressBar">Progress bar UI element to update when writing progress records.</param>
/// <param name="progressLabel">Label UI element to update when writing progress records.</param>
public PowerShellHost(PowerShellConnectionForm parentForm, TerminalControl terminal, Func<string, Collection<PSObject>> executeHelper, ToolStripProgressBar progressBar, ToolStripStatusLabel progressLabel)
{
_parentForm = parentForm;
_powerShellHostUi = new PowerShellHostUi(terminal, executeHelper);
_powerShellHostUi = new PowerShellHostUi(terminal, executeHelper, progressBar, progressLabel);
}

/// <summary>
Expand Down
55 changes: 53 additions & 2 deletions Protocols/PowerShell/PowerShellHostUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
using System.Security;
using System.Text;
using System.Threading;
using System.Timers;
using System.Windows.Forms;
using WalburySoftware;
using Timer = System.Timers.Timer;

namespace EasyConnect.Protocols.PowerShell
{
Expand Down Expand Up @@ -59,7 +62,7 @@ public class PowerShellHostUi : PSHostUserInterface, IHostUISupportsMultipleChoi
protected Dictionary<string, List<string>> _intellisenseParameters = new Dictionary<string, List<string>>();

/// <summary>
/// Thread that is retrieving intellisense commands.
/// Thread that is retrieving Intellisense commands.
/// </summary>
protected Thread _intellisenseThread = null;

Expand Down Expand Up @@ -89,16 +92,35 @@ public class PowerShellHostUi : PSHostUserInterface, IHostUISupportsMultipleChoi
/// </summary>
protected Stack<string> _upCommandHistory = new Stack<string>();

/// <summary>
/// Progress bar UI element to update when writing progress records.
/// </summary>
protected ToolStripProgressBar _progressBar;

/// <summary>
/// Label UI element to update when writing progress records.
/// </summary>
protected ToolStripStatusLabel _progressLabel;

/// <summary>
/// Timer to use for asynchronous UI events.
/// </summary>
protected Timer _timer = new Timer();

/// <summary>
/// Default constructor.
/// </summary>
/// <param name="terminal">Terminal control that will display the PowerShell console.</param>
/// <param name="executeHelper">Method used to execute PowerShell commands within the current session.</param>
public PowerShellHostUi(TerminalControl terminal, Func<string, Collection<PSObject>> executeHelper)
/// <param name="progressBar">Progress bar UI element to update when writing progress records.</param>
/// <param name="progressLabel">Label UI element to update when writing progress records.</param>
public PowerShellHostUi(TerminalControl terminal, Func<string, Collection<PSObject>> executeHelper, ToolStripProgressBar progressBar, ToolStripStatusLabel progressLabel)
{
_terminal = terminal;
_powerShellRawUi = new PowerShellRawUi(terminal);
_executeHelper = executeHelper;
_progressBar = progressBar;
_progressLabel = progressLabel;
}

/// <summary>
Expand Down Expand Up @@ -868,6 +890,35 @@ public override void WriteLine(string value)
/// <param name="record">A ProgressReport object.</param>
public override void WriteProgress(long sourceId, ProgressRecord record)
{
_progressLabel.Text = String.IsNullOrEmpty(record.Activity)
? ""
: record.Activity + (String.IsNullOrEmpty(record.CurrentOperation)
? ""
: ": ") + record.CurrentOperation;
_progressBar.Value = record.PercentComplete;

// If we've completed, leave it in its current state for one second and then clear the progress data
if (_progressBar.Value == 100)
{
_timer.Enabled = true;
_timer.Interval = 1000;
_timer.Elapsed += _timer_Elapsed;
_timer.Start();
}
}

/// <summary>
/// Handler method that's called when <see cref="_timer"/> ticks. Clears the progress data from the UI and stops the timer.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="_timer"/> in this case.</param>
/// <param name="e">Arguments associated with this event.</param>
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
_progressLabel.Text = "";
_progressBar.Value = 0;

_timer.Elapsed -= _timer_Elapsed;
_timer.Stop();
}

/// <summary>
Expand Down

0 comments on commit d73b5e4

Please sign in to comment.