Skip to content

Instantly share code, notes, and snippets.

@cygnetix
Last active May 31, 2022 12:35
Show Gist options
  • Save cygnetix/769b4b90bdfce78145240ea7235db4f1 to your computer and use it in GitHub Desktop.
Save cygnetix/769b4b90bdfce78145240ea7235db4f1 to your computer and use it in GitHub Desktop.
A vlang adaptation of the nushell python example plugin https://github.com/nushell/nushell/blob/main/crates/nu_plugin_python/plugin.py
import os
import json
struct Signature {
name string
usage string
extra_usage string
required_positional []string
optional_positional []string
named []string
search_terms []string
is_filter bool
creates_scope bool
category string
}
const example_response = '{
"Value": {
"List": {
"vals": [
{
"Record": {
"cols": ["one", "two", "three"],
"vals": [
{
"Int": {
"val": 8,
"span": {"start": 0, "end": 1}
}
},
{
"Int": {
"val": 2,
"span": {"start": 0, "end": 1}
}
},
{
"Int": {
"val": 0,
"span": {"start": 0, "end": 1}
}
}
],
"span": {"start": 0, "end": 1}
}
},
{
"Record": {
"cols": ["one", "two", "three"],
"vals": [
{
"Int": {
"val": 0,
"span": {"start": 0, "end": 1}
}
},
{
"Int": {
"val": 1,
"span": {"start": 0, "end": 1}
}
},
{
"Int": {
"val": 5,
"span": {"start": 0, "end": 1}
}
}
],
"span": {"start": 0, "end": 1}
}
}
],
"span": {"start": 0, "end": 1}
}
}
}'
fn main() {
mut file := os.open_file('output.log', 'a') or { panic(err) }
defer {
file.close()
}
plugin_call := os.get_line()
if plugin_call == '"Signature"' {
s := Signature{
name: 'vplugin'
usage: 'Example Nushell plugin for the V programming language'
extra_usage: ''
required_positional: []string{}
optional_positional: []string{}
named: []string{}
search_terms: []string{}
is_filter: false
creates_scope: false
category: 'Experimental'
}
print('{"Signature": [${json.encode(s)}]}')
} else if plugin_call.starts_with('{"CallInfo"') {
print(example_response)
} else {
print('{
"Error": {
"label": "ERROR from plugin",
"msg": "error message pointing to call head span",
"span": {"start": 0, "end": 1},
}
}')
}
file.write_string('$plugin_call\n') or { panic(err) }
}
@cygnetix
Copy link
Author

cygnetix commented May 31, 2022

v -prod plugin.v
register -e json plugin.exe

vplugin
╭───┬─────┬─────┬───────╮
│ # │ one │ two │ three │
├───┼─────┼─────┼───────┤
│ 0820 │
│ 1015 │
╰───┴─────┴─────┴───────╯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment