Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of default prompt but with custom side effects in prompt function #501

Closed
felixfbecker opened this issue Nov 2, 2017 · 16 comments
Closed

Comments

@felixfbecker
Copy link

System Details

  • posh-git version/path: 1.0.0.0 pre00 ~/git/posh-git/src
  • PowerShell version: 6.0.0-beta
  • git version 2.13.6 (Apple Git-96)
  • OS: Unix 16.7.0.0 (macOS Sierra)

Issue Description

I want to customize my prompt function so that it checks whether the current directory has an .nvmrc file and switches Node versions depending on that. But I also want to keep the default git prompt. Is that possible?

@rkeithhill
Copy link
Collaborator

In theory you could do something like:

$GitPromptSettings.DefaultPromptPrefix = '$(if (Test-Path -LiteralPath $pwd/.nvmrc -EA 0) { <exec command> *> $null })'

Just keep in mind if any "output" returned by this would get displayed before the default posh-git prompt, hence the redirect to $null. You might notice a bit of slow down in your prompt display in directories containing a .nvmrc file.

@vegar
Copy link

vegar commented Nov 28, 2017

will this be executed more then once though?
I've tried adding a timestamp to the prompt, but the timestamp is evaluated once and then remain the same for the lifetime of the shell...

@rkeithhill
Copy link
Collaborator

Be careful to use single quotes when supplying the string. If you use double quotes, any variables / subexpressions i.e. $() will only get eval'd once.

@rkeithhill
Copy link
Collaborator

rkeithhill commented Jan 1, 2018

@felixfbecker What if we created another global variable (like $GitPromptSettings) called $GitPromptScriptBlock that you could invoke from your own prompt function e.g.:

function prompt {
    # ... script to do stuff like switch node versions
    &$GitPromptScriptBlock
}

I've prototyped this and it works fine in my testing. I might include this in the upcoming 1.0.0 beta. Sure would love to have you test it on macOS.

BTW I wonder if we should call this variable $PoshGitPromptScriptBlock to lessen chances of collision? But if we do that, it kind of implies we should change $GitPromptSettings to $PoshGitPromptSettings. I guess it is unlikely that a user would have two different modules trying to provide Git prompt functionality as that just isn't going to work well.

@dahlbyk
Copy link
Owner

dahlbyk commented Jan 1, 2018

Or, going the other direction, we could add $GitPromptSettings.OnPrompt (better name?) that we'll execute if set. Seems preferable to avoid folks needing to write their own prompt.

@rkeithhill
Copy link
Collaborator

rkeithhill commented Jan 1, 2018

We could actually do both. Allowing users to inject custom script via a ScriptBlock would be handy. But exposing $GitPromptScriptBlock is just a minor tweak i.e. change the variable name slightly and export it. We're already creating the scriptblock. I could see this being handy for more advanced users that want to write their own prompt function but want to invoke posh-git one as well.

@dahlbyk
Copy link
Owner

dahlbyk commented Jan 1, 2018

We're already creating the scriptblock. I could see this being handy for more advanced users that want to write their own prompt function but want to invoke posh-git one as well.

👍

@rkeithhill
Copy link
Collaborator

What if we made this change just for 1.0.0? @felixfbecker is on PS Core, so he'd be covered.

@rkeithhill
Copy link
Collaborator

rkeithhill commented Jan 1, 2018

@felixfbecker Would you be willing to try out the rkeithhill/refactor-write-prompt branch in this repo? Basically git clone the repo, checkout that branch and in your profile import posh-git using the path to where you cloned it e.g.:

function prompt {
    # ... script to do stuff like switch node versions
    if ($GitPromptScriptBlock) { &$GitPromptScriptBlock }
}
import-module ~/github/posh-git/src/posh-git.psd1

I added in the guard just in case you remove the posh-git module.

@felixfbecker
Copy link
Author

I'm curious why it is a dynamic script block and not just a function like Write-GitPrompt?

Checked out the branch to test but struggling with #486

@rkeithhill
Copy link
Collaborator

Because we assign that scriptblock to the prompt function i.e. that's how we "set" the prompt function.

Set-Item Function:\prompt -Value $GitPromptScriptBlock

@rkeithhill
Copy link
Collaborator

RE #486 - try setting:

$global:GitPromptSettings.DefaultPromptSuffix = '`n$(''>'' * ($nestedPromptLevel + 1)) '

That is supposed to get rid of the double prompt. Of course, PSReadline 2.0.0-beta1 is supposed to fix these issues. BTW I had a bear of a time to get my PS Core in WSL/Ubuntu to use the new version of PSReadline. I had to resort to importing it by path.

@felixfbecker
Copy link
Author

So PS Core 6.0.0-rc2 solved the readline issue.
With just this profile:

function prompt {
    &$GitPromptScriptBlock
}
Import-Module ~/git/posh-git/src/posh-git.psd1

I get this prompt:

PowerShell v6.0.0-rc.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

Loading personal and system profiles took 655ms.
/Users/felix> cd ./git/posh-git/
/Users/felix/git/posh-git [rkeithhill/refactor-write-prompt ≡]> 

But if I add:

function prompt {
    Set-NodeVersion
    &$GitPromptScriptBlock
}
Import-Module ~/git/posh-git/src/posh-git.psd1
Import-Module nvm

I get nothing:

PowerShell v6.0.0-rc.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

Loading personal and system profiles took 658ms.
PS>cd ./git/posh-git/
PS>

@rkeithhill
Copy link
Collaborator

Does Set-NodeVersion output anything? You might try Set-NodeVersion *> $null.

@felixfbecker
Copy link
Author

Ah, Set-NodeVersion throws an exception if there is no version given and no .nvmrc found. Weird that the exception is not printed. A try/catch works.

@felixfbecker
Copy link
Author

I think this is therefor solved :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants