Add support for PowerShell (#65)

This commit is contained in:
Jason Shirk 2020-05-05 23:03:06 -07:00 committed by GitHub
parent 12c6ce0076
commit 5cdd8fd599
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 108 additions and 4 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
### Added
- Support for PowerShell.
### Removed
- Backward compatibility with `v0.2.x` databases.

View File

@ -15,6 +15,7 @@ A faster way to navigate your filesystem
- [bash](#bash)
- [fish](#fish)
- [POSIX](#posix-shells)
- [PowerShell](#powershell)
- [zsh](#zsh)
- [Configuration](#configuration)
- [`init` flags](#init-flags)
@ -118,6 +119,17 @@ Add the following line to your `~/.zshrc`:
eval "$(zoxide init zsh)"
```
#### PowerShell
Add the following line to your profile:
```powershell
Invoke-Expression (& {
$hook = if ($PSVersionTable.PSVersion.Major -lt 6) { 'prompt' } else { 'pwd' }
(zoxide init --hook $hook powershell) -join "`n"
})
```
## Configuration
### `init` flags

View File

@ -43,6 +43,7 @@ impl Init {
Shell::bash => shell::bash::CONFIG,
Shell::fish => shell::fish::CONFIG,
Shell::posix => shell::posix::CONFIG,
Shell::powershell => shell::powershell::CONFIG,
Shell::zsh => shell::zsh::CONFIG,
};
@ -77,6 +78,7 @@ arg_enum! {
bash,
fish,
posix,
powershell,
zsh,
}
}

View File

@ -13,7 +13,7 @@ pub const CONFIG: ShellConfig = ShellConfig {
},
};
fn z(z_cmd: &str) -> String {
fn z(cmd: &str) -> String {
format!(
r#"
function _z_cd
@ -52,11 +52,11 @@ function {}
end
end
"#,
z_cmd
cmd
)
}
fn alias(z_cmd: &str) -> String {
fn alias(cmd: &str) -> String {
format!(
r#"
abbr -a {0}i '{0} -i'
@ -69,7 +69,7 @@ abbr -a {0}qi 'zoxide query -i'
abbr -a {0}r 'zoxide remove'
abbr -a {0}ri 'zoxide remove -i'
"#,
z_cmd
cmd
)
}

View File

@ -1,6 +1,7 @@
pub mod bash;
pub mod fish;
pub mod posix;
pub mod powershell;
pub mod zsh;
use anyhow::Result;

View File

@ -0,0 +1,85 @@
use super::{HookConfig, ShellConfig};
use anyhow::Result;
use std::borrow::Cow;
pub const CONFIG: ShellConfig = ShellConfig {
z,
alias,
hook: HookConfig {
prompt: HOOK_PROMPT,
pwd: hook_pwd,
},
};
fn z(cmd: &str) -> String {
format!(
r#"
function {} {{
function z_cd($dir) {{
Set-Location $dir -ea Stop
if ($env:_ZO_ECHO -eq "1") {{
Write-Host "$PWD"
}}
}}
if ($args.Length -eq 0) {{
z_cd ~
}}
elseif ($args.Length -eq 1 -and $args[0] -eq '-') {{
z_cd -
}}
else {{
$result = zoxide query @args
if ($LASTEXITCODE -eq 0 -and $result -is [string] -and (Test-Path $result)) {{
z_cd $result
}} else {{
$result
}}
}}
}}
"#,
cmd
)
}
fn alias(cmd: &str) -> String {
format!(
r#"
function {0}i {{ {0} -i @args }}
function {0}a {{ zoxide add @args }}
function {0}q {{ zoxide query @args }}
function {0}qi {{ zoxide query -i @args }}
function {0}r {{ zoxide remove @args }}
function {0}ri {{ zoxide remove -i @args }}
"#,
cmd
)
}
const HOOK_PROMPT: &str = r#"
$PreZoxidePrompt = $function:prompt
function prompt {
$null = zoxide add
& $PreZoxidePrompt
}
"#;
const fn hook_pwd() -> Result<Cow<'static, str>> {
const HOOK_PWD: &str = r#"
if ($PSVersionTable.PSVersion.Major -ge 6) {
$ExecutionContext.InvokeCommand.LocationChangedAction = {
$null = zoxide add
}
} else {
Write-Error "pwd hook requires pwsh - use 'zoxide init powershell --hook prompt'"
}
"#;
Ok(Cow::Borrowed(HOOK_PWD))
}