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 is_using_stdio(&self) -> bool
pub fn is_using_stdio(&self) -> bool
Returns true
if the plugin is communicating on stdio. When this is the case, stdin and
stdout should not be used by the plugin for other purposes.
If the plugin can not be used without access to stdio, an error should be presented to the user instead.
Sourcepub fn register_signal_handler(
&self,
handler: Handler,
) -> Result<HandlerGuard, ShellError>
pub fn register_signal_handler( &self, handler: Handler, ) -> Result<HandlerGuard, ShellError>
Register a closure which will be called when the engine receives an interrupt signal. Returns a RAII guard that will keep the closure alive until it is dropped.
Sourcepub fn get_config(&self) -> Result<Arc<Config>, ShellError>
pub fn get_config(&self) -> Result<Arc<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()
] (Self::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 enter_foreground(&self) -> Result<ForegroundGuard, ShellError>
pub fn enter_foreground(&self) -> Result<ForegroundGuard, ShellError>
Returns a guard that will keep the plugin in the foreground as long as the guard is alive.
Moving the plugin to the foreground is necessary for plugins that need to receive input and signals directly from the terminal.
The exact implementation is operating system-specific. On Unix, this ensures that the plugin process becomes part of the process group controlling the terminal.
Sourcepub fn get_span_contents(&self, span: Span) -> Result<Vec<u8>, ShellError>
pub fn get_span_contents(&self, span: Span) -> Result<Vec<u8>, ShellError>
Get the contents of a Span
from the engine.
This method returns Vec<u8>
as it’s possible for the matched span to not be a valid UTF-8
string, perhaps because it sliced through the middle of a UTF-8 byte sequence, as the
offsets are byte-indexed. Use String::from_utf8_lossy()
for display if necessary.
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 find_decl(
&self,
name: impl Into<String>,
) -> Result<Option<DeclId>, ShellError>
pub fn find_decl( &self, name: impl Into<String>, ) -> Result<Option<DeclId>, ShellError>
Ask the engine for the identifier for a declaration. If found, the result can then be passed
to .call_decl()
to call other internal commands.
See .call_decl()
for an example.
Sourcepub fn call_decl(
&self,
decl_id: DeclId,
call: EvaluatedCall,
input: PipelineData,
redirect_stdout: bool,
redirect_stderr: bool,
) -> Result<PipelineData, ShellError>
pub fn call_decl( &self, decl_id: DeclId, call: EvaluatedCall, input: PipelineData, redirect_stdout: bool, redirect_stderr: bool, ) -> Result<PipelineData, ShellError>
Ask the engine to call an internal command, using the declaration ID previously looked up
with .find_decl()
.
§Example
if let Some(decl_id) = engine.find_decl("scope commands")? {
let commands = engine.call_decl(
decl_id,
EvaluatedCall::new(call.head),
PipelineData::Empty,
true,
false,
)?;
commands.into_value(call.head)
} else {
Ok(Value::list(vec![], call.head))
}
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.
pub fn signals(&self) -> &Signals
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 moreSource§impl Debug for EngineInterface
impl Debug for EngineInterface
Source§impl Interface for EngineInterface
impl Interface for EngineInterface
Source§type Output = PluginOutput
type Output = PluginOutput
StreamMessage
.Source§type DataContext = ()
type DataContext = ()
PipelineData
. Can be ()
if not needed.Source§fn write(&self, output: PluginOutput) -> Result<(), ShellError>
fn write(&self, output: PluginOutput) -> Result<(), ShellError>
Source§fn flush(&self) -> Result<(), ShellError>
fn flush(&self) -> Result<(), ShellError>
Source§fn stream_id_sequence(&self) -> &Sequence
fn stream_id_sequence(&self) -> &Sequence
StreamId
s.Source§fn stream_manager_handle(&self) -> &StreamManagerHandle
fn stream_manager_handle(&self) -> &StreamManagerHandle
StreamManagerHandle
for doing stream operations.Source§fn prepare_pipeline_data(
&self,
data: PipelineData,
_context: &(),
) -> Result<PipelineData, ShellError>
fn prepare_pipeline_data( &self, data: PipelineData, _context: &(), ) -> Result<PipelineData, ShellError>
PipelineData
to be written. This is called by init_write_pipeline_data()
as
a hook so that values that need special handling can be taken care of.Source§fn init_write_pipeline_data(
&self,
data: PipelineData,
context: &Self::DataContext,
) -> Result<(PipelineDataHeader, PipelineDataWriter<Self>), ShellError>
fn init_write_pipeline_data( &self, data: PipelineData, context: &Self::DataContext, ) -> Result<(PipelineDataHeader, PipelineDataWriter<Self>), ShellError>
PipelineData
. This returns two parts: the header, which can be
embedded in the particular message that references the stream, and a writer, which will
write out all of the data in the pipeline when .write()
is called. 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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§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