Skip to content

Commit

Permalink
Make prompt hook and addition of frequent folders configurable
Browse files Browse the repository at this point in the history
Make the module consume arguments and use this to configure 2 things:
- prompt hook: currently prompt registration only works if no other
module also overrides the prompt function after importing the ZLocation
module. Normally fine, but it can be more convenient for users to just
define their own prompt and call Update-ZLocation in it
- adding frequent folders: this is nice, but it can also be particularly
slow (> 200mSec on an above-average machine) and is not always used
  • Loading branch information
stinos committed Apr 21, 2021
1 parent f0376c9 commit 6c80b10
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,16 @@ If you want to display some additional information about ZLocation on start-up,
Write-Host -Foreground Green "`n[ZLocation] knows about $((Get-ZLocation).Keys.Count) locations.`n"
```

Some features are configurable, these are the defaults:
```powershell
Import-Module ZLocation -ArgumentList @{AddFrequentFolders = $True; RegisterPromptHook = $True}
```
- turn off `AddFrequentFolders` to not add directories from Explorer's QuickAccess to the ZLocation database automatically (this also results in faster loading times)
- turn off `RegisterPromptHook` to not automaticaly hook the prompt, see below

### Note

ZLocation alters your prompt function to track the location. Meaning if you use this module with other modules that modify your prompt function (e.g. `posh-git`), then you'd need to adjust your [Powershell profile file](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7). The statement `Import-Module ZLocation` needs to be placed **after** the other module imports that modify your prompt function.
By default importing ZLocation alters your prompt function to track the location. Meaning if you use this module with other modules that modify your prompt function (e.g. `posh-git`), then you'd need to adjust your [Powershell profile file](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7). The statement `Import-Module ZLocation` needs to be placed **after** the other module imports that modifies your prompt function. As an alternative import the ZLocation module with `RegisterPromptHook=$False` and add a call to `Update-ZLocation $pwd` in your own prompt function.

You can open `profile.ps1` using the below commands:

Expand Down
30 changes: 24 additions & 6 deletions ZLocation/ZLocation.psm1
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
param([parameter(Position=0, Mandatory=$false)] [Hashtable] $ModuleArguments=@{})

Set-StrictMode -Version Latest

# Defaults from before we had arguments.
$defaultArgumentValues = @{
AddFrequentFolders = $True;
RegisterPromptHook = $True;
}
foreach ($item in $defaultArgumentValues.GetEnumerator()) {
if (-not $ModuleArguments.ContainsKey($item.Name)) {
$ModuleArguments[$item.Name] = $item.Value
}
}

# Listing nested modules in .psd1 creates additional scopes and Pester cannot mock cmdlets in those scopes.
# Instead we import them here which works.
Import-Module "$PSScriptRoot\ZLocation.Service.psd1"
Expand Down Expand Up @@ -63,9 +76,11 @@ function Register-PromptHook

# On removal/unload of the module, restore original prompt or LocationChangedAction event handler.
$ExecutionContext.SessionState.Module.OnRemove = {
Copy-Item function:\global:ZlocationOrigPrompt function:\global:prompt
Remove-Item function:\ZlocationOrigPrompt
Remove-Variable ZLocationPromptScriptBlock -Scope Global
if (Test-Path function:\global:ZlocationOrigPrompt) {
Copy-Item function:\global:ZlocationOrigPrompt function:\global:prompt
Remove-Item function:\ZlocationOrigPrompt
Remove-Variable ZLocationPromptScriptBlock -Scope Global
}
}

#
Expand Down Expand Up @@ -252,9 +267,12 @@ function Get-FrequentFolders {
}
}

Get-FrequentFolders | ForEach-Object {Add-ZWeight -Path $_ -Weight 0}

Register-PromptHook
if ($ModuleArguments.AddFrequentFolders) {
Get-FrequentFolders | ForEach-Object {Add-ZWeight -Path $_ -Weight 0}
}
if ($ModuleArguments.RegisterPromptHook) {
Register-PromptHook
}

Set-Alias -Name z -Value Invoke-ZLocation

Expand Down

0 comments on commit 6c80b10

Please sign in to comment.