Home Explore Blog CI



nushell

16th chunk of `contributor-book/plugins.md`
6ecc2e5de447c27fa34a45c2a47cc0baa19e1042744a935500000001000007ff
For this plugin, we have to serve two basic roles: responding to a request for the plugin configuration, and doing the actual filtering. This code acts as our main routine, responding to a message from Nu by doing some work and then returning a response: either returning with the plugin signature or handling input.

```python
def send_encoder():
    sys.stdout.write(chr(4))
    for ch in "json":
        sys.stdout.write(chr(ord(ch)))
    sys.stdout.flush()


def send_hello():
    hello = {
        "Hello": {
            "protocol": "nu-plugin",
            "version": "0.90.2",
            "features": []
        }
    }
    sys.stdout.writelines([json.dumps(hello)])
    sys.stdout.flush()
```

The first thing our plugin must do is write out the desired serialization format, in this case JSON. We do that with the `send_encoder()` method. Then we use `send_hello()` to send our [`Hello`](plugin_protocol_reference.md#hello) message, informing Nu of our compatibility with it, and which is required before we can send any other messages. Then we read the JSON serialized messages that Nu sends us. Since Nu always sends each message on its own line, we simply read each line of input and parse it separately.

Each [`Call`](plugin_protocol_reference.md#call) comes with an ID number, which we must keep for the [`CallResponse`](plugin_protocol_reference.md#callresponse) (including errors).

When we're sent a `Signature` request, we respond with the signature of this plugin, which is a bit of information to tell Nu how the command should be called.

When sent a `Run` request, we parse the supplied JSON and respond to the request

```python
def handle_call(id, call_info):
    try:
        input = call_info["input"]["Value"]["String"]
        output = {
            "PipelineData": {
                "Value": {
                    "Int": {
                        "val": len(input["val"]),
                        "span": input["span"]
                    }
                }
            }
        }
        send_response(id, output)

Title: Plugin Initialization and Handling Calls: Encoder, Hello, Signature and Run
Summary
The plugin begins by establishing communication with Nu via `send_encoder()` to specify JSON serialization and `send_hello()` to declare compatibility. It then discusses handling different types of 'Call' messages from Nu, including 'Signature' (responding with the plugin's signature) and 'Run' (parsing the input and responding with the result of the operation, which in this case, appears to be calculating the length of the input string).