summaryrefslogtreecommitdiff
path: root/crates/nu_plugin_nu_example/nu_plugin_nu_example.nu
blob: 09f476931191ae3deb6471e78480f077a96b827d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env -S nu --stdin
# Example of using a Nushell script as a Nushell plugin
#
# This is a port of the nu_plugin_python_example plugin to Nushell itself. There is probably not
# really any reason to write a Nushell plugin in Nushell, but this is a fun proof of concept, and
# it also allows us to test the plugin interface with something manually implemented in a scripting
# language without adding any extra dependencies to our tests.

const NUSHELL_VERSION = "0.97.2"
const PLUGIN_VERSION = "0.1.1" # bump if you change commands!

def main [--stdio] {
  if ($stdio) {
    start_plugin
  } else {
    print -e "Run me from inside nushell!"
    exit 1
  }
}

const SIGNATURES = [
  {
    sig: {
      name: nu_plugin_nu_example,
      description: "Signature test for Nushell plugin in Nushell",
      extra_description: "",
      required_positional: [
        [
          name,
          desc,
          shape
        ];
        [
          a,
          "required integer value",
          Int
        ],
        [
          b,
          "required string value",
          String
        ]
      ],
      optional_positional: [
        [
          name,
          desc,
          shape
        ];
        [
          opt,
          "Optional number",
          Int
        ]
      ],
      rest_positional: {
        name: rest,
        desc: "rest value string",
        shape: String
      },
      named: [
        [
          long,
          short,
          arg,
          required,
          desc
        ];
        [
          help,
          h,
          null,
          false,
          "Display the help message for this command"
        ],
        [
          flag,
          f,
          null,
          false,
          "a flag for the signature"
        ],
        [
          named,
          n,
          String,
          false,
          "named string"
        ]
      ],
      input_output_types: [
        [Any, Any]
      ],
      allow_variants_without_examples: true,
      search_terms: [
        Example
      ],
      is_filter: false,
      creates_scope: false,
      allows_unknown_args: false,
      category: Experimental
    },
    examples: []
  }
]

def process_call [
  id: int,
  plugin_call: record<
    name: string,
    call: record<
      head: record<start: int, end: int>,
      positional: list,
      named: list,
    >,
    input: any
  >
] {
  # plugin_call is a dictionary with the information from the call
  # It should contain:
  #         - The name of the call
  #         - The call data which includes the positional and named values
  #         - The input from the pipeline

  # Use this information to implement your plugin logic

  # Print the call to stderr, in raw nuon and as a table
  $plugin_call | to nuon --raw | print -e
  $plugin_call | table -e | print -e

  # Get the span from the call
  let span = $plugin_call.call.head

  # Create a Value of type List that will be encoded and sent to Nushell
  let value = {
    Value: [{
      List: {
        vals: (0..9 | each { |x|
          {
            Record: {
              val: (
                [one two three] |
                  zip (0..2 | each { |y|
                    {
                      Int: {
                        val: ($x * $y),
                        span: $span,
                      }
                    }
                  }) |
                  into record
              ),
              span: $span
            }
          }
        }),
        span: $span
      }
    }, null]
  }

  write_response $id { PipelineData: $value }
}

def tell_nushell_encoding [] {
  print -n "\u{0004}json"
}

def tell_nushell_hello [] {
  # A `Hello` message is required at startup to inform nushell of the protocol capabilities and
  # compatibility of the plugin. The version specified should be the version of nushell that this
  # plugin was tested and developed against.
  let hello = {
    Hello: {
      protocol: "nu-plugin", # always this value
      version: $NUSHELL_VERSION,
      features: []
    }
  }
  $hello | to json --raw | print
}

def write_response [id: int, response: record] {
  # Use this format to send a response to a plugin call. The ID of the plugin call is required.
  let wrapped_response = {
    CallResponse: [
      $id,
      $response,
    ]
  }
  $wrapped_response | to json --raw | print
}

def write_error [id: int, text: string, span?: record<start: int, end: int>] {
  # Use this error format to send errors to nushell in response to a plugin call. The ID of the
  # plugin call is required.
  let error = if ($span | is-not-empty) {
    {
      Error: {
        msg: "ERROR from plugin",
        labels: [
          {
            text: $text,
            span: $span,
          }
        ],
      }
    }
  } else {
    {
      Error: {
        msg: "ERROR from plugin",
        help: $text,
      }
    }
  }
  write_response $id $error
}

def handle_input []: any -> nothing {
  match $in {
    { Hello: $hello } => {
      if ($hello.version != $NUSHELL_VERSION) {
        exit 1
      }
    }
    "Goodbye" => {
      exit 0
    }
    { Call: [$id, $plugin_call] } => {
      match $plugin_call {
        "Metadata" => {
          write_response $id {
            Metadata: {
              version: $PLUGIN_VERSION
            }
          }
        }
        "Signature" => {
          write_response $id { Signature: $SIGNATURES }
        }
        { Run: $call_info } => {
          process_call $id $call_info
        }
        _ => {
          write_error $id $"Operation not supported: ($plugin_call | to json --raw)"
        }
      }
    }
    $other => {
      print -e $"Unknown message: ($other | to json --raw)"
      exit 1
    }
  }
}

def start_plugin [] {
  lines |
    prepend (do {
      # This is a hack so that we do this first, but we can also take input as a stream
      tell_nushell_encoding
      tell_nushell_hello
      []
    }) |
    each { from json | handle_input } |
    ignore
}