::::::::: PowerShell :::::::::
Sunday, June 11, 2006
  "Prompt" function
EDIT2*: Well, That weird out-of-order registration of commands in history is not so funky any more after reading Bruce Payette's post on Microsoft.Public.Windows.PowerShell NG...
"Command that's creating a nested prompt level is complete only after you exit the nested level"...

EDIT*:
This is kind of funky...
Photobucket - Video and Image Hosting
When you look at the output of Get-History in the 6th prompt, Start-NewScope are registered into history after Exit... and moreover, after each Exit, history counts have incremented by 2 instead of 1...

Uhm, I am kind of bored so I am gonna just post my "Prompt" function source in my "Profile.Ps1".
I have 3 parts in my PowerShell prompt
  1. Nested Prompt Level([^_^] <- when the nested prompt level is "0")
  2. "PS" in red(Uhm, I forgot where i saw that flashy red MSH> Prompt last year for the first time...)
  3. History Count([2], not count of last history but the history count of what you will run)
Here is the screenshot of my prompt

Photobucket - Video and Image Hosting

I have found history count(refer to Lee Holmes' blog on "A History Browsing Prompt" for extracting history count, there are few quirks here and there.) to be quite useful as I find myself copying and pasting from console and history(by the way, check out How Keith Hill got around the problem with a SelectHistory script)

Below is my "Prompt" function and "Start-NewScope"(function invoked on the screenshot) can be found on PowerShell Team blog http://blogs.msdn.com/monad/archive/2006/04/15/576996.aspx

# reference: For History Count, 
# Author: DBMwS
# http://www.leeholmes.com/blog/CommentView,guid,0a552b85-2abe-4528-be41-d55762eb41de.aspx
# and Jeffrey Snover's comment on NG for nested prompt(or Start-NewScope)
function prompt
{
if (($NestedPromptLevel -eq $null) -or ($NestedPromptLevel -eq 0)) {
# I want to see more friendly Prompt... so chose "^_^" over "0"
$private:level = "^_^"
} else { $private:level = $NestedPromptLevel }

$private:h = @(Get-History)
$private:nextCommand = $private:h[$private:h.Count - 1].Id + 1

Write-Host -NoNewline -ForeGroundColor Blue [$private:level]
Write-Host -NoNewline -ForeGroundColor Red PS;
Write-Host -NoNewLine -ForeGroundColor Cyan [$private:nextCommand]; ">"
}

I would like to address why i am checking for ($NestedPromptLevel -eq $null) in the "if" statement(According to Jeffrey Snover, "BTW - every time you enter a nested prompt, $NESTEDPROMPTLEVEL is incremented so I include this information in my prompt", but I could find out about it on "get-help about_automatic_variables", thought).

At the time I wrote this function(I am sorry. I have to admit that I almost blatantly copied and pasted from Lee Holmes' blog...), when the powershell started, $NestedPromptLevel was set to a null instead of "0" but I think it seems to have been fixed in RC1 Update.

Tony (Msh For Fun) has a blog entry on how to modify your prompt function the way you like it(like displaying time, and colorizing your prompt, evening randomizing prompt the color...) and what Prompt function is about and why you should bother at all... Great stuff.~

Prompt related links can be found on http://del.icio.us/powershell/Prompt(I have only 3 links so far but I will be tagging more later on.)

Tags :
 
Comments: Post a Comment



<< Home
Let's get lazy with PowerShell!

Name:
Location: Flushing, NY, United States

Experimenting with a different format of blogs...

Links
ARCHIVES
10/01/2005 - 11/01/2005 / 11/01/2005 - 12/01/2005 / 12/01/2005 - 01/01/2006 / 01/01/2006 - 02/01/2006 / 02/01/2006 - 03/01/2006 / 03/01/2006 - 04/01/2006 / 04/01/2006 - 05/01/2006 / 05/01/2006 - 06/01/2006 / 06/01/2006 - 07/01/2006 / 07/01/2006 - 08/01/2006 / 08/01/2006 - 09/01/2006 / 10/01/2006 - 11/01/2006 / 11/01/2006 - 12/01/2006 /


Powered by Blogger