kaeru 0.1.0

Reopens your terminal tabs, and the coding agent sessions in them, after the terminal closes or the computer restarts.
# kaeru (https://github.com/gloomydumber/kaeru) keeps track of this tab, so
# that it can come back after the terminal closes. Your profile loads this
# with: Invoke-Expression (& kaeru init powershell | Out-String)

if ($Host.Name -eq 'ConsoleHost' -and -not [Console]::IsInputRedirected -and
    $null -eq $global:__kaeru_cwd_file) {
    $global:__kaeru_cwd_file = ''
    $global:__kaeru_ask = $false
    $__kaeru_encoding = [Console]::OutputEncoding
    try {
        # kaeru prints a path, which can hold any character, as UTF-8.
        [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)
        $global:__kaeru_cwd_file = [string] (& kaeru shell start --pid $PID | Select-Object -Last 1)
        # Exit code 3: tabs are waiting, and this tab is the one that asks.
        $global:__kaeru_ask = $LASTEXITCODE -eq 3
    } catch {
    } finally {
        [Console]::OutputEncoding = $__kaeru_encoding
        Remove-Variable -Name __kaeru_encoding
    }
    if ($global:__kaeru_cwd_file) {
        # The prompt writes the folder to the tab's .cwd file when it changes,
        # without starting a process.
        $global:__kaeru_last_cwd = ''
        $global:__kaeru_prompt = $function:prompt
        function global:prompt {
            # Prompt themes show whether the last command failed, from $?,
            # which this function's own statements would reset.
            $__kaeru_succeeded = $?
            if ($global:__kaeru_ask) {
                # The picker waits for the user, so it shows at the first
                # prompt and not while the profile runs, which PowerShell times
                # and reports when it takes long. kaeru runs as a process of
                # its own, with the console as its input and output, because
                # what a command in this function prints becomes the prompt.
                $global:__kaeru_ask = $false
                try {
                    $kaeru = Get-Command -Name kaeru -CommandType Application -ErrorAction Stop |
                        Select-Object -First 1
                    $start = [System.Diagnostics.ProcessStartInfo]::new($kaeru.Source)
                    $start.UseShellExecute = $false
                    $start.EnvironmentVariables['KAERU_NEW_TAB'] = '1'
                    $picker = [System.Diagnostics.Process]::Start($start)
                    $picker.WaitForExit()
                    # Exit code 4: every tab came back in this window, so this
                    # new tab, which only showed the picker, is one too many.
                    if ($picker.ExitCode -eq 4) {
                        [System.Environment]::Exit(0)
                    }
                } catch {
                }
            }
            try {
                $location = $ExecutionContext.SessionState.Path.CurrentLocation
                if ($location.Provider.Name -eq 'FileSystem' -and
                    $location.ProviderPath -ne $global:__kaeru_last_cwd) {
                    [System.IO.File]::WriteAllText($global:__kaeru_cwd_file, $location.ProviderPath)
                    $global:__kaeru_last_cwd = $location.ProviderPath
                }
            } catch {
            }
            if (-not $__kaeru_succeeded) {
                # Sets $? back to false, without adding to $Error.
                Write-Error -Message 'kaeru' -ErrorAction Ignore
            }
            & $global:__kaeru_prompt
        }
    }
}