From 6c80b10284b9028481b57743026c81425fcc2c49 Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 21 Apr 2021 09:30:00 +0200 Subject: [PATCH] Make prompt hook and addition of frequent folders configurable 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 --- README.md | 9 ++++++++- ZLocation/ZLocation.psm1 | 30 ++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c1f9a65..3232f01 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/ZLocation/ZLocation.psm1 b/ZLocation/ZLocation.psm1 index e99fc24..9619939 100644 --- a/ZLocation/ZLocation.psm1 +++ b/ZLocation/ZLocation.psm1 @@ -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" @@ -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 + } } # @@ -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