PoshCode Logo PowerShell Code Repository

Start-Demo 3.3.2 by Joel Bennett 8 years ago (modification of post by BSonPosh view diff)
View followups from Joel Bennett | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/690"></script>download | new post

This is an overhaul of Jeffrey Snover’s original Start-Demo script … I’ve switched it to use ReadKey, which saves you some typing and makes the whole thing seem more natural when you’re demoing, (at least to me). I’ve also added a bunch of command-line options and a couple of features in the process (see the Revision History in the script).

  1. ## Start-Demo.ps1
  2. ##################################################################################################
  3. ## This is an overhaul of Jeffrey Snover's original Start-Demo script by Joel "Jaykul" Bennett
  4. ##
  5. ## I've switched it to using ReadKey instead of ReadLine (you don't have to hit Enter each time)
  6. ## As a result, I've changed the names and keys for a lot of the operations, so that they make
  7. ## sense with only a single letter to tell them apart (sorry if you had them memorized).
  8. ##
  9. ## I've also been adding features as I come across needs for them, and you'll contribute your
  10. ## improvements back to the PowerShell Script repository as well.
  11. ##################################################################################################
  12. ## Revision History (version 3.3)
  13. ## 3.3.2 Fixed:    Changed the "x" to match the "a" in the help text
  14. ## 3.3.1 Fixed:    Added a missing bracket in the script
  15. ## 3.3 - Added:    Added a "Clear Screen" option
  16. ##     - Added:    Added a "Rewind" function (which I'm not using much)
  17. ## 3.2 - Fixed:    Put back the trap { continue; }
  18. ## 3.1 - Fixed:    No Output when invoking Get-Member (and other cmdlets like it???)
  19. ## 3.0 - Fixed:    Commands which set a variable, like: $files = ls
  20. ##     - Fixed:    Default action doesn't continue
  21. ##     - Changed:  Use ReadKey instead of ReadLine
  22. ##     - Changed:  Modified the option prompts (sorry if you had them memorized)
  23. ##     - Changed:  Various time and duration strings have better formatting
  24. ##     - Enhance:  Colors are settable: prompt, command, comment
  25. ##     - Added:    NoPauseAfterExecute switch removes the extra pause
  26. ##                 If you set this, the next command will be displayed immediately
  27. ##     - Added:    Auto Execute mode (FullAuto switch) runs the rest of the script
  28. ##                 at an automatic speed set by the AutoSpeed parameter (or manually)
  29. ##     - Added:    Automatically append an empty line to the end of the demo script
  30. ##                 so you have a chance to "go back" after the last line of you demo
  31. ##################################################################################################
  32. ##
  33. param(
  34.   $file=".\demo.txt",
  35.   [int]$command=0,
  36.   [System.ConsoleColor]$promptColor="Yellow",
  37.   [System.ConsoleColor]$commandColor="White",
  38.   [System.ConsoleColor]$commentColor="Green",
  39.   [switch]$FullAuto,
  40.   [int]$AutoSpeed = 3,
  41.   [switch]$NoPauseAfterExecute
  42. )
  43.  
  44. $RawUI = $Host.UI.RawUI
  45. $hostWidth = $RawUI.BufferSize.Width
  46.  
  47. # A function for reading in a character
  48. function Read-Char() {
  49.   $_OldColor = $RawUI.ForeGroundColor
  50.   $RawUI.ForeGroundColor = "Red"
  51.   $inChar=$RawUI.ReadKey("IncludeKeyUp")
  52.   # loop until they press a character, so Shift or Ctrl, etc don't terminate us
  53.   while($_input.Character -eq 0){
  54.     $inChar=$RawUI.ReadKey("IncludeKeyUp")
  55.   }
  56.   $RawUI.ForeGroundColor = $_OldColor
  57.   return $inChar.Character
  58. }
  59.  
  60. function Rewind($lines, $index, $steps = 1) {
  61.    $started = $index;
  62.    $index -= $steps;
  63.    while(($index -ge 0) -and ($lines[$index].Trim(" `t").StartsWith("#"))){
  64.       $index--
  65.    }
  66.    if( $index -lt 0 ) { $index = $started }
  67.    return $index
  68. }
  69.  
  70. $file = Resolve-Path $file
  71. while(-not(Test-Path $file)) {
  72.   $file = Read-Host "Please enter the path of your demo script (Crtl+C to cancel)"
  73.   $file = Resolve-Path $file
  74. }
  75.  
  76. Clear-Host
  77.  
  78. $_lines = Get-Content $file
  79. # Append an extra (do nothing) line on the end so we can still go back after the last line.
  80. $_lines += "Write-Host 'The End'"
  81. $_starttime = [DateTime]::now
  82. $FullAuto = $false
  83.  
  84. Write-Host -nonew -back black -fore $promptColor $(" " * $hostWidth)
  85. Write-Host -nonew -back black -fore $promptColor @"
  86. <Demo Started :: $(split-path $file -leaf)>$(' ' * ($hostWidth -(18 + $(split-path $file -leaf).Length)))
  87. "@
  88. Write-Host -nonew -back black -fore $promptColor "Press"
  89. Write-Host -nonew -back black -fore Red " ? "
  90. Write-Host -nonew -back black -fore $promptColor "for help.$(' ' * ($hostWidth -17))"
  91. Write-Host -nonew -back black -fore $promptColor $(" " * $hostWidth)
  92.  
  93. # We use a FOR and an INDEX ($_i) instead of a FOREACH because
  94. # it is possible to start at a different location and/or jump
  95. # around in the order.
  96. for ($_i = $Command; $_i -lt $_lines.count; $_i++)
  97. {  
  98.         # Put the current command in the Window Title along with the demo duration
  99.         $Dur = [DateTime]::Now - $_StartTime
  100.   $RawUI.WindowTitle = "$(if($dur.Hours -gt 0){'{0}h '})$(if($dur.Minutes -gt 0){'{1}m '}){2}s   {3}" -f
  101.                         $dur.Hours, $dur.Minutes, $dur.Seconds, $($_Lines[$_i])
  102.  
  103.         # Echo out the commmand to the console with a prompt as though it were real
  104.         Write-Host -nonew -fore $promptColor "[$_i]PS> "
  105.         if ($_lines[$_i].Trim(" ").StartsWith("#")) {
  106.                 Write-Host -fore $commentColor "$($_Lines[$_i])  "
  107.                 continue
  108.         } else {
  109.                 Write-Host -nonew -fore $commandColor "$($_Lines[$_i])  "
  110.         }
  111.  
  112.         if( $FullAuto ) { Start-Sleep $autoSpeed; $ch = [char]13 } else { $ch = Read-Char }
  113.         switch($ch)
  114.         {
  115.                 "?" {
  116.                         Write-Host -Fore $promptColor @"
  117.  
  118. Running demo: $file
  119. (n) Next       (p) Previous
  120. (q) Quit       (s) Suspend
  121. (t) Timecheck  (v) View $(split-path $file -leaf)
  122. (g) Go to line by number
  123. (f) Find lines by string
  124. (a) Auto Execute mode
  125. (c) Clear Screen
  126. "@
  127.                         $_i-- # back a line, we're gonna step forward when we loop
  128.                 }
  129.                 "n" { # Next (do nothing)
  130.                         Write-Host -Fore $promptColor "<Skipping Line>"
  131.                 }
  132.                 "p" { # Previous
  133.                         Write-Host -Fore $promptColor "<Back one Line>"
  134.                         while ($_lines[--$_i].Trim(" ").StartsWith("#")){}
  135.                         $_i-- # back a line, we're gonna step forward when we loop
  136.                 }
  137.                 "a" { # EXECUTE (Go Faster)
  138.                         $AutoSpeed = [int](Read-Host "Pause (seconds)")
  139.                         $FullAuto = $true;
  140.                         Write-Host -Fore $promptColor "<eXecute Remaining Lines>"
  141.                         $_i-- # Repeat this line, and then just blow through the rest
  142.                 }
  143.                 "q" { # Quit
  144.                         Write-Host -Fore $promptColor "<Quiting demo>"
  145.                         $_i = $_lines.count;
  146.                         break;
  147.                 }
  148.                 "v" { # View Source
  149.                         $lines[0..($_i-1)] | Write-Host -Fore Yellow
  150.                         $lines[$_i]        | Write-Host -Fore Green
  151.                         $lines[($_i+1)..$lines.Count] | Write-Host -Fore Yellow
  152.                         $_i-- # back a line, we're gonna step forward when we loop
  153.                 }
  154.                 "t" { # Time Check
  155.                          $dur = [DateTime]::Now - $_StartTime
  156.        Write-Host -Fore $promptColor $(
  157.           "{3} -- $(if($dur.Hours -gt 0){'{0}h '})$(if($dur.Minutes -gt 0){'{1}m '}){2}s" -f
  158.           $dur.Hours, $dur.Minutes, $dur.Seconds, ([DateTime]::Now.ToShortTimeString()))
  159.                          $_i-- # back a line, we're gonna step forward when we loop
  160.                 }
  161.                 "s" { # Suspend (Enter Nested Prompt)
  162.                         Write-Host -Fore $promptColor "<Suspending demo - type 'Exit' to resume>"
  163.                         $Host.EnterNestedPrompt()
  164.                         $_i-- # back a line, we're gonna step forward when we loop
  165.                 }
  166.                 "g" { # GoTo Line Number
  167.                         $i = [int](Read-Host "line number")
  168.                         if($i -le $_lines.Count) {
  169.                                 if($i -gt 0) {
  170.                # extra line back because we're gonna step forward when we loop
  171.                $_i = Rewind $_lines $_i (($_i-$i)+1)
  172.                                 } else {
  173.                                         $_i = -1 # Start negative, because we step forward when we loop
  174.                                 }
  175.                         }
  176.                 }
  177.                 "f" { # Find by pattern
  178.                         $match = $_lines | Select-String (Read-Host "search string")
  179.                         if($match -eq $null) {
  180.                                 Write-Host -Fore Red "Can't find a matching line"
  181.                         } else {
  182.                                 $match | % { Write-Host -Fore $promptColor $("[{0,2}] {1}" -f ($_.LineNumber - 1), $_.Line) }
  183.                                 if($match.Count -lt 1) {
  184.                                         $_i = $match.lineNumber - 2  # back a line, we're gonna step forward when we loop
  185.                                 } else {              
  186.                                         $_i-- # back a line, we're gonna step forward when we loop
  187.                                 }
  188.                         }
  189.                 }
  190.       "c" {
  191.          Clear-Host
  192.          $_i-- # back a line, we're gonna step forward when we loop
  193.       }
  194.                 "$([char]13)" { # on enter
  195.                         Write-Host
  196.                         trap [System.Exception] {Write-Error $_; continue;}
  197.                         Invoke-Expression ($_lines[$_i]) | out-default
  198.                         if(-not $NoPauseAfterExecute -and -not $FullAuto) {
  199.                                 $null = $RawUI.ReadKey("NoEcho,IncludeKeyUp")  # Pause after output for no apparent reason... ;)
  200.                         }
  201.                 }
  202.                 default
  203.                 {
  204.                         Write-Host -Fore Green "`nKey not recognized.  Press ? for help, or ENTER to execute the command."
  205.                         $_i-- # back a line, we're gonna step forward when we loop
  206.                 }
  207.         }
  208. }
  209. $dur = [DateTime]::Now - $_StartTime
  210. Write-Host -Fore $promptColor $(
  211.    "<Demo Complete -- $(if($dur.Hours -gt 0){'{0}h '})$(if($dur.Minutes -gt 0){'{1}m '}){2}s>" -f
  212.    $dur.Hours, $dur.Minutes, $dur.Seconds, [DateTime]::Now.ToLongTimeString())
  213. Write-Host -Fore $promptColor $([DateTime]::now)
  214. Write-Host

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