Add support for PowerShell

This commit is contained in:
Jason Shirk 2020-04-10 22:12:00 -07:00
parent 12c6ce0076
commit be1aeed756
5 changed files with 101 additions and 0 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

@ -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,82 @@
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(z_cmd: &str) -> String {
format!(
r#"
function {} {{
function z_cd($dir) {{
try {{
Set-Location $dir -ea Stop
if ($env:_ZO_ECHO -eq "1") {{
Write-Host "$PWD"
}}
}} catch {{
}}
}}
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) {{
z_cd $result
}}
}}
}}
"#,
z_cmd
)
}
fn alias(z_cmd: &str) -> String {
format!(
r#"
function zi {{ {} -i @args }}
function za {{ zoxide add @args }}
function zq {{ zoxide query @args }}
function zqi {{ zoxide query -i @args }}
function zr {{ zoxide remove @args }}
function zri {{ zoxide remove -i @args }}
"#,
z_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#"
$ExecutionContext.InvokeCommand.LocationChangedAction = {
$null = zoxide add
}
"#;
Ok(Cow::Borrowed(HOOK_PWD))
}