edit-helper.ps1

#Requires -Version 7.2
<#
.SYNOPSIS
    Runs `zoxide edit <action> [path]` for fzf's reload bindings and prints the raw feed.
.DESCRIPTION
    `zoxide edit` opens fzf with reload bindings (see New-ZoEditFzfCommand). The zoxide binary's
    bindings call `zoxide edit <action> {2..}`, a program on PATH; here `zoxide` is a PowerShell
    alias that fzf's shell cannot see, so the bindings run `pwsh -File` on this script instead.
    -File passes every argument through unquoted and unparsed, which keeps paths with spaces and
    quotes intact. The script ships in the module folder, beside the manifest, and imports the
    module from that manifest by path (never by name from PSModulePath), so it always runs the
    copy it belongs to. It runs the subcommand and writes its output (NUL-terminated feed lines,
    no trailing newline) to stdout as UTF-8 bytes, then exits with the command's exit code.
    Errors reach stderr as `zoxide: ...`, as they would from the binary. The prompt hook and
    aliases are not installed in this short-lived session. The module loader never dot-sources
    this file: it only loads Private/ and Public/.
.EXAMPLE
    pwsh -NoProfile -File src/PSZoxide/edit-helper.ps1 increment /home/user/src
#>

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

$env:_ZO_HOOK = 'none'
$env:_ZO_NO_CMD = '1'
Import-Module (Join-Path $PSScriptRoot 'PSZoxide.psd1')

$output = @(Invoke-Zoxide edit @args)
$code = $global:LASTEXITCODE
$bytes = [Text.UTF8Encoding]::new($false).GetBytes(($output -join ''))
$stdout = [Console]::OpenStandardOutput()
$stdout.Write($bytes, 0, $bytes.Length)
$stdout.Flush()
exit $code