-
Notifications
You must be signed in to change notification settings - Fork 755
Add nushell completion for __zoxide_z #1129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
32ed3ec
bdedc2a
79f5f66
9ae8519
b106d38
b8844cd
be220c9
59155c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,101 +1,134 @@ | ||||||
| {%- let section = "# =============================================================================\n#" -%} | ||||||
| {%- let section = "# =============================================================================" -%} | ||||||
| {%- let not_configured = "# -- not configured --" -%} | ||||||
|
|
||||||
| # Code generated by zoxide. DO NOT EDIT. | ||||||
|
|
||||||
| {{ section }} | ||||||
| # Hook configuration for zoxide. | ||||||
| # | ||||||
| module zoxide_integration { | ||||||
| # From version 0.110.0, $nu.home-path has been renamed to $nu.home-dir | ||||||
| const homedir = if $nu.home-dir? != null { $nu.home-dir } else { $nu.home-path } | ||||||
|
|
||||||
| {% if hook == InitHook::None -%} | ||||||
| {{ not_configured }} | ||||||
|
|
||||||
| {%- else -%} | ||||||
| # Initialize hook to add new entries to the database. | ||||||
| export-env { | ||||||
| {%- if hook == InitHook::Prompt %} | ||||||
| $env.config = ( | ||||||
| $env.config? | ||||||
| | default {} | ||||||
| | upsert hooks { default {} } | ||||||
| | upsert hooks.pre_prompt { default [] } | ||||||
| ) | ||||||
| let __zoxide_hooked = ( | ||||||
| $env.config.hooks.pre_prompt | any { try { get __zoxide_hook } catch { false } } | ||||||
| ) | ||||||
| if not $__zoxide_hooked { | ||||||
| $env.config.hooks.pre_prompt = ($env.config.hooks.pre_prompt | append { | ||||||
| __zoxide_hook: true, | ||||||
| code: {|| ^zoxide add -- $env.PWD} | ||||||
| }) | ||||||
| } | ||||||
| {%- else if hook == InitHook::Pwd %} | ||||||
| $env.config = ( | ||||||
| $env.config? | ||||||
| | default {} | ||||||
| | upsert hooks { default {} } | ||||||
| | upsert hooks.env_change { default {} } | ||||||
| | upsert hooks.env_change.PWD { default [] } | ||||||
| ) | ||||||
| let __zoxide_hooked = ( | ||||||
| $env.config.hooks.env_change.PWD | any { try { get __zoxide_hook } catch { false } } | ||||||
| ) | ||||||
| if not $__zoxide_hooked { | ||||||
| $env.config.hooks.env_change.PWD = ($env.config.hooks.env_change.PWD | append { | ||||||
| __zoxide_hook: true, | ||||||
| code: {|_, dir| ^zoxide add -- $dir} | ||||||
| }) | ||||||
| {{ section }} | ||||||
| # | ||||||
| # Hook configuration for zoxide. | ||||||
| # | ||||||
|
|
||||||
| {% if hook == InitHook::None -%} | ||||||
| {{ not_configured }} | ||||||
|
|
||||||
| {%- else -%} | ||||||
| # Initialize hook to add new entries to the database. | ||||||
| export-env { | ||||||
| {%- if hook == InitHook::Prompt %} | ||||||
| let __zoxide_hooked = ( | ||||||
| $env.config.hooks.pre_prompt | ||||||
| | any { get __zoxide_hook? | default false } | ||||||
| ) | ||||||
| if not $__zoxide_hooked { | ||||||
| $env.config.hooks.pre_prompt = ($env.config.hooks.pre_prompt | append { | ||||||
| __zoxide_hook: true, | ||||||
| code: {|| ^zoxide add -- $env.PWD} | ||||||
| }) | ||||||
| } | ||||||
| {%- else if hook == InitHook::Pwd %} | ||||||
| let __zoxide_hooked = ( | ||||||
| $env.config.hooks.env_change.PWD? | default [] | ||||||
| | any { get __zoxide_hook? | default false } | ||||||
| ) | ||||||
| if not $__zoxide_hooked { | ||||||
| $env.config.hooks.env_change.PWD = ($env.config.hooks.env_change.PWD? | append { | ||||||
| __zoxide_hook: true, | ||||||
| code: {|_, dir| ^zoxide add -- $dir} | ||||||
| }) | ||||||
| } | ||||||
| {%- endif %} | ||||||
| } | ||||||
| {%- endif %} | ||||||
| } | ||||||
|
|
||||||
| {%- endif %} | ||||||
| {%- endif %} | ||||||
|
|
||||||
| {{ section }} | ||||||
| # When using zoxide with --no-cmd, alias these internal functions as desired. | ||||||
| # | ||||||
| {{ section }} | ||||||
| # | ||||||
| # Completion for __zoxide_z | ||||||
| # | ||||||
|
|
||||||
| def "nu-complete __zoxide_z" [context: string] { | ||||||
| let ast = ast --flatten $context | skip 1 | ||||||
|
|
||||||
| # If the user has typed a space after the first argument, use the custom | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you look at the next few lines, I compare the output of if $ast.0.span.end >= ($context | str length) { return null } |
||||||
| # completer. If not, use the built-in directory completion. | ||||||
| if ($ast | is-empty) { return null } | ||||||
| if $ast.0.span.end >= ($context | str length) { return null } | ||||||
|
|
||||||
| let completions = ^zoxide query --exclude $env.PWD --list -- ...$ast.content | ||||||
| | lines | each { | ||||||
| if $in starts-with $"($homedir)(char psep)" { | ||||||
| str replace $homedir "~" | ||||||
| } else {} | ||||||
| } | wrap display_override | ||||||
| | insert value { get display_override | debug --raw-value } | ||||||
| | insert span { start: ($ast | first).span.start, end: ($ast | last).span.end } | ||||||
|
|
||||||
| # Jump to a directory using only keywords. | ||||||
| def --env --wrapped __zoxide_z [...rest: string] { | ||||||
| let path = match $rest { | ||||||
| [] => {'~'}, | ||||||
| [ '-' ] => {'-'}, | ||||||
| [ $arg ] if ($arg | path expand | path type) == 'dir' => {$arg} | ||||||
| _ => { | ||||||
| ^zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n" | ||||||
| { | ||||||
| options: { | ||||||
Juhan280 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| sort: false, | ||||||
Juhan280 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| filter: false, | ||||||
| case_sensitive: false, | ||||||
| completion_algorithm: "substring", | ||||||
| } | ||||||
| completions: $completions, | ||||||
| } | ||||||
| } | ||||||
| cd $path | ||||||
| {%- if echo %} | ||||||
| echo $env.PWD | ||||||
| {%- endif %} | ||||||
| } | ||||||
|
|
||||||
| # Jump to a directory using interactive search. | ||||||
| def --env --wrapped __zoxide_zi [...rest:string] { | ||||||
| cd $'(^zoxide query --interactive -- ...$rest | str trim -r -c "\n")' | ||||||
| {%- if echo %} | ||||||
| echo $env.PWD | ||||||
| {%- endif %} | ||||||
| } | ||||||
| {{ section }} | ||||||
| # | ||||||
| # When using zoxide with --no-cmd, alias these internal functions as desired. | ||||||
| # | ||||||
|
|
||||||
| {{ section }} | ||||||
| # Commands for zoxide. Disable these using --no-cmd. | ||||||
| # | ||||||
| # Jump to a directory using only keywords. | ||||||
| export def --env --wrapped __zoxide_z [...rest: directory@"nu-complete __zoxide_z"] { | ||||||
| let path = match $rest { | ||||||
| [] => {'~'}, | ||||||
| [ '-' ] => {'-'}, | ||||||
| [ $arg ] if ($arg | path expand | path type) == 'dir' => {$arg} | ||||||
| _ => { | ||||||
| ^zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n" | ||||||
| } | ||||||
| } | ||||||
| cd $path | ||||||
| {%- if echo %} | ||||||
| echo $env.PWD | ||||||
| {%- endif %} | ||||||
| } | ||||||
|
|
||||||
| {%- match cmd %} | ||||||
| {%- when Some with (cmd) %} | ||||||
| # Jump to a directory using interactive search. | ||||||
| export def --env --wrapped __zoxide_zi [...rest: string] { | ||||||
| cd $'(^zoxide query --interactive -- ...$rest | str trim -r -c "\n")' | ||||||
| {%- if echo %} | ||||||
| echo $env.PWD | ||||||
| {%- endif %} | ||||||
| } | ||||||
|
|
||||||
| {{ section }} | ||||||
| # | ||||||
| # Commands for zoxide. Disable these using --no-cmd. | ||||||
| # | ||||||
|
|
||||||
| {%- match cmd %} | ||||||
| {%- when Some with (cmd) %} | ||||||
|
|
||||||
| alias {{cmd}} = __zoxide_z | ||||||
| alias {{cmd}}i = __zoxide_zi | ||||||
| export alias {{cmd}} = __zoxide_z | ||||||
| export alias {{cmd}}i = __zoxide_zi | ||||||
|
|
||||||
| {%- when None %} | ||||||
| {%- when None %} | ||||||
|
|
||||||
| {{ not_configured }} | ||||||
| {{ not_configured }} | ||||||
|
|
||||||
| {%- endmatch %} | ||||||
| {%- endmatch %} | ||||||
| } | ||||||
|
|
||||||
| export use zoxide_integration * | ||||||
|
|
||||||
| {{ section }} | ||||||
| # | ||||||
| # Add this to your env file (find it by running `$nu.env-path` in Nushell): | ||||||
| # | ||||||
| # zoxide init nushell | save -f ~/.zoxide.nu | ||||||
|
|
@@ -105,4 +138,4 @@ alias {{cmd}}i = __zoxide_zi | |||||
| # | ||||||
| # source ~/.zoxide.nu | ||||||
| # | ||||||
| # Note: zoxide only supports Nushell v0.89.0+. | ||||||
| # Note: zoxide only supports Nushell v0.106.0+. | ||||||
Uh oh!
There was an error while loading. Please reload this page.