Struct nu_plugin::EngineInterface
source · pub struct EngineInterface { /* private fields */ }
Expand description
A reference through which the nushell engine can be interacted with during execution.
Implementations§
source§impl EngineInterface
impl EngineInterface
sourcepub fn get_config(&self) -> Result<Box<Config>, ShellError>
pub fn get_config(&self) -> Result<Box<Config>, ShellError>
Get the full shell configuration from the engine. As this is quite a large object, it is provided on request only.
§Example
Format a value in the user’s preferred way:
let config = engine.get_config()?;
eprintln!("{}", value.to_expanded_string(", ", &config));
sourcepub fn get_plugin_config(&self) -> Result<Option<Value>, ShellError>
pub fn get_plugin_config(&self) -> Result<Option<Value>, ShellError>
Get the plugin-specific configuration from the engine. This lives in
$env.config.plugins.NAME
for a plugin named NAME
. If the config is set to a closure,
it is automatically evaluated each time.
§Example
Print this plugin’s config:
let config = engine.get_plugin_config()?;
eprintln!("{:?}", config);
sourcepub fn get_env_var(
&self,
name: impl Into<String>
) -> Result<Option<Value>, ShellError>
pub fn get_env_var( &self, name: impl Into<String> ) -> Result<Option<Value>, ShellError>
Get an environment variable from the engine.
Returns Some(value)
if present, and None
if not found.
§Example
Get $env.PATH
:
engine.get_env_var("PATH") // => Ok(Some(Value::List([...])))
sourcepub fn get_current_dir(&self) -> Result<String, ShellError>
pub fn get_current_dir(&self) -> Result<String, ShellError>
Get the current working directory from the engine. The result is always an absolute path.
§Example
engine.get_current_dir() // => "/home/user"
sourcepub fn get_env_vars(&self) -> Result<HashMap<String, Value>, ShellError>
pub fn get_env_vars(&self) -> Result<HashMap<String, Value>, ShellError>
Get all environment variables from the engine.
Since this is quite a large map that has to be sent, prefer to use [.get_env_var()
] if
the variables needed are known ahead of time and there are only a small number needed.
§Example
engine.get_env_vars() // => Ok({"PATH": Value::List([...]), ...})
sourcepub fn add_env_var(
&self,
name: impl Into<String>,
value: Value
) -> Result<(), ShellError>
pub fn add_env_var( &self, name: impl Into<String>, value: Value ) -> Result<(), ShellError>
Set an environment variable in the caller’s scope.
If called after the plugin response has already been sent (i.e. during a stream), this will only affect the environment for engine calls related to this plugin call, and will not be propagated to the environment of the caller.
§Example
engine.add_env_var("FOO", Value::test_string("bar"))
sourcepub fn get_help(&self) -> Result<String, ShellError>
pub fn get_help(&self) -> Result<String, ShellError>
Get the help string for the current command.
This returns the same string as passing --help
would, and can be used for the top-level
command in a command group that doesn’t do anything on its own (e.g. query
).
§Example
eprintln!("{}", engine.get_help()?);
sourcepub fn eval_closure_with_stream(
&self,
closure: &Spanned<Closure>,
positional: Vec<Value>,
input: PipelineData,
redirect_stdout: bool,
redirect_stderr: bool
) -> Result<PipelineData, ShellError>
pub fn eval_closure_with_stream( &self, closure: &Spanned<Closure>, positional: Vec<Value>, input: PipelineData, redirect_stdout: bool, redirect_stderr: bool ) -> Result<PipelineData, ShellError>
Ask the engine to evaluate a closure. Input to the closure is passed as a stream, and the output is available as a stream.
Set redirect_stdout
to true
to capture the standard output stream of an external
command, if the closure results in an external command.
Set redirect_stderr
to true
to capture the standard error stream of an external command,
if the closure results in an external command.
§Example
Invoked as:
my_command { seq 1 $in | each { |n| $"Hello, ($n)" } }
let closure = call.req(0)?;
let input = PipelineData::Value(Value::int(4, call.head), None);
let output = engine.eval_closure_with_stream(
&closure,
vec![],
input,
true,
false,
)?;
for value in output {
eprintln!("Closure says: {}", value.as_str()?);
}
Output:
Closure says: Hello, 1
Closure says: Hello, 2
Closure says: Hello, 3
Closure says: Hello, 4
sourcepub fn eval_closure(
&self,
closure: &Spanned<Closure>,
positional: Vec<Value>,
input: Option<Value>
) -> Result<Value, ShellError>
pub fn eval_closure( &self, closure: &Spanned<Closure>, positional: Vec<Value>, input: Option<Value> ) -> Result<Value, ShellError>
Ask the engine to evaluate a closure. Input is optionally passed as a Value
, and output
of the closure is collected to a Value
even if it is a stream.
If the closure results in an external command, the return value will be a collected string
or binary value of the standard output stream of that command, similar to calling
eval_closure_with_stream()
with redirect_stdout
=
true
and redirect_stderr
= false
.
Use eval_closure_with_stream()
if more control over the
input and output is desired.
§Example
Invoked as:
my_command { |number| $number + 1}
let closure = call.req(0)?;
for n in 0..4 {
let result = engine.eval_closure(&closure, vec![Value::int(n, call.head)], None)?;
eprintln!("{} => {}", n, result.as_int()?);
}
Output:
0 => 1
1 => 2
2 => 3
3 => 4
sourcepub fn set_gc_disabled(&self, disabled: bool) -> Result<(), ShellError>
pub fn set_gc_disabled(&self, disabled: bool) -> Result<(), ShellError>
Tell the engine whether to disable garbage collection for this plugin.
The garbage collector is enabled by default, but plugins can turn it off (ideally temporarily) as necessary to implement functionality that requires the plugin to stay running for longer than the engine can automatically determine.
The user can still stop the plugin if they want to with the plugin stop
command.
Trait Implementations§
source§impl Clone for EngineInterface
impl Clone for EngineInterface
source§fn clone(&self) -> EngineInterface
fn clone(&self) -> EngineInterface
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for EngineInterface
impl !RefUnwindSafe for EngineInterface
impl Send for EngineInterface
impl Sync for EngineInterface
impl Unpin for EngineInterface
impl !UnwindSafe for EngineInterface
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoSpanned for T
impl<T> IntoSpanned for T
source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg
or
a color-specific method, such as OwoColorize::green
, Read moresource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg
or
a color-specific method, such as OwoColorize::on_yellow
, Read more