Do not require a pre-existing entry name for PowerShell completions

This commit is contained in:
2024-03-14 18:33:22 -04:00
parent 80e4148b9b
commit 7e5823c35e
+18 -22
View File
@@ -1,27 +1,22 @@
Class libmuttonEntries : System.Management.Automation.IValidateSetValuesGenerator { function MUTNEntryCompleter {
[string[]] GetValidValues() { param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
#$entryPath = (Resolve-Path '~/.local/share/libmutton').Path # UNIX testing $entryPath = (Resolve-Path '~/.local/share/libmutton').Path # UNIX testing
$entryPath = (Resolve-Path '~/AppData/Local/libmutton/entries').Path #$entryPath = (Resolve-Path '~/AppData/Local/libmutton/entries').Path
$entryNames = If (Test-Path $entryPath) {(Get-ChildItem -Path $entryPath -Recurse).FullName.Substring($entryPath.Length) -replace '\\', '/'} $entryNames = If (Test-Path $entryPath) {(Get-ChildItem -Path $entryPath -Recurse).FullName.Substring($entryPath.Length) -replace '\\', '/' -replace ' ', '` '}
return [string[]] $entryNames $entryNames | Where-Object { $_ -like "$wordToComplete*" }
}
} }
function MUTNArgumentCompleter { function MUTNOptionCompleter {
param ( $commandName, param ($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters )
$possibleValues = @{ $possibleValues = @{
add = @('password', 'note', 'folder') add = @('password', 'note', 'folder')
copy = @('password', 'username', 'url', 'note') copy = @('password', 'username', 'url', 'note')
edit = @('password', 'username', 'url', 'note', 'rename') edit = @('password', 'username', 'url', 'note', 'rename')
gen = @('update') gen = @('update')
} }
if ($fakeBoundParameters.ContainsKey('argument')) { if ($fakeBoundParameters.ContainsKey('argument')) {
$possibleValues[$fakeBoundParameters.argument] | Where-Object { $possibleValues[$fakeBoundParameters.argument] | Where-Object {
$_ -like "$wordToComplete*" $_ -like "$wordToComplete*"
} }
@@ -31,10 +26,10 @@ if ($fakeBoundParameters.ContainsKey('argument')) {
} }
function mutn { function mutn {
[CmdletBinding()] [CmdletBinding()]
param ( param (
[Parameter(Mandatory=$false, Position = 0)] [Parameter(Position = 0)]
[ValidateSet([libmuttonEntries])] [ArgumentCompleter({ MUTNEntryCompleter @args })]
[string]$entry, [string]$entry,
[Parameter(Position = 1)] [Parameter(Position = 1)]
@@ -42,9 +37,10 @@ function mutn {
[string]$argument, [string]$argument,
[Parameter(Position = 2)] [Parameter(Position = 2)]
[ArgumentCompleter({ MUTNArgumentCompleter @args })] [ArgumentCompleter({ MUTNOptionCompleter @args })]
[string]$option [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()
} }