Skip to content
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

Completion support for Nushell #1857

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open

Completion support for Nushell #1857

wants to merge 34 commits into from

Conversation

ayax79
Copy link

@ayax79 ayax79 commented Nov 15, 2022

Completion support for Nushell. Besides the tests included, I have also tested against minikube.

@CLAassistant
Copy link

CLAassistant commented Nov 15, 2022

CLA assistant check
All committers have signed the CLA.

@github-actions
Copy link

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

@ayax79 ayax79 changed the title Completion support for [Nushell](https://nushell.sh) Completion support for Nushell Nov 15, 2022
@marckhouzam
Copy link
Collaborator

Hey @ayax79 this exciting! I have never used nushell but I have now installed it on my Mac.

I see that this PR generates a script containing the completions. However, this is no longer the approach used by Cobra. Instead, the program, through Cobra, provides the completions itself through the hidden __complete command. The nushell script should then call prog __complete <all other args on the command-line> to obtain the list of completions and it should use them as appropriate for nushell. You can test out the __complete command manually by doing things like prog __complete '' or prog __complete -- and such.

This approach means that we have centralized all the logic to generate completions and coded it in Go (in Cobra's completions.go file). Then Cobra generates a shell script for each supported shell which uses the __complete command to get the completions.

Following this approach would be your first step. You can look at the completion scripts for any of the other shells to see how this is done. These scripts are in files named for the shell they support.

Eventually, the nushell script should also support the ShellCompDirectives, which you will notice in the other scripts.

Feel free to ask for more info if needed.

@ayax79
Copy link
Author

ayax79 commented Nov 16, 2022

Hey @marckhouzam,

Thanks for looking at this and thanks for the feedback!

Nushell completions quite a bit differently than completions in other shells I have used (zsh). nushell_completions.go doesn't generate a script for the completions, it generates something more akin to a header file. each "export extern " maps to an external, non-nushell command. An example would be export extern "minikube start" It is telling nushell about the 'minikube start' sub-command and describing the parameters it accepts.

For example, see the minikube-completions.nu I generated.
minikube-completions.nu.txt

You can load it into nushell via (added .txt to get it to upload):
use minikube-completions.nu.txt *

I'll dig into the new model, but I would love any implementation advice.

@marckhouzam
Copy link
Collaborator

We had a similar situation with Fish where normal completions are a long list of choices. So in our case we have one generic completion entry which calls a function that calls prog __complete ... and processes the results.

The nushell doc talks about custom completions which seem like an avenue to investigate. For example it shows:

export extern "git push" [
    remote?: string@"nu-complete git remotes"
...

which calls git remotes to get the list of completions.

@marckhouzam
Copy link
Collaborator

marckhouzam commented Nov 16, 2022

An important advantage of Cobra's approach is that using the __complete command in your nushell script will automatically support dynamic completions provided by the program using Cobra (which your solution doesn't). Please see details here: https://github.com/spf13/cobra/blob/main/shell_completions.md#dynamic-completion-of-nouns

@ayax79
Copy link
Author

ayax79 commented Nov 16, 2022

@rsteube the author of (carapace)[https://github.com/rsteube/carapace] chimed in with some good feedback on the nushell discord that I will include too.
As already commented in the issue it is better to take the external_completer (https://www.nushell.sh/blog/2022-09-06-nushell-0_68.html#external-completions-experimental-herlon214-rsteube) approach for this. You can grab some insights from https://github.com/rsteube/carapace/blob/master/internal/shell/nushell/action.go if it's any help. For the meantime i got a custom completer for minikube and kubectl at https://github.com/rsteube/carapace-bin . Completion of cobra based commands can also be bridged if those don't work well for you: https://rsteube.github.io/carapace-bin/spec/bride.html .

@ayax79
Copy link
Author

ayax79 commented Nov 18, 2022

I have played around with a few options and haven't found anything that works well utilizing __command.

This works for only the first level for commands and breaks flags:

def 'nu-complete minikube' [] {
	minikube __command
}

export extern minikube [
	command?: string@'nu-complete minikube'
]

Screenshot_minikube_level0

Broken help flag:
Screenshot_minikube_broken_help

To access a flag you have to bypass nushell via:

^minikube --help

By adding another level:

def 'nu-complete minikube start' [] {
    minikube start __command
}

export extern 'minikube start' [
	command?: string@'nu-complete minikube start'
]

This overrides the completion list and removes the description (unless it is manually added as comment). It also breaks any flags.

The other option is to utilize the external completer support. This however only provides an option for one global fallback completer. This how carapace-bin works with nushell.

I'll poke around more, but it seems like I am may be running out of options.

@marckhouzam
Copy link
Collaborator

Thanks for continuing the investigation @ayax79.

In the above comment you typed __command everywhere, but I assume you meant __complete?
Also be aware that the format is prog __complete <other args>, so the __complete command should always be the first argument to the program.

It is also important to pass the current command-line arguments that the user typed. So, if the user typed minikube start --<tab> to complete flags, the way to get the completions is to call minikube __complete start --.

Finally, if the last character on the command-line before the user pressed TAB is a space, you must pass an extra empty argument. So, if the user typed minikube start <TAB> (notice the space after start) you need to call minikube __complete start "". This complexity allows cobra to tell the difference between the user typing minikube start<TAB> which is request to complete the start argument, versus minikube start <TAB> which is a request to complete the argument after start.

You can find documentation about all that here: https://github.com/spf13/cobra/blob/main/shell_completions.md#debugging

@rsteube
Copy link
Contributor

rsteube commented Nov 18, 2022

Yeah you'll definitely have no success with the extern approach, it won't allow unknown flags.
Sorry if it wasn't clear before, but you can do some nu scripting magic to support mutliple completers - it's just bad in regards to "registering" them.

let external_completer = {|spans| 
  {
    $spans.0: { } # default
    kubectl: { kubectl __complete ($spans | into df | slice 1 1000) | handleOutput }
    minikube: { minikube __complete ($spans | into df | slice 1 1000) | handleOutput }
  } | get $spans.0 | each {|it| do $it}
}

@ayax79
Copy link
Author

ayax79 commented Nov 23, 2022

I finally got some more time to work on this. I also spent some time digging into the cobra completions and the nushell external completions code .

This seems to work, although it needs some more testing. There might be some weirdness around mixing flags and commands:

# Executes a cobra apps __complete cmd with the following span
export def exec_cobra_complete [
  cmd: string,  # The cobra command (e.g. kubectl)
  spans: list,  # The list of spans
 ] {
  # skip the first entry in the span (the command) and join the rest of the span to create __complete args
  let cmd_args = ($spans | skip 1 | str join ' ') 

  # If the last span entry was empty add "" to the end of the command args
  let cmd_args = if ($spans | last | str trim | is-empty) {
    $'($cmd_args) ""'
  } else {
    $cmd_args
  }

  # The full command to be executed
  let full_cmd = $'($cmd) __complete ($cmd_args)'

  # Since nushell doesn't have anything like eval, execute in a subshell
  let result = (do -i { nu -c $"'($full_cmd)'" } | complete)

  # Create a record with all completion related info. 
  # directive and directive_str are for posterity
  let stdout_lines = ($result.stdout | lines)
  {
    completions: ($stdout_lines | drop | lines | parse "{value}\t{description}")
    directive_str: ($result.stderr)
    directive: ($stdout_lines | last)
  }
}

# Execute the __complete command and only return completions
export def get_cobra_completions [
  cmd: string,  # The cobra command (e.g. kubectl)
  spans: list,  # The list of spans
] {
  # return just the completions from the result. 
  (exec_cobra_complete $cmd $spans).completions
}

let external_completer = {|spans|
  {
    $spans.0: { }
    kubectl: { get_cobra_completions 'kubectl' $spans }
    minikube: { get_cobra_completions 'minikube' $spans }
  } | get $spans.0 | each {|it| do $it}
}

As @rsteube mentioned, registration will be a little bit odd. I think that can be documented in commends for the completion generation though. It will require adding the that code block to the nushell config file ($nu.config-path) and updating config.external_completer entry:

let-env config = {
    external_completer: $external_completer
}

One nice thing is that for other cobra commands, only the external_completer block will need to be updated.

@marckhouzam, if this approach looks good, I'll update nushell_completer.go with it.

@marckhouzam
Copy link
Collaborator

Thanks @ayax79.
I was able to test the above script and things look promising.
However, we need to come up with a solution to enable completion for more than one tool using Cobra. Here is where I think it needs some thought.

I'm assuming that each tool, myprogram for example, will generate the above script ending with

let external_completer = {|spans|
  {
    $spans.0: { }
    myprogram: { get_cobra_completions 'myprogram' $spans }
  } | get $spans.0 | each {|it| do $it}
}

The problem is that if I have multiple tools using this, only the last sourced script will work as each one sets the same external_completer variable. What we do in Cobra is prefix each variable and function in the script by <programName>_. In this case the script would set variables such as myprogram_external_completer.

After that, we need to figure out a way to have the one

let-env config = {
    external_completer: $external_completer
}

manage to include all tools that have cobra completions.

@ayax79
Copy link
Author

ayax79 commented Nov 24, 2022

If we took this approach, it would still be possible to wrap the cobra completer and chain completers together -- it would be up to the user to figure how they wanted to handle it.

The configuration itself isn't changeable once the shell is loaded. Nushell works much more like a compiled language. Dynamic loading of resources and execution isn't really possible. The two phase execution approach is part of the reason why nushell is so fast, but it does create some awkwardness. You can see how I got around this, by creating a subshell to execute the command I assembled:

 # Since nushell doesn't have anything like eval, execute in a subshell
 let result = (do -i { nu -c $"'($full_cmd)'" } | complete)

What I really don't like about this approach is that nushell's environment has to load to execute the command (I think, need to verify). It might be feasible to do something kind of like execute completions from a directory, but it could get expensive:

let external_completer = {|spans|
    let completion_command = $"($env.COMPLETION_DIR)/($spans.0).nu ($spans | str join ' ')"
    nu -c $completion_file 
}

Using something like carapace-bin is the best use of external_completers, because carpace itself is plugable.

@marckhouzam
Copy link
Collaborator

Not knowing anything about nushell, I'm having trouble following the subtleties.
If you could describe the steps the user would take to get the completions to work for multiple tool, we could see if it is something that we can move forward with.

If we can reach a solution where any one tool can generate some nushell completion script and the user could add something to their configuration file to setup any number of such scripts, we would have something sufficient.

@ayax79
Copy link
Author

ayax79 commented Nov 26, 2022

I simplified the nushell external configurator to this:

# An external configurator that works with any cobra based
# command line application (e.g. kubectl, minikube)
let cobra_configurator = {|spans| 
  let cmd = $spans.0

  # skip the first entry in the span (the command) and join the rest of the span to create __complete args
  let cmd_args = ($spans | skip 1 | str join ' ') 

  # If the last span entry was empty add "" to the end of the command args
  let cmd_args = if ($spans | last | str trim | is-empty) {
    $'($cmd_args) ""'
  } else {
    $cmd_args
  }

  # The full command to be executed
  let full_cmd = $'($cmd) __complete ($cmd_args)'

  # Since nushell doesn't have anything like eval, execute in a subshell
  let result = (do -i { nu -c $"'($full_cmd)'" } | complete)

  # Create a record with all completion related info. 
  # directive and directive_str are for posterity
  let stdout_lines = ($result.stdout | lines)
  let result = ({
    completions: ($stdout_lines | drop | lines | parse "{value}\t{description}")
    directive_str: ($result.stderr)
    directive: ($stdout_lines | last)
  })

  $result.completions
}

The process would be:

  1. Run <cmd> completion nushell to generate the above block
  2. Add the above block to the $nu.config-path (location of the nushell config file)
  3. Configure the entry external_configurator under config to be $cobra_configurator

There will be no need to change the configuration for any subsequent cobra based app. The above configuration will work with any cobra based app without any further change.

@marckhouzam
Copy link
Collaborator

Wow! After sourcing the script in the latest comment and then doing

let-env config = {external_completer: $cobra_configurator}

I was able to do completion for helm and kubectl!

One question with this approach: are we going to break completion for any non-cobra program that uses the external_completer?

@ayax79 Could you update the PR with the new script and then I can start testing more thoroughly?
Also, there are the shell completion directives we'll have to try to support.

One thing to investigate is that flag value completion does not seem to work:

$ kubectl --namespace [tab]
NO RECORDS FOUND

We should be getting a list of namespaces.

But this is a great start!

@ayax79
Copy link
Author

ayax79 commented Nov 26, 2022

The external completer is a fallback. Completions using externs will load first. If someone wants to use more than one external completer they could write a completer that wraps two:

let chaining_completer = {|spans|
  let completions = (do $cobra_completer $spans)
  let completions = if ($completions | is-empty) {
    (do $other_completer $spans)
  } else {
    $completions
  }
  $completions
}

I'll poke around with the flag completions. It should work the same as anything else.

@ayax79
Copy link
Author

ayax79 commented Nov 26, 2022

There was a bug in the line parsing causing it to drop entries that weren't exactly {value}\t{description}. Anything without a description was getting dropped. I changed to parse via regex. The only thing that might get dropped with this one is values (commands/flags/options) that have particularly exotic characters, I am matching [\w-.:+]. This is the corrected version:

# An external configurator that works with any cobra based
# command line application (e.g. kubectl, minikube)
let cobra_configurator = {|spans| 
 
  let cmd = $spans.0

  # skip the first entry in the span (the command) and join the rest of the span to create __complete args
  let cmd_args = ($spans | skip 1 | str join ' ') 

  # If the last span entry was empty add "" to the end of the command args
  let cmd_args = if ($spans | last | str trim | is-empty) {
    $'($cmd_args) ""'
  } else {
    $cmd_args
  }

  # The full command to be executed
  let full_cmd = $'($cmd) __complete ($cmd_args)'

  # Since nushell doesn't have anything like eval, execute in a subshell
  let result = (do -i { nu -c $"'($full_cmd)'" } | complete)

  # Create a record with all completion related info. 
  # directive and directive_str are for posterity
  let stdout_lines = ($result.stdout | lines)

  let $completions = ($stdout_lines | drop | parse -r '([\w\-\.:\+]*)\t?(.*)' | rename value description)

  let result = ({
    completions: $completions
    directive_str: ($result.stderr)
    directive: ($stdout_lines | last)
  })

  $result.completions
}

I will have an updated pull request shortly.

Copy link
Collaborator

@marckhouzam marckhouzam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great. Here are some things I noticed:

  1. Could you add nushell to the default completion command? For example: https://github.com/spf13/cobra/blob/main/completions.go#L782-L785
  2. The = form of flags does not quite work. When doing kubectl -n=<tab>, the -n= part is removed
  3. Probably linked to the previous point, doing kubectl --namespace=<tab> then selecting a completion choice, and then pressing backpace actually crashes the shell
  4. When a completion is accepted a space should be added after it. For example kubectl comple<tab> should become kubectl completion (with a space after completion) ( except if the ShellCompDirectiveNoSpace is specified)
  5. Would we be able to support the ShellComp directives? Or maybe a subset of them?

    cobra/completions.go

    Lines 54 to 78 in 7bb1440

    const (
    // ShellCompDirectiveError indicates an error occurred and completions should be ignored.
    ShellCompDirectiveError ShellCompDirective = 1 << iota
    // ShellCompDirectiveNoSpace indicates that the shell should not add a space
    // after the completion even if there is a single completion provided.
    ShellCompDirectiveNoSpace
    // ShellCompDirectiveNoFileComp indicates that the shell should not provide
    // file completion even when no completion is provided.
    ShellCompDirectiveNoFileComp
    // ShellCompDirectiveFilterFileExt indicates that the provided completions
    // should be used as file extension filters.
    // For flags, using Command.MarkFlagFilename() and Command.MarkPersistentFlagFilename()
    // is a shortcut to using this directive explicitly. The BashCompFilenameExt
    // annotation can also be used to obtain the same behavior for flags.
    ShellCompDirectiveFilterFileExt
    // ShellCompDirectiveFilterDirs indicates that only directory names should
    // be provided in file completion. To request directory names within another
    // directory, the returned completions should specify the directory within
    // which to search. The BashCompSubdirsInDir annotation can be used to
    // obtain the same behavior but only for flags.
    ShellCompDirectiveFilterDirs
  6. File completion does not seem to work: if there are no completions choices, the shell should suggests file names (except if the ShellCompDirectiveNoFileComp is specified)

nushell_completions.go Outdated Show resolved Hide resolved
nushell_completions.go Outdated Show resolved Hide resolved
nushell_completions.go Outdated Show resolved Hide resolved
shell_completions.md Outdated Show resolved Hide resolved
@github-actions
Copy link

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

1 similar comment
@github-actions
Copy link

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

@ayax79
Copy link
Author

ayax79 commented Nov 27, 2022

I just found a nasty bug.

When running things vim, it locks up because vim interprets vim __complete as a valid command and never returns.

@ayax79
Copy link
Author

ayax79 commented Nov 27, 2022

I think I am going to have to put a whitelist in for commands to execute the completer for:

# A list of cobra that completion will be attempted for.
# Add new apps to this list to enable completion for them.
let cobra_apps = ["kubectl", "minikube"]

# An external configurator that works with any cobra based
# command line application (e.g. kubectl, minikube)
let cobra_completer = {|spans| 
  let cmd = $spans.0

  if not ($cobra_apps | where $it == $cmd | is-empty) {
    let ShellCompDirectiveError = 1
    let ShellCompDirectiveNoSpace = 2
    let ShellCompDirectiveNoFileComp = 4
    let ShellCompDirectiveFilterFileExt = 8
    let ShellCompDirectiveFilterDirs = 16
   
    let last_span = ($spans | last | str trim)

    # skip the first entry in the span (the command) and join the rest of the span to create __complete args
    let cmd_args = ($spans | skip 1 | str join ' ') 

    # If the last span entry was empty add "" to the end of the command args
    let cmd_args = if ($last_span | is-empty) {
      $'($cmd_args) ""'
    } else {
      $cmd_args
    }

    # The full command to be executed with active help disable (Nushell does not support active help)
    let full_cmd = $'($cmd)_ACTIVE_HELP=0 ($cmd) __complete ($cmd_args)'

    # Since nushell doesn't have anything like eval, execute in a subshell
    let result = (do -i { nu -c $"'($full_cmd)'" } | complete)

    # Create a record with all completion related info. 
    # directive and directive_str are for posterity
    let stdout_lines = ($result.stdout | lines)
    let directive = ($stdout_lines | last | str trim | str replace ":" "" | into int)
    let completions = ($stdout_lines | drop | parse -r '([\w\-\.:\+\=]*)\t?(.*)' | rename value description)

    # Add space at the end of each completion
    let completions = if $directive != $ShellCompDirectiveNoSpace {
      ($completions | each {|it| {value: $"($it.value) ", description: $it.description}})
    } else {
      $completions
    }

    if $last_span =~ '=$' {
      # return flag as part of the completion so that it doesn't get replaced
      $completions | each {|it| $"($last_span)($it.value)" }
    } else if $directive == $ShellCompDirectiveNoFileComp {
      # Allow empty results as this will stop file completion
      $completions
    } else if ($completions | is-empty)  or  $directive == $ShellCompDirectiveError {
      # Not returning null causes file completions to break
      # Return null if there are no completions or ShellCompDirectiveError 
      null
    } else {
      $completions
    }
  } else {
    null
  }
}

I don't think it will be too onerous. The workflow will be:

If you haven't setup cobra_completer

  1. edit config.nu
  2. add generated contents
  3. set external_completer = cobra_completer
  4. start new shell

If you have setup cobra_completer

  1. Update the cobra_apps array to contain your app, for example:
let cobra_apps = ["kubectl", "minikube", "helm"]
  1. restart shell

@marckhouzam
Copy link
Collaborator

marckhouzam commented Nov 27, 2022

I think I am going to have to put a whitelist in for commands to execute the completer

For other shells we don't use a "global" completer but instead have to register each program individually. So if kubectl generates the completion script, it also register kubectl (and only kubectl) to use the functions in that script.

If nushell completions don't work like that then your allow-list idea sounds like the right approach. I wonder if we could automate adding a program name to the list in the script itself?

@marckhouzam
Copy link
Collaborator

@rsteube if you have any guidance for us don't hesitate to chime in and correct us.

@github-actions
Copy link

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

@ayax79
Copy link
Author

ayax79 commented Nov 27, 2022

We could certainly automate adding a line like:
let cobra_apps = ($cobra_apps | append "newapp")
to below the initial cobra_apps definition in the config.nu

Copy link
Collaborator

@marckhouzam marckhouzam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Things are moving very nicely @ayax79. You probably didn't know what you were getting into when you first posted the PR 😄 but with a little more effort, we should get this in.

  1. I now get a space after a single completion and the NoFileComp directive is respected. Nice work!

  2. Is it normal the completion does not work if I use a path along with the program?
    For example: kubectl [tab] works but not ~/bin/kubectl [tab]

  3. Aliases work, nice! But...

alias k = kubectl
k [tab]

works, but most probably related to the previous point

alias k = ~/bin/kubectl
k [tab]

doesn't work

  1. The = form of flags is starting to work but not completely.
    For instance kubectl -n=[tab] does work, but not kubectl -n=de[tab]

  2. I'm getting a weird behaviour where no records are found when I think they should:

$ kubectl get ns
NAME              STATUS   AGE
default           Active   23d
kube-system       Active   23d
kube-public       Active   23d
kube-node-lease   Active   23d
$ kubectl get namespaces k[tab]
NO RECORDS FOUND
$ kubectl get namespaces ku[tab]
kube-system
kube-public
kube-node-lease
  1. New (advanced) requirement that I forgot about: the __complete command may
    return completions that don't match the last argument the user typed.
    For example:
$ helm __complete repo remove hello
test1       https://charts.bitnami.com/bitnami
test2   https://charts.bitnami.com/bitnami
test3   https://charts.bitnami.com/bitnami
bitnami https://charts.bitnami.com/bitnami
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

Notice that although the last argument is "hello", the list of completions
does not filter on "hello". This case should be supported by Cobra because
it allows for shells that support it to do fuzzy matching. For example
with zsh and fish if I do helm repo remove nami[tab] the shell will
match nami with bitnami.
This however is causing problems with the current nushell script. So:

$ helm repo remove bitn[tab]
test1
test2
test3
bitnami

Notice that nushell completion still suggests all completions and does not
complete the bitn argument to bitnami.
Therefore it seems the nushell completion script will need to do the filtering by
itself to get rid of all completions that don't match what the user has typed.
The cool thing is that I tested and it seems nushell will support fuzzy matching,
which will be richer than just matching on prefix. For example, nushell will allow:

$ helm repo remove nami[tab]
$ helm repo remove bitnami

The filtering rules in the script should be as follows:
1- try to match on prefix. If any completions start with what the user has typed,
then only those completions should be kept.
2- if no completion choices match on prefix, then we should look for any completions
that contain what the user typed
For example if the list of completions obtained is:

test
mytest
bitnami
origami

if the last argument typed is te[tab] then we should only keep the completion
test because it starts with te (and remove the other 3 completions).
but if the user typed ami[tab], since nothing starts with ami we match both
bitnami and origami which both contain ami.

completions.go Outdated Show resolved Hide resolved
completions.go Outdated Show resolved Hide resolved
completions.go Outdated Show resolved Hide resolved
completions.go Show resolved Hide resolved
shell_completions.md Outdated Show resolved Hide resolved
completions.go Outdated Show resolved Hide resolved
Copy link

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

Copy link

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

Copy link

github-actions bot commented Dec 2, 2024

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

Copy link

github-actions bot commented Dec 2, 2024

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

Copy link

github-actions bot commented Dec 2, 2024

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

@ayax79
Copy link
Author

ayax79 commented Dec 3, 2024

@marckhouzam I believe I have addressed all the feedback. Can you give this another look?

@marckhouzam
Copy link
Collaborator

I just found a nasty bug.

When running things vim, it locks up because vim interprets vim __complete as a valid command and never returns.

@ayax79 I think this bug is still present. Maybe we have to tell the user to use multiple_completers?

@marckhouzam
Copy link
Collaborator

If we have to use multiple_completers can we try again to do it like suggested in this comment: #1857 (comment)

Copy link
Collaborator

@marckhouzam marckhouzam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're getting so close I can taste it!

1- Is there a way to add debug statements to the nushell completion cobra_completer. I want to make sure I can debug this in case there is a bug reported.

I was thinking we could add a function like this, maybe? But I'm having trouble calling it in the completer.

    def debug [message] {
        let file = $env.BASH_COMP_DEBUG_FILE?
        if (not ($file == "")) {
            echo $"($message)\n" | save $file --append
        }
    }

2- I'm hitting different bugs but as long as they are minor, I was thinking we can commit this in "beta level" and keep working on fixing it. However we need to fix the vim <tab> hanging. In the mean time, I'll post a small program and give examples of the small issues I'm noticing.

3- Is there no way in nushell to interrupt the shell completion? I was testing with tanzu and in some cases the tanzu __complete command will trigger an interactive login and will wait there until I login. If I don't login, it will hand there forever. In zsh I can press ^C to interrupt the ongoing completion.

nushell_completions_test.go Outdated Show resolved Hide resolved
site/content/completions/_index.md Outdated Show resolved Hide resolved
completions.go Outdated Show resolved Hide resolved
completions.go Outdated Show resolved Hide resolved
completions.go Outdated Show resolved Hide resolved
completions.go Outdated Show resolved Hide resolved
"os"
)

func (c *Command) GenNushellCompletion(w io.Writer, includeDesc bool) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The includeDesc variable is not used.

What you need to do is simple, if includeDesc == false you want to use __completeNoDesc in the script, instead of __complete.
If we have a single cobra_completer this won't really work as it will affect every program.
Let's see where we end up with having different completers and revisit at that time.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could allow this to be set via an environment variable? Maybe something like this:

$env.COBRA_NO_DESC_COMMANDS = [kubectl minikube]

nushell_completions.go Show resolved Hide resolved
@ayax79
Copy link
Author

ayax79 commented Dec 9, 2024

We're getting so close I can taste it!

1- Is there a way to add debug statements to the nushell completion cobra_completer. I want to make sure I can debug this in case there is a bug reported.

I was thinking we could add a function like this, maybe? But I'm having trouble calling it in the completer.

    def debug [message] {
        let file = $env.BASH_COMP_DEBUG_FILE?
        if (not ($file == "")) {
            echo $"($message)\n" | save $file --append
        }
    }

This is pretty much what I do when debugging.

2- I'm hitting different bugs but as long as they are minor, I was thinking we can commit this in "beta level" and keep working on fixing it. However we need to fix the vim <tab> hanging. In the mean time, I'll post a small program and give examples of the small issues I'm noticing.

Sounds good.

3- Is there no way in nushell to interrupt the shell completion? I was testing with tanzu and in some cases the tanzu __complete command will trigger an interactive login and will wait there until I login. If I don't login, it will hand there forever. In zsh I can press ^C to interrupt the ongoing completion.

I don't think so currently. I can investigate if it is possible to handle this in nushell core. The currently external completer stuff is pretty limited. You can define a block that is handled a list of spans and you can either return a list of records in the format of {value: "foo", description: "description of foo"} or null to no op.

@fdncred, to you have any thoughts?

Copy link

github-actions bot commented Dec 9, 2024

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

@fdncred
Copy link

fdncred commented Dec 9, 2024

I'm not sure. I haven't worked with the completions much. I think the external completer is built in nushell but uses some reedline primitives.

Copy link

github-actions bot commented Dec 9, 2024

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

Copy link

github-actions bot commented Dec 9, 2024

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

@ayax79
Copy link
Author

ayax79 commented Dec 9, 2024

@marckhouzam, I added a logging function.

@marckhouzam
Copy link
Collaborator

3- Is there no way in nushell to interrupt the shell completion? I was testing with tanzu and in some cases the tanzu __complete command will trigger an interactive login and will wait there until I login. If I don't login, it will hand there forever. In zsh I can press ^C to interrupt the ongoing completion.

I don't think so currently. I can investigate if it is possible to handle this in nushell core. The currently external completer stuff is pretty limited. You can define a block that is handled a list of spans and you can either return a list of records in the format of {value: "foo", description: "description of foo"} or null to no op.

Should we put a timeout when running the __complete command in the cobra_completer?
Maybe a timeout of 10 seconds?

@ayax79
Copy link
Author

ayax79 commented Dec 9, 2024

3- Is there no way in nushell to interrupt the shell completion? I was testing with tanzu and in some cases the tanzu __complete command will trigger an interactive login and will wait there until I login. If I don't login, it will hand there forever. In zsh I can press ^C to interrupt the ongoing completion.

I don't think so currently. I can investigate if it is possible to handle this in nushell core. The currently external completer stuff is pretty limited. You can define a block that is handled a list of spans and you can either return a list of records in the format of {value: "foo", description: "description of foo"} or null to no op.

Should we put a timeout when running the __complete command in the cobra_completer? Maybe a timeout of 10 seconds?

There isn't currently a way to handle timeouts on commands. Probably the best approach is to try to make the external completer support respect ^C.

Copy link

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/docs-generation Generation of docs via Cobra area/github For changes to Github specific things not shipped in the library area/shell-completion All shell completions kind/feature A feature request for cobra; new or enhanced behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants