Public/Invoke-Zoxide.ps1
|
Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' function Invoke-Zoxide { <# .SYNOPSIS Runs zoxide: the same subcommands, output, error text and exit codes as zoxide 0.10.0. .DESCRIPTION A drop-in for the zoxide binary (alias 'zoxide'): add, query and remove work as in the binary; init, edit and import are parsed and then fail until Phase 3. Results go to the pipeline as strings, one per line. Errors go to the host's error stream as 'zoxide: <message>', and $LASTEXITCODE is 0 on success, 1 on an error and 2 on a usage error, like the binary's exit status. Nothing is thrown. This is a simple function on purpose: it declares no parameters, so PowerShell passes '-ls', '-i' and every other token through $args unbound, and a clap-style parser reads them. Numbers become their text, array and other collection arguments are flattened at any depth (a hashtable stays one token), and $null arguments and items are dropped. PowerShell itself removes a bare '--' before any function sees it; quote it ('--') to pass it through. .EXAMPLE zoxide add $HOME/src .EXAMPLE zoxide query -ls --all .LINK https://github.com/ajeetdsouza/zoxide #> # Depth-first, in order: a collection's items go back on the stack in reverse. $tokens = [Collections.Generic.List[string]]::new() $pending = [Collections.Generic.Stack[object]]::new() for ($i = $args.Count - 1; $i -ge 0; $i--) { $pending.Push($args[$i]) } while ($pending.Count) { $item = $pending.Pop() if ($null -eq $item) { continue } if ($item -is [Collections.IEnumerable] -and $item -isnot [string] -and $item -isnot [Collections.IDictionary]) { $children = @($item) for ($i = $children.Count - 1; $i -ge 0; $i--) { $pending.Push($children[$i]) } continue } $tokens.Add([string]$item) } Invoke-ZoMain -Arguments $tokens.ToArray() } Set-Alias -Name zoxide -Value Invoke-Zoxide |