# 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
}
# kaeru resume prints the code that resumes the session, and this runs it
# here, as if you typed it, so that no kaeru process waits while the
# session runs. A kaeru that waits keeps its file in use, which an update
# can't replace on Windows. Other commands run kaeru as they are.
function global:kaeru {
$kaeru = Get-Command -Name kaeru -CommandType Application -ErrorAction Stop |
Select-Object -First 1
if ($args.Count -ne 1 -or $args[0] -ne 'resume') {
& $kaeru.Source @args
return
}
$encoding = [Console]::OutputEncoding
try {
# kaeru prints the folder, which can hold any character, as UTF-8.
[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)
$code = (& $kaeru.Source resume --shell powershell) -join "`n"
} finally {
[Console]::OutputEncoding = $encoding
}
if ($code) {
Invoke-Expression -Command $code
}
}
if ($global:__kaeru_cwd_file) {
# The prompt writes the folder to the tab's .cwd file when it changes,
# and touches the file at every prompt, 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
} else {
# The file's time says when the prompt last showed, so
# that kaeru knows an agent that ran in this tab ended.
[System.IO.File]::SetLastWriteTimeUtc($global:__kaeru_cwd_file, [DateTime]::UtcNow)
}
} catch {
}
if (-not $__kaeru_succeeded) {
# Sets $? back to false, without adding to $Error.
Write-Error -Message 'kaeru' -ErrorAction Ignore
}
& $global:__kaeru_prompt
}
}
}