nushell/crates
Andy Gayton 7b82c6b482
feat: make ctrlc available to plugins (#13181)
# Description

This PR adds a new method to `EngineInterface`: `register_ctrlc_handler`
which takes a closure to run when the plugin's driving engine receives a
ctrlc-signal. It also adds a mirror of the `signals` attribute from the
main shell `EngineState`.

This is an example of how a plugin which makes a long poll http request
can end the request on ctrlc:
https://github.com/cablehead/nu_plugin_http/blob/main/src/commands/request.rs#L68-L77

To facilitate the feature, a new attribute has been added to
`EngineState`: `ctrlc_handlers`. This is a Vec of closures that will be
run when the engine's process receives a ctrlc signal.

When plugins are added to an `engine_state` during a `merge_delta`, the
engine passes the ctrlc_handlers to the plugin's
`.configure_ctrlc_handler` method, which gives the plugin a chance to
register a handler that sends a ctrlc packet through the
`PluginInterface`, if an instance of the plugin is currently running.

On the plugin side: `EngineInterface` also has a ctrlc_handlers Vec of
closures. Plugin calls can use `register_ctrlc_handler` to register a
closure that will be called in the plugin process when the
PluginInput::Ctrlc command is received.

For future reference these are some alternate places that were
investigated for tying the ctrlc trigger to transmitting a Ctrlc packet
through the `PluginInterface`:

- Directly from `src/signals.rs`: the handler there would need a
reference to the Vec<Arc<RegisteredPlugins>>, which would require us to
wrap the plugins in a Mutex, which we don't want to do.

- have `PersistentPlugin.get_plugin` pass down the engine's
CtrlcHandlers to .get and then to .spawn (if the plugin isn't already
running). Once we have CtrlcHandlers in spawn, we can register a handler
to write directly to PluginInterface. We don't want to double down on
passing engine_state to spawn this way though, as it's unpredictable
because it would depend on whether the plugin has already been spawned
or not.

- pass `ctrlc_handlers` to PersistentPlugin::new so it can store it on
itself so it's available to spawn.

- in `PersistentPlugin.spawn`, create a handler that sends to a clone of
the GC event loop's tx. this has the same issues with regards to how to
get CtrlcHandlers to the spawn method, and is more complicated than a
handler that writes directly to PluginInterface

# User-Facing Changes

No breaking changes

---------

Co-authored-by: Ian Manske <ian.manske@pm.me>
2024-07-30 08:29:18 -05:00
..
nu_plugin_custom_values Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu_plugin_example feat: make ctrlc available to plugins (#13181) 2024-07-30 08:29:18 -05:00
nu_plugin_formats Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu_plugin_gstat Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu_plugin_inc Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu_plugin_nu_example Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu_plugin_polars Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu_plugin_python Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu_plugin_query Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu_plugin_stress_internals Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-cli Keep forward slash when autocomplete on Windows (#13321) 2024-07-30 08:28:41 -05:00
nu-cmd-base Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-cmd-extra Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-cmd-lang Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-cmd-plugin Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-color-config Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-command Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-derive-value Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-engine Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-explore Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-glob Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-json Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-lsp Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-parser Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-path Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-plugin feat: make ctrlc available to plugins (#13181) 2024-07-30 08:29:18 -05:00
nu-plugin-core feat: make ctrlc available to plugins (#13181) 2024-07-30 08:29:18 -05:00
nu-plugin-engine feat: make ctrlc available to plugins (#13181) 2024-07-30 08:29:18 -05:00
nu-plugin-protocol feat: make ctrlc available to plugins (#13181) 2024-07-30 08:29:18 -05:00
nu-plugin-test-support Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-pretty-hex Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-protocol feat: make ctrlc available to plugins (#13181) 2024-07-30 08:29:18 -05:00
nu-std Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-system Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-table Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-term-grid Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-test-support Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nu-utils Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
nuon Bump version to 0.96.2 (#13485) 2024-07-29 17:20:55 -07:00
README.md Remove old nushell/merge engine-q 2022-02-07 14:54:06 -05:00

Nushell core libraries and plugins

These sub-crates form both the foundation for Nu and a set of plugins which extend Nu with additional functionality.

Foundational libraries are split into two kinds of crates:

  • Core crates - those crates that work together to build the Nushell language engine
  • Support crates - a set of crates that support the engine with additional features like JSON support, ANSI support, and more.

Plugins are likewise also split into two types:

  • Core plugins - plugins that provide part of the default experience of Nu, including access to the system properties, processes, and web-connectivity features.
  • Extra plugins - these plugins run a wide range of different capabilities like working with different file types, charting, viewing binary data, and more.