From 7e5823c35ee2a2ce9528a3274d27e1f6f3e86ee5 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Thu, 14 Mar 2024 18:33:22 -0400 Subject: [PATCH] Do not require a pre-existing entry name for PowerShell completions --- extra/completion.ps1 | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/extra/completion.ps1 b/extra/completion.ps1 index 9f27dd4..be50bde 100644 --- a/extra/completion.ps1 +++ b/extra/completion.ps1 @@ -1,27 +1,22 @@ -Class libmuttonEntries : System.Management.Automation.IValidateSetValuesGenerator { - [string[]] GetValidValues() { - #$entryPath = (Resolve-Path '~/.local/share/libmutton').Path # UNIX testing - $entryPath = (Resolve-Path '~/AppData/Local/libmutton/entries').Path - $entryNames = If (Test-Path $entryPath) {(Get-ChildItem -Path $entryPath -Recurse).FullName.Substring($entryPath.Length) -replace '\\', '/'} - return [string[]] $entryNames - } +function MUTNEntryCompleter { + param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) + $entryPath = (Resolve-Path '~/.local/share/libmutton').Path # UNIX testing + #$entryPath = (Resolve-Path '~/AppData/Local/libmutton/entries').Path + $entryNames = If (Test-Path $entryPath) {(Get-ChildItem -Path $entryPath -Recurse).FullName.Substring($entryPath.Length) -replace '\\', '/' -replace ' ', '` '} + $entryNames | Where-Object { $_ -like "$wordToComplete*" } } -function MUTNArgumentCompleter { - param ( $commandName, - $parameterName, - $wordToComplete, - $commandAst, - $fakeBoundParameters ) +function MUTNOptionCompleter { + param ($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) -$possibleValues = @{ + $possibleValues = @{ add = @('password', 'note', 'folder') copy = @('password', 'username', 'url', 'note') edit = @('password', 'username', 'url', 'note', 'rename') gen = @('update') } -if ($fakeBoundParameters.ContainsKey('argument')) { + if ($fakeBoundParameters.ContainsKey('argument')) { $possibleValues[$fakeBoundParameters.argument] | Where-Object { $_ -like "$wordToComplete*" } @@ -31,10 +26,10 @@ if ($fakeBoundParameters.ContainsKey('argument')) { } function mutn { -[CmdletBinding()] - param ( - [Parameter(Mandatory=$false, Position = 0)] - [ValidateSet([libmuttonEntries])] + [CmdletBinding()] + param ( + [Parameter(Position = 0)] + [ArgumentCompleter({ MUTNEntryCompleter @args })] [string]$entry, [Parameter(Position = 1)] @@ -42,9 +37,10 @@ function mutn { [string]$argument, [Parameter(Position = 2)] - [ArgumentCompleter({ MUTNArgumentCompleter @args })] + [ArgumentCompleter({ MUTNOptionCompleter @args })] [string]$option ) - #Invoke-Expression -Command ('/usr/local/bin/mutn ' + ($entry -replace ' ', '` '), $argument, ($option -replace ':', '-')).Trim() # UNIX testing - Invoke-Expression -Command ('mutn.exe ' + ($entry -replace ' ', '` '), $argument, $option).Trim() + + Invoke-Expression -Command ('/usr/local/bin/mutn ' + ($entry -replace ' ', '` '), $argument, ($option -replace ':', '-')).Trim() # UNIX testing + #Invoke-Expression -Command ('mutn.exe ' + ($entry -replace ' ', '` '), $argument, $option).Trim() }