Improve error handling and portability

This commit is contained in:
Jason Shirk 2020-05-05 22:52:51 -07:00
parent e58442cef2
commit bc5c04b41c
1 changed files with 10 additions and 9 deletions

View File

@ -18,13 +18,10 @@ fn z(cmd: &str) -> String {
r#" r#"
function {} {{ function {} {{
function z_cd($dir) {{ function z_cd($dir) {{
try {{
Set-Location $dir -ea Stop Set-Location $dir -ea Stop
if ($env:_ZO_ECHO -eq "1") {{ if ($env:_ZO_ECHO -eq "1") {{
Write-Host "$PWD" Write-Host "$PWD"
}} }}
}} catch {{
}}
}} }}
if ($args.Length -eq 0) {{ if ($args.Length -eq 0) {{
@ -35,7 +32,7 @@ function {} {{
}} }}
else {{ else {{
$result = zoxide query @args $result = zoxide query @args
if ($LASTEXITCODE -eq 0 -and ((Test-Path $result) -eq $true)) {{ if ($LASTEXITCODE -eq 0 -and $result -is [string] -and (Test-Path $result)) {{
z_cd $result z_cd $result
}} else {{ }} else {{
$result $result
@ -74,9 +71,13 @@ function prompt {
const fn hook_pwd() -> Result<Cow<'static, str>> { const fn hook_pwd() -> Result<Cow<'static, str>> {
const HOOK_PWD: &str = r#" const HOOK_PWD: &str = r#"
if ($PSVersionTable.PSVersion.Major -ge 6) {
$ExecutionContext.InvokeCommand.LocationChangedAction = { $ExecutionContext.InvokeCommand.LocationChangedAction = {
$null = zoxide add $null = zoxide add
} }
} else {
Write-Error "pwd hook requires pwsh - use 'zoxide init powershell --hook prompt'"
}
"#; "#;
Ok(Cow::Borrowed(HOOK_PWD)) Ok(Cow::Borrowed(HOOK_PWD))