Code Monkey home page Code Monkey logo

Comments (24)

vors avatar vors commented on August 23, 2024

Interesting, thank you for the detailed analyze Keith!
Maybe a slightly less special-casing could be & (Get-Module posh-git) {[scriptblock]::Create(...)} ? That should also evaluate the prompt in the context of posh-git, but

I'd recommend also opening a corresponding issue in posh-git.

from zlocation.

dahlbyk avatar dahlbyk commented on August 23, 2024

I'm watching this here; we can make a posh-git issue when there's a more concrete direction.

I'm certainly not opposed to making posh-git play more nicely with other modules that modify prompt. In this case, it may be as simple as matching a prompt that ends with the default prompt and pulling anything that's been prepended into a ScriptBlock we can execute as part of posh-git's prompt.

On the ZLocation side, it would definitely be helpful to avoid stepping on an existing customized prompt function, either by checking against the default prompt or by checking $prompt.module.

https://github.com/dahlbyk/posh-git/blob/220c06f5b5685e9da381f3c6611d2afb104abc8e/src/posh-git.psm1#L21-L28

https://github.com/dahlbyk/posh-git/blob/220c06f5b5685e9da381f3c6611d2afb104abc8e/src/posh-git.psm1#L125-L129

from zlocation.

vors avatar vors commented on August 23, 2024

@dahlbyk thank you for chiming in!

Here is how ZLocation currently modifies the prompt:

function Register-PromptHook

As you can see this is pretty harmless in itself, it simply adds a call to a function that's publicly exported into the user session (from the same ZLocation module).

As far as I understand from Keith's investigation (correct me if I'm wrong) posh-git calls the private functions and hence relays on the fact that prompt [scriptblock] got created in the module scope.

My proposition about posh-git change would be alternating the prompt registration in a way that doesn't rely on the fact that prompt is created in the posh-git module scope. That would allow the prompt to be later re-created from anywhere. Practically it could be implemented in different ways, i.e. exporting the functions used in the prompt as public module members.

It would have other side benefits, like allowing people to customize their prompt by using posh-git primitives as building blocks.

from zlocation.

dahlbyk avatar dahlbyk commented on August 23, 2024

The downside is posh-git exporting functions that aren't meant for public consumption. Instead of prepending Update-ZLocation to the existing prompt definition, have you calling the old prompt inside yours? Something like…

 function Register-PromptHook 
 { 
     param() 
  
     $oldPrompt = function:\prompt 
     if( $oldPrompt.Definition -notlike '*Update-ZLocation*' ) 
     { 
         $newPrompt = @' 
 Update-ZLocation $pwd 
  
 & $oldPrompt
 '@
         Set-Content -Force -Path function:\prompt -Value ([ScriptBlock]::Create($newPrompt)) 
     } 
 } 

I really don't know if this will properly create a closure over $oldPrompt, but it seems like there should be a way?

(Please pardon typos from my phone.)

from zlocation.

dahlbyk avatar dahlbyk commented on August 23, 2024

On the other hand… what if the posh-git actual prompt was just (effectively):

function prompt {
  & $GitPromptScriptBlock
}

from zlocation.

rkeithhill avatar rkeithhill commented on August 23, 2024

Well, what do you know? Look what I found in PS Core 6.1!

function Register-PromptHook
{
    param()

    if (($PSVersionTable.PSVersion.Major -ge 7) -or (($PSVersionTable.PSVersion.Major -eq 6) -and ($PSVersionTable.PSVersion.Minor -ge 1))) {
        $prevHandler = $ExecutionContext.InvokeCommand.LocationChangedAction
        $ExecutionContext.InvokeCommand.LocationChangedAction = {
            param($sender, $eventArgs)
            Update-ZLocation $eventArgs.NewPath
            if ($prevHandler) {
                $prevHandler.Invoke($sender, $eventArgs)
            }
        }.GetNewClosure()

        return
    }
    …
}

Works a treat! Now just need to figure out best approach for PS < 6.1.

from zlocation.

vors avatar vors commented on August 23, 2024

Great finding!

from zlocation.

vors avatar vors commented on August 23, 2024

@rkeithhill is it fully fixed by #66 ?

from zlocation.

rkeithhill avatar rkeithhill commented on August 23, 2024

