nushell/crates/nu-protocol/src/engine
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
..
argument.rs Internal representation (IR) compiler and evaluator (#13330) 2024-07-10 17:33:59 -07:00
cached_file.rs Refactor source cache into CachedFile struct (#12240) 2024-03-20 19:43:50 +01:00
call_info.rs Add command_prelude module (#12291) 2024-03-26 21:17:30 +00:00
call.rs Internal representation (IR) compiler and evaluator (#13330) 2024-07-10 17:33:59 -07:00
capture_block.rs Remove the Value::Block case (#12582) 2024-04-21 07:03:33 +02:00
command.rs Internal representation (IR) compiler and evaluator (#13330) 2024-07-10 17:33:59 -07:00
ctrlc.rs feat: make ctrlc available to plugins (#13181) 2024-07-30 08:29:18 -05:00
engine_state.rs feat: make ctrlc available to plugins (#13181) 2024-07-30 08:29:18 -05:00
error_handler.rs Internal representation (IR) compiler and evaluator (#13330) 2024-07-10 17:33:59 -07:00
mod.rs feat: make ctrlc available to plugins (#13181) 2024-07-30 08:29:18 -05:00
overlay.rs Style: move some Option if/else to method chains (#12285) 2024-03-26 08:35:51 +08:00
pattern_match.rs Shrink the size of Expr (#12610) 2024-04-24 15:46:35 +00:00
sequence.rs feat: make ctrlc available to plugins (#13181) 2024-07-30 08:29:18 -05:00
stack_out_dest.rs Rename IoStream to OutDest (#12433) 2024-04-09 16:48:32 +00:00
stack.rs Fix bad method links in docstrings (#13471) 2024-07-27 19:39:29 -07:00
state_delta.rs Span ID Refactor - Step 1 (#12960) 2024-06-05 09:57:14 +08:00
state_working_set.rs Remove unused field in StateWorkingSet (#13387) 2024-07-16 11:28:31 +02:00
usage.rs Fix usage parsing for commands defined in CRLF (windows) files (#13212) 2024-06-23 18:43:05 -05:00
variable.rs Restructure nu-protocol in more meaningful units (#11917) 2024-03-10 18:45:45 +01:00