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

contributor-book/plugins #1734

Open
jesper-olsen opened this issue Jan 5, 2025 · 1 comment
Open

contributor-book/plugins #1734

jesper-olsen opened this issue Jan 5, 2025 · 1 comment

Comments

@jesper-olsen
Copy link
Contributor

This page is not up to date - I read it recently for the first time and tried to follow the examples:
https://www.nushell.sh/contributor-book/plugins.html#creating-a-plugin-in-rust

The examples reference v. 0.90.2 and nushell is now at (0.101.1) - I think there have been a lot of changes to the plugin interface, in particular the streaming interface.

For SimplePluginCommand, it is enough to rename fn usage() to fn description() - then it compiles and seems to work ok. For PluginCommand() it is more complicated.

In section "under the hood" there is this example:

Putting that together, it looks like this:

$ ./target/release/nu_plugin_len --stdio
json{"Hello":{"protocol":"nu-plugin","version":"0.90.2","features":[]}}
{"Hello":{"protocol":"nu-plugin","version":"0.90.2","features":[]}}
{"Call":[0,"Signature"]}
{"CallResponse":[0, {"Signature":[{"sig":{"name":"len","usage":"calculates the length of its input","extra_usage":"","search_terms":[],"required_positional":[],"optional_positional":[],"rest_positional":null,"vectorizes_over_list":false,"named":[{"long":"help","short":"h","arg":null,"required":false,"desc":"Display the help message for this command","var_id":null,"default_value":null}],"input_type":"String","output_type":"Int","input_output_types":[],"allow_variants_without_examples":false,"is_filter":false,"creates_scope":false,"allows_unknown_args":false,"category":"Default"},"examples":[]}]}]}

I does work - with the SimplePluginCommand implementation - but I think it should be re-written using piped input. Input and output in this version are interleaved and it is not possible - at least in my terminal - to interactively input the example; Plugin waits for enter and input terminates after enter.

There is also this example in the section:

$ echo '{"Hello":{"protocol":"nu-plugin","version":"0.90.2","features":[]}}{"Call":[0,{"Run":{"name":"len","call":{"head":{"start":100953,"end":100957},"positional":[],"named":[]},"input":{"Value":{"String":{"val":"hello","span":{"start":100953,"end":100957}}}}}}]}' | target/release/nu_plugin_len --stdio
json{"Hello":{"protocol":"nu-plugin","version":"0.90.2","features":[]}}
{"PipelineData":{"Value":{"Int":{"val":5,"span":{"start":100953,"end":100957}}}}}

It doesn't work as listed any more and in any case, it would be good to have an explanation of where 100953 comes from.
I guess 100953 and 100957 are indexes into the input buffer - beginning and end of "hello". But why are the indexes so large?
Are they stable over different shell sessions?

@fdncred
Copy link
Collaborator

fdncred commented Jan 5, 2025

We'd accept a PR for these changes.

It doesn't work as listed any more and in any case, it would be good to have an explanation of where 100953 comes from.
I guess 100953 and 100957 are indexes into the input buffer - beginning and end of "hello". But why are the indexes so large?

These are span offsets and the indexes grow as files and repl is read into memory. You can see an example of these with view files

Are they stable over different shell sessions?

I wouldn't rely on it being stable over different sessions. If you change config files or files you source, the offsets will be different. Or if you run a command initially after you start the repl and then 20 commands later, the offsets will be different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants