PoshCode Logo PowerShell Code Repository

Start-Demo 1 for PS3 ISE by Joel Bennett 4 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/4132"></script>download | new post

The first version of a Start-Demo script (module) for ISE from PowerShell 3+

Import this as a module and call Start-Demo

  1. function Start-Demo {
  2.     param(
  3.        $Text = $PSISE.CurrentFile.Editor.Text
  4.     )
  5.     $psISE.CurrentPowerShellTab.ConsolePane.Clear()
  6.  
  7.     $Tokens =  $Errors = $null
  8.     $Script:Editor = $PSISE.CurrentFile.Editor
  9.     $Script:Text = $Script:Editor.Text
  10.     $AST = [System.Management.Automation.Language.Parser]::ParseInput( $Text, [ref]$Tokens, [ref]$Errors )
  11.     if($Errors) { $Errors | Write-Error }
  12.     # Assumes that demo scripts don't have begin/process blocks
  13.     $Script:Statements = $AST.EndBlock.Statements.Extent
  14.     $Script:Index = $Script:Offset = 0
  15.  
  16.     $Function:Prompt = { PreDemoPrompt; Pop-Demo }
  17. }
  18.  
  19. function Stop-Demo {
  20.     $Function:Prompt = $function:PreDemoPrompt
  21. }
  22.  
  23. function Pop-Demo {
  24.     if($Script:Index -lt $Script:Statements.Count) {
  25.         $Statement = $Script:Statements[$Script:Index]
  26.         # use the offset to make sure we type any leading comments, and not just the statement
  27.         # $DemoStep = $Script:Text.Substring($Script:Offset, $Statement.EndOffset - $Script:Offset).TrimStart("`r`n")
  28.        
  29.         $DemoStep = $Statement.Text
  30.  
  31.         $Script:Editor.Select($Statement.StartLineNumber, $Statement.StartColumnNumber, $Statement.EndLineNumber, $Statement.EndColumnNumber)
  32.         $Script:Offset = $Statement.EndOffset + 1
  33.         # put it in the console
  34.         $psISE.CurrentPowerShellTab.ConsolePane.InputText = $DemoStep
  35.         $psISE.CurrentPowerShellTab.ConsolePane.Focus()
  36.         $Script:Index += 1
  37.     } else {
  38.         Stop-Demo
  39.     }
  40. }
  41.  
  42. $function:PreDemoPrompt = $function:Prompt
  43.  
  44. if(!($psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.DisplayName -eq "Start Demo")) {
  45.     try {
  46.         $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Start Demo',{Start-Demo},"F6")
  47.     } catch [System.Management.Automation.MethodInvocationException] {
  48.         $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Start Demo',{Start-Demo},$null)
  49.     }
  50. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me