You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contributor-book/plugin_protocol_reference.md
+64Lines changed: 64 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,6 +33,8 @@ The plugin **may** send [engine calls](#enginecall) during the execution of a ca
33
33
34
34
The engine **may** send a [`Goodbye`](#goodbye) message to the plugin indicating that it will no longer send any more plugin calls. Upon receiving this message, the plugin **may** choose not to accept any more plugin calls, and **should** exit after any in-progress plugin calls have finished.
35
35
36
+
**Note**: During the sequence, the engine may also send a [`Signal`](#signal) message asynchronously, such as when an interrupt (Ctrl+C) or reset signal is triggered. Plugins should handle these messages as they are received, for example, by pausing or stopping operations when an `Interrupt` signal is sent.
37
+
36
38
## `Hello`
37
39
38
40
After the encoding type has been decided, both the engine and plugin **must** send a `Hello` message containing relevant version and protocol support information.
@@ -557,6 +559,21 @@ Example:
557
559
}
558
560
```
559
561
562
+
### `Signal`
563
+
564
+
The `Signal` message type is used to relay a signal from the engine to the plugin, allowing the plugin to respond to various system-level or user-initiated signals. The message body consists of a `SignalAction` enum, which currently supports the following variants:
565
+
566
+
-**`Interrupt`**: Sent when the engine receives an interrupt signal (such as Ctrl+C), to gracefully interrupt a plugin's operation.
567
+
-**`Reset`**: Sent when the engine’s `reset_signals` method is called, indicating that the plugin should reset its signal state.
568
+
569
+
Example:
570
+
571
+
```json
572
+
{
573
+
"Signal": "Interrupt"
574
+
}
575
+
```
576
+
560
577
### `Goodbye`
561
578
562
579
Indicate that no further plugin calls are expected, and that the plugin **should** exit as soon as it is finished processing any in-progress plugin calls.
@@ -1228,6 +1245,42 @@ Example:
1228
1245
}
1229
1246
```
1230
1247
1248
+
## Signal Handling in Plugins
1249
+
1250
+
Plugins can respond to signals sent from the engine, such as interrupts (Ctrl+C) or resets, by registering handlers. The plugin’s signal handling methods allow for customizable responses to user or system actions, enhancing the plugin's integration with the Nu engine.
1251
+
1252
+
### `register_signal_handler`
1253
+
1254
+
The `register_signal_handler` method allows a plugin to register a handler that will be called when a signal is received. This method accepts a closure with the following signature:
1255
+
```rust
1256
+
|action:SignalAction| { ... }
1257
+
```
1258
+
The closure will be invoked with the `SignalAction` variant received from the engine. This method returns an RAII guard that ensures the handler remains active until it is dropped.
1259
+
1260
+
#### Example Usage
1261
+
1262
+
Below is an example of registering a handler that responds to both `Interrupt` and `Reset` signals:
SignalAction::Interrupt=>println!("Interrupt signal received"),
1268
+
SignalAction::Reset=>println!("Reset signal received"),
1269
+
}
1270
+
}));
1271
+
```
1272
+
1273
+
#### `signals()`
1274
+
1275
+
The `signals()` method allows the plugin to check the status of the signal, specifically for `Interrupt`. This method returns a `Signals` struct, which includes the method `interrupted()` that indicates if an interrupt has occurred.
1276
+
1277
+
```rust
1278
+
ifengine.signals().interrupted() {
1279
+
println!("Operation was interrupted.");
1280
+
}
1281
+
```
1282
+
Use `signals().interrupted()` to check for interrupt status, particularly when managing long-running operations.
0 commit comments