As long as you import zlocation after posh-git, yes, it should be fixed. If you import zlocation first, posh-git will see that the prompt has been modified by the user and will not modify it.

from zlocation.

inputfalken avatar inputfalken commented on August 23, 2024

I'm using PS 6.1 and import posh-git (V1.0.0) before ZLocation (V1.1.0) But I still do not have a git prompt.
I'm also using PSReadLine (V2.0.0)

Any idea what i can do?

from zlocation.

rkeithhill avatar rkeithhill commented on August 23, 2024

When you run this command - gcm prompt | % definition - what output do you get? If things are working right, you should get:

            Update-ZLocation $pwd
            ZLocationOrigPrompt

And gcm ZlocationOrigPrompt | % definition should spit out something like:

    $settings = $global:GitPromptSettings
    if (!$settings) {
        return "<`$GitPromptSettings not found> "
    }

    if ($settings.DefaultPromptEnableTiming) {
        $sw = [System.Diagnostics.Stopwatch]::StartNew()
    }

    $origLastExitCode = $global:LASTEXITCODE
…

from zlocation.

inputfalken avatar inputfalken commented on August 23, 2024

I get the following

gcm prompt | % definition


            Update-ZLocation $pwd
            ZLocationOrigPrompt

gcm ZlocationOrigPrompt | % definition

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
# .Link
# https://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

from zlocation.

rkeithhill avatar rkeithhill commented on August 23, 2024

Hmm, so the ZlocationOrigPrompt is the default prompt. If you are importing posh-git before zlocation then this shouldn't be happening unless for some reason posh-git is changing the prompt function.

Try this. Start pwsh with -noprofile and then execute Import-Module posh-git. Does that modify the prompt to show Git status?

from zlocation.

inputfalken avatar inputfalken commented on August 23, 2024

Yes it modifies my prompt.

from zlocation.

rkeithhill avatar rkeithhill commented on August 23, 2024

And then if, in the same session, you execute Import-Module ZLocation the Git prompt goes away?

from zlocation.

inputfalken avatar inputfalken commented on August 23, 2024

It stays with Import-Module ZLocation as well. 🤔

from zlocation.

rkeithhill avatar rkeithhill commented on August 23, 2024

Wait, what? If the Git prompt stays, then it is working, right?

from zlocation.

inputfalken avatar inputfalken commented on August 23, 2024

Well it's gone when I load my profile.

My profile can be found here: https://github.com/inputfalken/dotfiles/blob/master/powershell/Microsoft.PowerShell_profile.ps1

I have not updated it with zlocation. But otherwise it's about the same as my local file.

I have skipped importing the other plugins without success.

from zlocation.

rkeithhill avatar rkeithhill commented on August 23, 2024

Do you have anything in your other profile files?

$profile | fl -force


AllUsersAllHosts       : C:\Program Files\PowerShell\6\profile.ps1
AllUsersCurrentHost    : C:\Program Files\PowerShell\6\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    : C:\Users\Keith\Documents\PowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\Keith\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Length                 : 68

from zlocation.

inputfalken avatar inputfalken commented on August 23, 2024

Thanks for your help and time 👍

That's a very good question 🙂.

I currently do not have access to my computer. I can check it tommorow.

from zlocation.

inputfalken avatar inputfalken commented on August 23, 2024

Thank you so much C:\Users\{user}\Documents\PowerShell\profile.ps1 also imported ZLocation 👍 .

from zlocation.

inputfalken avatar inputfalken commented on August 23, 2024

Which one of AllUsersAllHosts or AllUsersCurrentHost is best practice to have my profile script in?

from zlocation.

rkeithhill avatar rkeithhill commented on August 23, 2024

I rarely used AllUsers*. I put stuff that I'm confident will run in any host in addition to the console host e.g. ISE, VSCode's PowerShell extension, etc inside CurrentUserAllHosts. Anything that is host specific, like registering VSCode editor commands, goes in the host-specific profile (CurrentUserCurrentHost) which is Microsoft.VSCode_profile.ps1 for the PS integrated console in VSCode.

from zlocation.

inputfalken avatar inputfalken commented on August 23, 2024

Oh ye I meant those two 😜 thanks for your input.

from zlocation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.