


# Change working dir in powershell to last dir in lf on exit.
#
# You need to put this file to a folder in $ENV:PATH variable.
#
# You may also like to assign a key to this command:
#
#     Set-PSReadLineKeyHandler -Chord Ctrl+o -ScriptBlock {
#         [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
#         [Microsoft.PowerShell.PSConsoleReadLine]::Insert('lfcd.ps1')
#         [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
#     }
#
# You may put this in one of the profiles found in $PROFILE.

function lfcd {
    $tmp = [System.IO.Path]::GetTempFileName()
    ~\AppData\Local\Microsoft\WindowsApps\lf.exe -last-dir-path="$tmp" $args
    if (Test-Path -PathType Leaf "$tmp")
    {
        $dir = Get-Content "$tmp"
        Remove-Item -Force "$tmp"
        if (Test-Path -PathType Container "$dir")
        {
            if ("$dir" -ne "$pwd")
            {
                Set-Location "$dir"
            }
        }
    }
}

Set-Alias lf lfcd
