Windows Vista Forums

customized prompt function
  1. #1


    Larry__Weiss Guest

    customized prompt function

    Please take a look at my code for a customized PS prompt below.
    I'd like to figure out how to eliminate #2 and only display $pwd
    when only whitespace is entered as a command.



    And also, this is my first "real" PS code, so comments regarding
    technique or even style would be appreciated.

    All lines below indented two spaces so wrap can be detected:
    Function prompt {
    # PS prompt displays $pwd only when either of any of these is true
    # #1 current command is just whitespace
    # #2 last command is exactly the same as current command
    # #3 no commands have yet been entered
    # When $pwd is displayed, it displays on a line above the effective PS prompt
    #
    $h = h
    $p = ""
    if ($h -eq $null)
    {$p = 'PS ' + "$pwd" + "`n"}
    else
    {
    if ( $h[$h.length-1].CommandLine -eq $globalrompt_last_command )
    {$p = 'PS ' + "$pwd" + "`n"}
    }
    $p + 'PS> '
    if ($h -eq $null)
    {$globalrompt_last_command = ""}
    else
    {$globalrompt_last_command = $h[$h.length-1].CommandLine}
    }

    - Larry

      My System SpecsSystem Spec

  2. #2


    Andrew Savinykh Guest

    Re: customized prompt function

    What if you try comparing IDs instead of CommandLines?

    Andrew.


    Larry__Weiss wrote:

    > Please take a look at my code for a customized PS prompt below.
    > I'd like to figure out how to eliminate #2 and only display $pwd
    > when only whitespace is entered as a command.
    >
    > And also, this is my first "real" PS code, so comments regarding
    > technique or even style would be appreciated.
    >
    > All lines below indented two spaces so wrap can be detected:
    > Function prompt {
    > # PS prompt displays $pwd only when either of any of these is true
    > # #1 current command is just whitespace
    > # #2 last command is exactly the same as current command
    > # #3 no commands have yet been entered
    > # When $pwd is displayed, it displays on a line above the effective PS
    > prompt
    > #
    > $h = h
    > $p = ""
    > if ($h -eq $null)
    > {$p = 'PS ' + "$pwd" + "`n"}
    > else
    > {
    > if ( $h[$h.length-1].CommandLine -eq $globalrompt_last_command )
    > {$p = 'PS ' + "$pwd" + "`n"}
    > }
    > $p + 'PS> '
    > if ($h -eq $null)
    > {$globalrompt_last_command = ""}
    > else
    > {$globalrompt_last_command = $h[$h.length-1].CommandLine}
    > }
    >
    > - Larry

      My System SpecsSystem Spec

  3. #3


    Larry__Weiss Guest

    Re: customized prompt function

    Thanks! That substitution makes it work like I wanted it to.
    Now, if I want to see the current directory, I just hit the Enter key.
    Otherwise, the PS prompt stays minimal.

    Function prompt {
    # PS prompt displays $pwd only when either
    # 1) current command is just whitespace
    # 2) no commands have yet been entered
    # When $pwd is displayed, it displays on a line above the effective PS prompt
    #
    $h = Get-History
    $p = ""
    if ($h -eq $null)
    {$p = 'PS ' + "$pwd" + "`n"}
    else
    {
    if ( $h[$h.length-1].Id -eq $globalrompt_last_command_Id )
    {$p = 'PS ' + "$pwd" + "`n"}
    }
    $p + 'PS> '
    if ($h -eq $null)
    {$globalrompt_last_Id = ""}
    else
    {$globalrompt_last_command_Id = $h[$h.length-1].Id}
    }

    - Larry

    Andrew Savinykh wrote:

    > What if you try comparing IDs instead of CommandLines?
    >
    >
    > Larry__Weiss wrote:

    >> Please take a look at my code for a customized PS prompt below.
    >> I'd like to figure out how to eliminate #2 and only display $pwd
    >> when only whitespace is entered as a command.
    >>
    >> And also, this is my first "real" PS code, so comments regarding
    >> technique or even style would be appreciated.
    >>
    >> All lines below indented two spaces so wrap can be detected:
    >> Function prompt {
    >> # PS prompt displays $pwd only when either of any of these is true
    >> # #1 current command is just whitespace
    >> # #2 last command is exactly the same as current command
    >> # #3 no commands have yet been entered
    >> # When $pwd is displayed, it displays on a line above the effective PS prompt
    >> #
    >> $h = h
    >> $p = ""
    >> if ($h -eq $null)
    >> {$p = 'PS ' + "$pwd" + "`n"}
    >> else
    >> {
    >> if ( $h[$h.length-1].CommandLine -eq $globalrompt_last_command )
    >> {$p = 'PS ' + "$pwd" + "`n"}
    >> }
    >> $p + 'PS> '
    >> if ($h -eq $null)
    >> {$globalrompt_last_command = ""}
    >> else
    >> {$globalrompt_last_command = $h[$h.length-1].CommandLine}
    >> }
    >>
    >> - Larry

      My System SpecsSystem Spec

  4. #4


    Andrew Savinykh Guest

    Re: customized prompt function

    By the way, a handy shortcut: you can write $h[-1] instead of
    $h[$h.length-1]

    Larry__Weiss wrote:

    > Thanks! That substitution makes it work like I wanted it to.
    > Now, if I want to see the current directory, I just hit the Enter key.
    > Otherwise, the PS prompt stays minimal.
    >
    > Function prompt {
    > # PS prompt displays $pwd only when either
    > # 1) current command is just whitespace
    > # 2) no commands have yet been entered
    > # When $pwd is displayed, it displays on a line above the effective PS
    > prompt
    > #
    > $h = Get-History
    > $p = ""
    > if ($h -eq $null)
    > {$p = 'PS ' + "$pwd" + "`n"}
    > else
    > {
    > if ( $h[$h.length-1].Id -eq $globalrompt_last_command_Id )
    > {$p = 'PS ' + "$pwd" + "`n"}
    > }
    > $p + 'PS> '
    > if ($h -eq $null)
    > {$globalrompt_last_Id = ""}
    > else
    > {$globalrompt_last_command_Id = $h[$h.length-1].Id}
    > }
    >
    > - Larry
    >
    > Andrew Savinykh wrote:

    >> What if you try comparing IDs instead of CommandLines?
    >>
    >>
    >> Larry__Weiss wrote:

    >>> Please take a look at my code for a customized PS prompt below.
    >>> I'd like to figure out how to eliminate #2 and only display $pwd
    >>> when only whitespace is entered as a command.
    >>>
    >>> And also, this is my first "real" PS code, so comments regarding
    >>> technique or even style would be appreciated.
    >>>
    >>> All lines below indented two spaces so wrap can be detected:
    >>> Function prompt {
    >>> # PS prompt displays $pwd only when either of any of these is true
    >>> # #1 current command is just whitespace
    >>> # #2 last command is exactly the same as current command
    >>> # #3 no commands have yet been entered
    >>> # When $pwd is displayed, it displays on a line above the effective
    >>> PS prompt
    >>> #
    >>> $h = h
    >>> $p = ""
    >>> if ($h -eq $null)
    >>> {$p = 'PS ' + "$pwd" + "`n"}
    >>> else
    >>> {
    >>> if ( $h[$h.length-1].CommandLine -eq
    >>> $globalrompt_last_command )
    >>> {$p = 'PS ' + "$pwd" + "`n"}
    >>> }
    >>> $p + 'PS> '
    >>> if ($h -eq $null)
    >>> {$globalrompt_last_command = ""}
    >>> else
    >>> {$globalrompt_last_command = $h[$h.length-1].CommandLine}
    >>> }
    >>>
    >>> - Larry

      My System SpecsSystem Spec

  5. #5


    Larry__Weiss Guest

    Re: customized prompt function

    Thanks! I've now coded it that way.

    I'm also finding that I need to do
    $h = (Get-History)
    to create an array in the case where there is only one command in the session
    history.

    - Larry


    Andrew Savinykh wrote:

    > By the way, a handy shortcut: you can write $h[-1] instead of
    > $h[$h.length-1]
    >
    > Larry__Weiss wrote:

    >> Thanks! That substitution makes it work like I wanted it to.
    >> Now, if I want to see the current directory, I just hit the Enter key.
    >> Otherwise, the PS prompt stays minimal.
    >>
    >> Function prompt {
    >> # PS prompt displays $pwd only when either
    >> # 1) current command is just whitespace
    >> # 2) no commands have yet been entered
    >> # When $pwd is displayed, it displays on a line above the PS prompt
    >> #
    >> $h = Get-History
    >> $p = ""
    >> if ($h -eq $null)
    >> {$p = 'PS ' + "$pwd" + "`n"}
    >> else
    >> {
    >> if ( $h[$h.length-1].Id -eq $globalrompt_last_command_Id )
    >> {$p = 'PS ' + "$pwd" + "`n"}
    >> }
    >> $p + 'PS> '
    >> if ($h -eq $null)
    >> {$globalrompt_last_Id = ""}
    >> else
    >> {$globalrompt_last_command_Id = $h[$h.length-1].Id}
    >> }
    >>
    >> - Larry
    >>
    >> Andrew Savinykh wrote:

    >>> What if you try comparing IDs instead of CommandLines?
    >>>

      My System SpecsSystem Spec

  6. #6


    Larry__Weiss Guest

    Re: customized prompt function

    And now I find I have to use

    $h = @(Get-History)

    instead to make sure $h is an array.

    - Larry


    Larry__Weiss wrote:

    > Thanks! I've now coded it that way.
    >
    > I'm also finding that I need to do
    > $h = (Get-History)
    > to create an array in the case where there is only one command in the
    > session history.
    >
    >
    > Andrew Savinykh wrote:

    >> By the way, a handy shortcut: you can write $h[-1] instead of
    >> $h[$h.length-1]
    >>
    >> Larry__Weiss wrote:

    >>> Thanks! That substitution makes it work like I wanted it to.
    >>> Now, if I want to see the current directory, I just hit the Enter key.
    >>> Otherwise, the PS prompt stays minimal.
    >>>
    >>> Function prompt {
    >>> # PS prompt displays $pwd only when either
    >>> # 1) current command is just whitespace
    >>> # 2) no commands have yet been entered
    >>> # When $pwd is displayed, it displays on a line above the PS prompt
    >>> #
    >>> $h = Get-History
    >>> $p = ""
    >>> if ($h -eq $null)
    >>> {$p = 'PS ' + "$pwd" + "`n"}
    >>> else
    >>> {
    >>> if ( $h[$h.length-1].Id -eq $globalrompt_last_command_Id )
    >>> {$p = 'PS ' + "$pwd" + "`n"}
    >>> }
    >>> $p + 'PS> '
    >>> if ($h -eq $null)
    >>> {$globalrompt_last_Id = ""}
    >>> else
    >>> {$globalrompt_last_command_Id = $h[$h.length-1].Id}
    >>> }
    >>>
    >>> - Larry
    >>>
    >>> Andrew Savinykh wrote:
    >>>> What if you try comparing IDs instead of CommandLines?
    >>>>

      My System SpecsSystem Spec

  7. #7


    Alex K. Angelopoulos Guest

    Re: customized prompt function

    Want to repost the latest version of the function? : )

    "Larry__Weiss" <lfw@xxxxxx> wrote in message
    news:#wp8ubVEKHA.4220@xxxxxx

    > And now I find I have to use
    >
    > $h = @(Get-History)
    >
    > instead to make sure $h is an array.
    >
    > - Larry
    >
    >
    > Larry__Weiss wrote:

    >> Thanks! I've now coded it that way.
    >>
    >> I'm also finding that I need to do
    >> $h = (Get-History)
    >> to create an array in the case where there is only one command in the
    >> session history.
    >>
    >>
    >> Andrew Savinykh wrote:

    >>> By the way, a handy shortcut: you can write $h[-1] instead of
    >>> $h[$h.length-1]
    >>>
    >>> Larry__Weiss wrote:
    >>>> Thanks! That substitution makes it work like I wanted it to.
    >>>> Now, if I want to see the current directory, I just hit the Enter key.
    >>>> Otherwise, the PS prompt stays minimal.
    >>>>
    >>>> Function prompt {
    >>>> # PS prompt displays $pwd only when either
    >>>> # 1) current command is just whitespace
    >>>> # 2) no commands have yet been entered
    >>>> # When $pwd is displayed, it displays on a line above the PS prompt
    >>>> #
    >>>> $h = Get-History
    >>>> $p = ""
    >>>> if ($h -eq $null)
    >>>> {$p = 'PS ' + "$pwd" + "`n"}
    >>>> else
    >>>> {
    >>>> if ( $h[$h.length-1].Id -eq $globalrompt_last_command_Id )
    >>>> {$p = 'PS ' + "$pwd" + "`n"}
    >>>> }
    >>>> $p + 'PS> '
    >>>> if ($h -eq $null)
    >>>> {$globalrompt_last_Id = ""}
    >>>> else
    >>>> {$globalrompt_last_command_Id = $h[$h.length-1].Id}
    >>>> }
    >>>>
    >>>> - Larry
    >>>>
    >>>> Andrew Savinykh wrote:
    >>>>> What if you try comparing IDs instead of CommandLines?
    >>>>>

      My System SpecsSystem Spec

  8. #8


    Larry__Weiss Guest

    Re: customized prompt function

    Sure! It has evolved some more:

    function prompt {
    # PS prompt displays $pwd only when last command is cd or Set-Location.
    # When $pwd is displayed, it displays on a line above the prompt.
    # The host window title always displays $pwd.
    #
    $host.ui.rawui.WindowTitle = "PS $pwd"
    $p = 'PS ' + (([string] $pwd).split('\'))[0] + '> '
    if (@(Get-History).count -eq 0) {return $p}
    $s = ((Get-History -count 1|Select-Object -expandproperty
    commandline).split(' '))[0]
    switch ($s) {
    'cd' {return "PS $pwd`n" + $p}
    'Set-Location' {return "PS $pwd`n" + $p}
    default {return $p}
    }
    }


    - Larry

    Alex K. Angelopoulos wrote:

    > Want to repost the latest version of the function? : )
    >
    > "Larry__Weiss" <lfw@xxxxxx> wrote in message
    > news:#wp8ubVEKHA.4220@xxxxxx

    >> And now I find I have to use
    >> $h = @(Get-History)
    >> instead to make sure $h is an array.
    >>
    >>
    >> Larry__Weiss wrote:

    >>> Thanks! I've now coded it that way.
    >>>
    >>> I'm also finding that I need to do
    >>> $h = (Get-History)
    >>> to create an array in the case where there is only one command in the
    >>> session history.
    >>>
    >>> Andrew Savinykh wrote:
    >>>> By the way, a handy shortcut: you can write $h[-1] instead of
    >>>> $h[$h.length-1]
    >>>>
    >>>> Larry__Weiss wrote:
    >>>>> Thanks! That substitution makes it work like I wanted it to.
    >>>>> Now, if I want to see the current directory, I just hit the Enter key.
    >>>>> Otherwise, the PS prompt stays minimal.
    >>>>>
    >>>>> Function prompt {
    >>>>> # PS prompt displays $pwd only when either
    >>>>> # 1) current command is just whitespace
    >>>>> # 2) no commands have yet been entered
    >>>>> # When $pwd is displayed, it displays on a line above the PS prompt
    >>>>> #
    >>>>> $h = Get-History
    >>>>> $p = ""
    >>>>> if ($h -eq $null)
    >>>>> {$p = 'PS ' + "$pwd" + "`n"}
    >>>>> else
    >>>>> {
    >>>>> if ( $h[$h.length-1].Id -eq $globalrompt_last_command_Id )
    >>>>> {$p = 'PS ' + "$pwd" + "`n"}
    >>>>> }
    >>>>> $p + 'PS> '
    >>>>> if ($h -eq $null)
    >>>>> {$globalrompt_last_Id = ""}
    >>>>> else
    >>>>> {$globalrompt_last_command_Id = $h[$h.length-1].Id}
    >>>>> }
    >>>>>
    >>>>> - Larry
    >>>>>
    >>>>> Andrew Savinykh wrote:
    >>>>>> What if you try comparing IDs instead of CommandLines?
    >>>>>>

      My System SpecsSystem Spec

customized prompt function problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Change to C:\ Prompt from C:\Users\username> Prompt cyberbiker General Discussion 2 27 Nov 2009
A near prompt function I created... James Gentile PowerShell 7 19 Sep 2009
prompt function and the last command entered Larry__Weiss PowerShell 5 31 Jul 2009
Customized Vista WIM Matt Vista installation & setup 1 28 Jan 2008
Prompt Function Poll Keith Hill [MVP] PowerShell 9 27 Nov 2006