Comments (24)
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.
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
.
from zlocation.
@dahlbyk thank you for chiming in!
Here is how ZLocation currently modifies the prompt:
ZLocation/ZLocation/ZLocation.psm1
Line 45 in 2ccc541
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.
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.
On the other hand… what if the posh-git actual prompt
was just (effectively):
function prompt {
& $GitPromptScriptBlock
}
from zlocation.
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.
Great finding!
from zlocation.
@rkeithhill is it fully fixed by #66 ?
from zlocation.
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.
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.
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.
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.
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.
Yes it modifies my prompt.
from zlocation.
And then if, in the same session, you execute Import-Module ZLocation
the Git prompt goes away?
from zlocation.
It stays with Import-Module ZLocation
as well.
from zlocation.
Wait, what? If the Git prompt stays, then it is working, right?
from zlocation.
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.
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.
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.
Thank you so much C:\Users\{user}\Documents\PowerShell\profile.ps1
also imported ZLocation
from zlocation.
Which one of AllUsersAllHosts
or AllUsersCurrentHost
is best practice to have my profile script in?
from zlocation.
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.
Oh ye I meant those two
from zlocation.
Related Issues (20)
- Windows detection bug in Get-FrequentFolders HOT 2
- Error on new install of 1.4.1 HOT 2
- It would be great to see a list of most recent HOT 2
- [Feature Request] Allow installation via Chocolatey
- Set-ZLocation and Update-ZLocation break for PS 7 if the user has defined an alias for 'find' HOT 1
- How to exclude a directory from tracking? HOT 1
- Exception calling "Add" with "2" argument(s) - issue after updating powershell to 7.1 HOT 2
- [Feature Request] z partial/path/to/dir? HOT 1
- Doesn't work with posh-git's error reporting (DollarQuestion) HOT 3
- how can I unsinstall it?thanks. HOT 1
- Database has duplicate entries differing in case only
- Cannot z to network drives HOT 1
- Invoke-ZLocation doesn't work with oh-my-posh + posh-git + ZLocation HOT 2
- Question: possible to prefer child directories? HOT 1
- [Feature request] only display directories with tab completion HOT 5
- Duplicate Unc Paths in Database Where Only One Is Updated
- ZLocation not working as intended. HOT 6
- profile file flood
- Can't jump a folder with `[` or `]` in it
- Add option to customize db location HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zlocation.