PoshCode Archive  Artifact [97be51fef2]

Artifact 97be51fef2c239ec456b2c6910767a9b27bc48397220a63b9f25f238c50f127b:

  • File Set-Prompt.ps1 — part of check-in [d656a92e90] at 2018-06-10 13:04:56 on branch trunk — My command Prompt — I recently added a “Pastable” option so that I can safely copy-paste a commandline and have it run without having to manually edit out my prompt. (user: Joel Bennett size: 7289)

# encoding: ascii
# api: powershell
# title: Set-Prompt
# description: My command Prompt — I recently added a “Pastable” option so that I can safely copy-paste a commandline and have it run without having to manually edit out my prompt.
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# x-poshcode-id: 2095
# x-archived: 2012-01-10T09:57:17
# x-published: 2012-08-20T12:43:00
#
#
param([Alias("copy","demo")][Switch]$Pasteable)
# This should go OUTSIDE the prompt function, it doesn't need re-evaluation
# We're going to calculate a prefix for the window title 
# Our basic title is "PoSh - C:\Your\Path\Here" showing the current path
if(!$global:WindowTitlePrefix) {
   # But if you're running "elevated" on vista, we want to show that ...
   if( ([System.Environment]::OSVersion.Version.Major -gt 5) -and ( # Vista and ...
         new-object Security.Principal.WindowsPrincipal (
            [Security.Principal.WindowsIdentity]::GetCurrent()) # current user is admin
            ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) )
   {
      $global:WindowTitlePrefix = "PoSh ${Env:UserName}@${Env:UserDomain} (ADMIN)"
   } else {
      $global:WindowTitlePrefix = "PoSh ${Env:UserName}@${Env:UserDomain}"
   }
}

if($Pasteable) {
   function global:prompt {
      Write-host "<#PS " -NoNewLine -fore gray
      # FIRST, make a note if there was an error in the previous command
      $err = !$?

      # Make sure Windows and .Net know where we are (they can only handle the FileSystem)
      [Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath
      # Also, put the path in the title ... (don't restrict this to the FileSystem
      $Host.UI.RawUI.WindowTitle = "{0} - {1} ({2})" -f $global:WindowTitlePrefix,$pwd.Path,$pwd.Provider.Name
      
      # Determine what nesting level we are at (if any)
      $Nesting = "$([char]0xB7)" * $NestedPromptLevel

      # Generate PUSHD(push-location) Stack level string
      $Stack = "+" * (Get-Location -Stack).count
      
      # my New-Script and Get-PerformanceHistory functions use history IDs
      # So, put the ID of the command in, so we can get/invoke-history easier
      # eg: "r 4" will re-run the command that has [4]: in the prompt
      $global:lastCommandId = (Get-History -count 1).Id
      $global:nextCommandId = $global:lastCommandId + 1
      # Output prompt string
      # If there's an error, set the prompt foreground to "Red", otherwise, "Yellow"
      if($err) { $fg = "Red" } else { $fg = "Yellow" }
      # Notice: no angle brackets, makes it easy to paste my buffer to the web
      Write-Host "[${Nesting}${nextCommandId}${Stack}]" -NoNewLine -Fore $fg   
      Write-host " #>" -NoNewLine -fore gray
      return " "
   }
} else {
   function global:prompt {
      # FIRST, make a note if there was an error in the previous command
      $err = !$?

      # Make sure Windows and .Net know where we are (they can only handle the FileSystem)
      [Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath
      # Also, put the path in the title ... (don't restrict this to the FileSystem
      $Host.UI.RawUI.WindowTitle = "{0} - {1} ({2})" -f $global:WindowTitlePrefix,$pwd.Path,$pwd.Provider.Name
      
      # Determine what nesting level we are at (if any)
      $Nesting = "$([char]0xB7)" * $NestedPromptLevel

      # Generate PUSHD(push-location) Stack level string
      $Stack = "+" * (Get-Location -Stack).count
      
      # my New-Script and Get-PerformanceHistory functions use history IDs
      # So, put the ID of the command in, so we can get/invoke-history easier
      # eg: "r 4" will re-run the command that has [4]: in the prompt
      $global:lastCommandId = (Get-History -count 1).Id
      $global:nextCommandId = $global:lastCommandId + 1
      # Output prompt string
      # If there's an error, set the prompt foreground to "Red", otherwise, "Yellow"
      if($err) { $fg = "Red" } else { $fg = "Yellow" }
      # Notice: no angle brackets, makes it easy to paste my buffer to the web
      Write-Host "[${Nesting}${nextCommandId}${Stack}]:" -NoNewLine -Fore $fg   
      return " "
   }
}

# SIG # Begin signature block
# MIIIDQYJKoZIhvcNAQcCoIIH/jCCB/oCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQ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