Skip to content

Commit

Permalink
Support Nushell 0.32.0 (ajeetdsouza#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeetdsouza authored Jun 2, 2021
1 parent fcdfb19 commit 36f3967
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 36 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Auto-generated shell completions.
- `zoxide query --all` for listing deleted directories.
- Lazy deletion for removed directories that have not been accessed in > 90 days.
- Nushell: support for 0.32.0+.

### Fixed

- Nushell: avoid calling `__zoxide_hook` on non-filesystem subshells.
- `alias cd=z` now works on Fish, but it must be done after calling `zoxide init`.
- Fish: `alias cd=z` now works, but it must be done after calling `zoxide init`.
- PowerShell: avoid calling `__zoxide_hook` on non-filesystem providers.
- Fish: avoid calling `__zoxide_hook` in private mode.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Add this to your configuration (usually `~/.config/fish/config.fish`):
zoxide init fish | source
```

#### `nushell`
#### `nushell 0.32.0+`

Initialize zoxide's Nushell script:

Expand Down
4 changes: 1 addition & 3 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ impl<'file> Database<'file> {
let rank = curr_dir.rank;
let last_accessed = curr_dir.last_accessed;
let next_dir = &mut self.dirs[idx - 1];
if next_dir.last_accessed < last_accessed {
next_dir.last_accessed = last_accessed;
}
next_dir.last_accessed = next_dir.last_accessed.max(last_accessed);
next_dir.rank += rank;

// Delete curr_dir.
Expand Down
1 change: 1 addition & 0 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ mod tests {
.stderr("");
}

#[ignore]
#[rstest]
fn nushell_nushell(
#[values(None, Some("z"))] cmd: Option<&str>,
Expand Down
52 changes: 21 additions & 31 deletions templates/nushell.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{%- let section = "# =============================================================================\n#" -%}
{%- let not_configured = "# -- not configured --" -%}
{%- let newline = "{{$(char newline)}}" -%}

{{ section }}
# Utility functions for zoxide.
#

# Default prompt for Nushell.
# Taken from: <https://github.com/nushell/nushell/blob/main/docs/sample_config/config.toml>
def __zoxide_prompt [] {
build-string $(ansi gb) $(pwd) $(ansi reset) '(' $(ansi cb) $(do -i { git rev-parse --abbrev-ref HEAD } | str trim) $(ansi reset) ')' $(ansi yb) $(date format '%m/%d/%Y %I:%M:%S%.3f %p') $(ansi reset) '> '
let git = $"(do -i {git rev-parse --abbrev-ref HEAD} | str trim)"
let git = (if (echo $git | str length) == 0 {
""
} {
build-string (char lparen) (ansi cb) $git (ansi reset) (char rparen)
})
build-string (ansi gb) (pwd) (ansi reset) $git "> "
}

{{ section }}
Expand All @@ -28,7 +32,7 @@ def __zoxide_hook [] {

{%- when InitHook::Pwd %}
def __zoxide_hook [] {}
echo `zoxide: PWD hooks are not supported on Nushell.{{ newline }} Use 'zoxide init nushell --hook prompt' instead.{{ newline }}`
$"zoxide: PWD hooks are not supported on Nushell.(char nl)Use 'zoxide init nushell --hook prompt' instead.(char nl)"
{%- endmatch %}

{{ section }}
Expand All @@ -38,34 +42,21 @@ echo `zoxide: PWD hooks are not supported on Nushell.{{ newline }} Use 'z

# Jump to a directory using only keywords.
def __zoxide_z [...rest:string] {
let args = $(echo $rest | skip 1);
if $(shells | where active == $true | get name) != filesystem {
if $(echo $args | length) > 1 {
echo `zoxide: can only jump directories on filesystem{{ newline }}`
if (shells | where active == $true | get name) != filesystem {
if (echo $rest | length) > 1 {
$"zoxide: can only jump directories on filesystem(char nl)"
} {
cd $(echo $args)
cd (echo $rest)
{%- if echo %}
pwd
{%- endif %}
}
} {
if $(echo $args | length) == 0 {
cd ~
let arg0 = (echo $rest | append '~' | first 1);
if (echo $rest | length) <= 1 && ($arg0 == '-' || (echo $arg0 | path expand | path exists)) {
cd $arg0
} {
if $(echo $args | length) == 1 {
let arg0 = $(echo $args | first 1);
if $arg0 == '-' {
cd -
} {
if $(echo $arg0 | path exists) {
cd $arg0
} {
cd $(zoxide query --exclude $(pwd) -- $args | str trim)
}
}
} {
cd $(zoxide query --exclude $(pwd) -- $args | str trim)
}
cd (zoxide query --exclude (pwd) -- $rest | str trim)
}
{%- if echo %}
pwd
Expand All @@ -75,11 +66,10 @@ def __zoxide_z [...rest:string] {

# Jump to a directory using interactive search.
def __zoxide_zi [...rest:string] {
if $(shells | where active == $true | get name) != filesystem {
echo `zoxide: can only jump directories on filesystem{{ newline }}`
if (shells | where active == $true | get name) != filesystem {
$"zoxide: can only jump directories on filesystem(char nl)"
} {
let args = $(echo $rest | skip 1)
cd $(zoxide query -i -- $args | str trim)
cd (zoxide query -i -- $rest | str trim)
{%- if echo %}
pwd
{%- endif %}
Expand All @@ -93,8 +83,8 @@ def __zoxide_zi [...rest:string] {
{%- match cmd %}
{%- when Some with (cmd) %}

alias {{cmd}} = __zoxide_z ''
alias {{cmd}}i = __zoxide_zi ''
alias {{cmd}} = __zoxide_z
alias {{cmd}}i = __zoxide_zi

{%- when None %}

Expand Down

0 comments on commit 36f3967

Please sign in to comment.