Skip to content

Commit

Permalink
move Explore from lib.rs to main.rs
Browse files Browse the repository at this point in the history
this commit also makes the `nu_plugin_explore::explore` function
public to be able to use it inside `main.rs`.
  • Loading branch information
amtoine committed Aug 27, 2023
1 parent 46550e0 commit 2d50115
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 51 deletions.
52 changes: 3 additions & 49 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,68 +9,22 @@ mod tui;

use anyhow::{Context, Result};

use nu_plugin::{EvaluatedCall, LabeledError, Plugin};
use nu_protocol::{Category, PluginExample, PluginSignature, Type, Value};
use nu_protocol::{Span, SyntaxShape};
use nu_plugin::{EvaluatedCall, LabeledError};
use nu_protocol::{Span, Value};

use app::{Mode, State};
use config::Config;
use terminal::restore as restore_terminal;
use terminal::setup as setup_terminal;

/// the main structure of the [Nushell](https://nushell.sh) plugin
pub struct Explore;

impl Plugin for Explore {
fn signature(&self) -> Vec<PluginSignature> {
vec![PluginSignature::build("explore")
.usage("interactively explore Nushell structured data")
.input_output_type(Type::Any, Type::Any)
.optional(
"config",
SyntaxShape::Record(vec![]),
"a config record to configure everything in explore",
)
.plugin_examples(vec![
PluginExample {
example: "open Cargo.toml | explore".into(),
description: "explore the Cargo.toml file of this project".into(),
result: None,
},
PluginExample {
example: r#"$nu | explore {show_cell_path: false, layout: "compact"}"#.into(),
description: "explore `$nu` and set some config options".into(),
result: None,
},
])
.category(Category::Experimental)]
}

fn run(
&mut self,
name: &str,
call: &EvaluatedCall,
input: &Value,
) -> Result<Value, LabeledError> {
match name {
"explore" => explore(call, input),
_ => Err(LabeledError {
label: "Plugin call with wrong name signature".into(),
msg: "the signature used to call the plugin does not match any name in the plugin signature vector".into(),
span: Some(call.head),
}),
}
}
}

/// the entry point of the `explore` command
///
/// this function
/// 1. parses the config and default to the [`config::Config::default`] otherwise
/// 1. sets the terminal up (see [`terminal::setup`])
/// 1. runs the application (see [`app::run`])
/// 1. restores the terminal (see [`terminal::restore`])
fn explore(call: &EvaluatedCall, input: &Value) -> Result<Value, LabeledError> {
pub fn explore(call: &EvaluatedCall, input: &Value) -> Result<Value, LabeledError> {
let config = Config::from_value(call.opt(0).unwrap().unwrap_or(Value::record(
vec![],
vec![],
Expand Down
50 changes: 48 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
use nu_plugin::{serve_plugin, MsgPackSerializer};
use nu_plugin_explore::Explore;
use nu_plugin::{serve_plugin, EvaluatedCall, LabeledError, MsgPackSerializer, Plugin};
use nu_plugin_explore::explore;
use nu_protocol::{Category, PluginExample, PluginSignature, SyntaxShape, Type, Value};

/// the main structure of the [Nushell](https://nushell.sh) plugin
struct Explore;

impl Plugin for Explore {
fn signature(&self) -> Vec<PluginSignature> {
vec![PluginSignature::build("explore")
.usage("interactively explore Nushell structured data")
.input_output_type(Type::Any, Type::Any)
.optional(
"config",
SyntaxShape::Record(vec![]),
"a config record to configure everything in explore",
)
.plugin_examples(vec![
PluginExample {
example: "open Cargo.toml | explore".into(),
description: "explore the Cargo.toml file of this project".into(),
result: None,
},
PluginExample {
example: r#"$nu | explore {show_cell_path: false, layout: "compact"}"#.into(),
description: "explore `$nu` and set some config options".into(),
result: None,
},
])
.category(Category::Experimental)]
}

fn run(
&mut self,
name: &str,
call: &EvaluatedCall,
input: &Value,
) -> Result<Value, LabeledError> {
match name {
"explore" => explore(call, input),
_ => Err(LabeledError {
label: "Plugin call with wrong name signature".into(),
msg: "the signature used to call the plugin does not match any name in the plugin signature vector".into(),
span: Some(call.head),
}),
}
}
}

fn main() {
serve_plugin(&mut Explore {}, MsgPackSerializer {})
Expand Down

0 comments on commit 2d50115

Please sign in to comment.