Skip to content

Commit

Permalink
One attempt to exporting a prompt function.
Browse files Browse the repository at this point in the history
Fix #217.  This would only export the prompt function if the prompt function is still set to the default.
  • Loading branch information
rkeithhill committed Jan 3, 2017
1 parent 480a302 commit 8908f89
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 30 deletions.
24 changes: 24 additions & 0 deletions GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,30 @@ $PoshGitVcsPrompt = {
Write-GitStatus $GitStatus
}

# A simple prompt that displays Git status summary info when inside of a Git repo.
function prompt {
$origLastExitCode = $LASTEXITCODE

# A UNC path has no drive so it's better to use the ProviderPath e.g. "\\server\share".
# However for any path with a drive defined, it's better to use the Path property.
# In this case, ProviderPath is "\LocalMachine\My"" whereas Path is "Cert:\LocalMachine\My".
# The latter is more desirable.
$pathInfo = $ExecutionContext.SessionState.Path.CurrentLocation
$curPath = if ($pathInfo.Drive) { $pathInfo.Path } else { $pathInfo.ProviderPath }
if ($curPath -and $curPath.ToLower().StartsWith($Home.ToLower()))
{
$curPath = "~" + $curPath.SubString($Home.Length)
}

Write-Host $curPath -NoNewline

# Write the Git status summary information to the host.
Write-VcsStatus

$global:LASTEXITCODE = $origLastExitCode
"> "
}

# Install handler for removal/unload of the module
$Global:VcsPromptStatuses += $PoshGitVcsPrompt
$ExecutionContext.SessionState.Module.OnRemove = {
Expand Down
35 changes: 19 additions & 16 deletions posh-git.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,25 @@ Description = 'A PowerShell environment for Git'
PowerShellVersion = '2.0'

# Functions to export from this module
FunctionsToExport = @('Invoke-NullCoalescing',
'Write-GitStatus',
'Write-Prompt',
'Write-VcsStatus',
'Get-GitStatus',
'Enable-GitColors',
'Get-GitDirectory',
'TabExpansion',
'Get-AliasPattern',
'Get-SshAgent',
'Start-SshAgent',
'Stop-SshAgent',
'Add-SshKey',
'Get-SshPath',
'Update-AllBranches',
'tgit')
FunctionsToExport = @(
'Invoke-NullCoalescing',
'Write-GitStatus',
'Write-Prompt',
'Write-VcsStatus',
'Get-GitStatus',
'Enable-GitColors',
'Get-GitDirectory',
'TabExpansion',
'Get-AliasPattern',
'Get-SshAgent',
'Start-SshAgent',
'Stop-SshAgent',
'Add-SshKey',
'Get-SshPath',
'Update-AllBranches',
'prompt',
'tgit'
)

# Cmdlets to export from this module
CmdletsToExport = @()
Expand Down
43 changes: 29 additions & 14 deletions posh-git.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
param([switch]$NoVersionWarn = $false)
param([switch]$NoVersionWarn)

if (Get-Module posh-git) { return }

Expand All @@ -11,26 +11,26 @@ if ($psv.Major -lt 3 -and !$NoVersionWarn) {
"To suppress this warning, change your profile to include 'Import-Module posh-git -Args `$true'.")
}

Push-Location $psScriptRoot
.\CheckVersion.ps1 > $null
& $PSScriptRoot\CheckVersion.ps1 > $null

. .\Utils.ps1
. .\GitUtils.ps1
. .\GitPrompt.ps1
. .\GitTabExpansion.ps1
. .\TortoiseGit.ps1
Pop-Location
# Get the current prompt function *before* we define a prompt function by dot sourcing GitPrompt.ps1.
$currentPromptDef = if ($funcInfo = Get-Command prompt -ErrorAction SilentlyContinue) { $funcInfo.Definition }

. $PSScriptRoot\Utils.ps1
. $PSScriptRoot\GitUtils.ps1
. $PSScriptRoot\GitPrompt.ps1
. $PSScriptRoot\GitTabExpansion.ps1
. $PSScriptRoot\TortoiseGit.ps1

if (!$Env:HOME) { $Env:HOME = "$Env:HOMEDRIVE$Env:HOMEPATH" }
if (!$Env:HOME) { $Env:HOME = "$Env:USERPROFILE" }

Get-TempEnv 'SSH_AGENT_PID'
Get-TempEnv 'SSH_AUTH_SOCK'

Export-ModuleMember `
-Alias @(
'??') `
-Function @(
$exportModuleParams = @{
Alias = @('??') # TODO: Remove in 1.0.0
Function = @(
'Invoke-NullCoalescing',
'Write-GitStatus',
'Write-Prompt',
Expand All @@ -46,6 +46,21 @@ Export-ModuleMember `
'Add-SshKey',
'Get-SshPath',
'Update-AllBranches',
'tgit')
'tgit'
)
}

# Get the default prompt definition.
if ($psv.Major -eq 2) {
$defaultPromptDef = "`$(if (test-path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) + 'PS ' + `$(Get-Location) + `$(if (`$nestedpromptlevel -ge 1) { '>>' }) + '> '"
}
else {
$defaultPromptDef = [Runspace]::DefaultRunspace.InitialSessionState.Commands['prompt'].Definition
}

# If there is no prompt function or the prompt function is the default, export the posh-git prompt function.
if (!$currentPromptDef -or ($currentPromptDef -eq $defaultPromptDef)) {
$exportModuleParams.Function += 'prompt'
}

Export-ModuleMember @exportModuleParams

0 comments on commit 8908f89

Please sign in to comment.