zoxide/templates/nushell.txt

116 lines
3.2 KiB
Plaintext
Raw Normal View History

2021-04-08 18:35:42 +00:00
{%- let section = "# =============================================================================\n#" -%}
{%- let not_configured = "# -- not configured --" -%}
2021-05-05 23:01:57 +00:00
{%- let newline = "{{$(char newline)}}" -%}
2021-03-31 16:45:43 +00:00
2021-04-27 12:07:38 +00:00
{{ 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) '> '
}
2021-04-08 18:35:42 +00:00
{{ section }}
2021-03-31 16:45:43 +00:00
# Hook configuration for zoxide.
#
# Hook to add new entries to the database.
{%- match hook %}
2021-05-03 21:12:43 +00:00
{%- when InitHook::None %}
2021-03-31 16:45:43 +00:00
def __zoxide_hook [] {}
2021-05-03 21:12:43 +00:00
{%- when InitHook::Prompt %}
2021-03-31 16:45:43 +00:00
def __zoxide_hook [] {
shells | where active == $true && name == filesystem | get path | each { zoxide add -- $it }
2021-03-31 16:45:43 +00:00
}
2021-05-03 21:12:43 +00:00
{%- when InitHook::Pwd %}
2021-03-31 16:45:43 +00:00
def __zoxide_hook [] {}
2021-05-05 23:01:57 +00:00
echo `zoxide: PWD hooks are not supported on Nushell.{{ newline }} Use 'zoxide init nushell --hook prompt' instead.{{ newline }}`
2021-03-31 16:45:43 +00:00
{%- endmatch %}
2021-04-08 18:35:42 +00:00
{{ section }}
2021-03-31 16:45:43 +00:00
# When using zoxide with --no-aliases, alias these internal functions as
# desired.
#
# Jump to a directory using only keywords.
def __zoxide_z [...rest:string] {
2021-05-05 23:01:57 +00:00
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 }}`
} {
cd $(echo $args)
{%- if echo %}
pwd
{%- endif %}
}
2021-03-31 16:45:43 +00:00
} {
2021-05-05 23:01:57 +00:00
if $(echo $args | length) == 0 {
cd ~
} {
if $(echo $args | length) == 1 {
let arg0 = $(echo $args | first 1);
if $arg0 == '-' {
cd -
2021-03-31 16:45:43 +00:00
} {
2021-05-05 23:01:57 +00:00
if $(echo $arg0 | path exists) {
cd $arg0
} {
cd $(zoxide query --exclude $(pwd) -- $args | str trim)
}
2021-03-31 16:45:43 +00:00
}
2021-05-05 23:01:57 +00:00
} {
cd $(zoxide query --exclude $(pwd) -- $args | str trim)
2021-03-31 16:45:43 +00:00
}
}
2021-04-08 18:35:42 +00:00
{%- if echo %}
2021-05-05 23:01:57 +00:00
pwd
2021-04-08 18:35:42 +00:00
{%- endif %}
2021-05-05 23:01:57 +00:00
}
2021-03-31 16:45:43 +00:00
}
# Jump to a directory using interactive search.
def __zoxide_zi [...rest:string] {
2021-05-05 23:01:57 +00:00
if $(shells | where active == $true | get name) != filesystem {
echo `zoxide: can only jump directories on filesystem{{ newline }}`
} {
let args = $(echo $rest | skip 1)
cd $(zoxide query -i -- $args | str trim)
2021-04-08 18:35:42 +00:00
{%- if echo %}
2021-05-05 23:01:57 +00:00
pwd
2021-04-08 18:35:42 +00:00
{%- endif %}
2021-05-05 23:01:57 +00:00
}
2021-03-31 16:45:43 +00:00
}
2021-04-08 18:35:42 +00:00
{{ section }}
2021-03-31 16:45:43 +00:00
# Convenient aliases for zoxide. Disable these using --no-aliases.
#
{%- match cmd %}
{%- when Some with (cmd) %}
alias {{cmd}} = __zoxide_z ''
alias {{cmd}}i = __zoxide_zi ''
{%- when None %}
2021-04-08 18:35:42 +00:00
{{ not_configured }}
2021-03-31 16:45:43 +00:00
{%- endmatch %}
2021-04-08 18:35:42 +00:00
{{ section }}
2021-04-27 12:07:38 +00:00
# To initialize zoxide, first create a Nushell script:
2021-03-31 16:45:43 +00:00
#
# zoxide init nushell --hook prompt | save ~/.zoxide.nu
#
2021-04-27 12:07:38 +00:00
# Add this to your configuration (usually ~/.config/nu/config.toml):
#
# prompt = "__zoxide_hook;__zoxide_prompt"
# startup = ["zoxide init nushell --hook prompt | save ~/.zoxide.nu", "source ~/.zoxide.nu"]
#
# You can replace __zoxide_prompt with a custom prompt.