forked from extern/nushell
Add support for defining known externals with their own custom completions (#4425)
* WIP for known externals * Now completions can work from scripts * Add support for definiing externs * finish cleaning up old proof-of-concept
This commit is contained in:
33
crates/nu-command/src/core_commands/extern_.rs
Normal file
33
crates/nu-command/src/core_commands/extern_.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, PipelineData, Signature, SyntaxShape};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Extern;
|
||||
|
||||
impl Command for Extern {
|
||||
fn name(&self) -> &str {
|
||||
"extern"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Define a signature for an external command"
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("extern")
|
||||
.required("def_name", SyntaxShape::String, "definition name")
|
||||
.required("params", SyntaxShape::Signature, "parameters")
|
||||
.category(Category::Core)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ mod export;
|
||||
mod export_def;
|
||||
mod export_def_env;
|
||||
mod export_env;
|
||||
mod extern_;
|
||||
mod for_;
|
||||
mod help;
|
||||
mod hide;
|
||||
@ -36,6 +37,7 @@ pub use export::ExportCommand;
|
||||
pub use export_def::ExportDef;
|
||||
pub use export_def_env::ExportDefEnv;
|
||||
pub use export_env::ExportEnv;
|
||||
pub use extern_::Extern;
|
||||
pub use for_::For;
|
||||
pub use help::Help;
|
||||
pub use hide::Hide;
|
||||
|
@ -38,6 +38,7 @@ pub fn create_default_context(cwd: impl AsRef<Path>) -> EngineState {
|
||||
ExportDef,
|
||||
ExportDefEnv,
|
||||
ExportEnv,
|
||||
Extern,
|
||||
For,
|
||||
Help,
|
||||
Hide,
|
||||
@ -358,9 +359,6 @@ pub fn create_default_context(cwd: impl AsRef<Path>) -> EngineState {
|
||||
#[cfg(feature = "plugin")]
|
||||
bind_command!(Register);
|
||||
|
||||
// This is a WIP proof of concept
|
||||
// bind_command!(ListGitBranches, Git, GitCheckout, Source);
|
||||
|
||||
working_set.render()
|
||||
};
|
||||
|
||||
|
@ -1,57 +0,0 @@
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, IntoPipelineData, PipelineData, Signature, Value};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Git;
|
||||
|
||||
impl Command for Git {
|
||||
fn name(&self) -> &str {
|
||||
"git"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Run a block"
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("git").category(Category::Experimental)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
use std::process::Command as ProcessCommand;
|
||||
use std::process::Stdio;
|
||||
|
||||
let proc = ProcessCommand::new("git").stdout(Stdio::piped()).spawn();
|
||||
|
||||
match proc {
|
||||
Ok(child) => {
|
||||
match child.wait_with_output() {
|
||||
Ok(val) => {
|
||||
let result = val.stdout;
|
||||
|
||||
Ok(Value::String {
|
||||
val: String::from_utf8_lossy(&result).to_string(),
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
}
|
||||
Err(_err) => {
|
||||
// FIXME: Move this to an external signature and add better error handling
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_err) => {
|
||||
// FIXME: Move this to an external signature and add better error handling
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
use nu_engine::eval_expression;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, IntoPipelineData, PipelineData, Signature, SyntaxShape, Value};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GitCheckout;
|
||||
|
||||
impl Command for GitCheckout {
|
||||
fn name(&self) -> &str {
|
||||
"git checkout"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Checkout a git revision"
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("git checkout")
|
||||
.required(
|
||||
"branch",
|
||||
SyntaxShape::Custom(Box::new(SyntaxShape::String), "list-git-branches".into()),
|
||||
"the branch to checkout",
|
||||
)
|
||||
.category(Category::Experimental)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
use std::process::Command as ProcessCommand;
|
||||
use std::process::Stdio;
|
||||
|
||||
let block = &call.positional[0];
|
||||
|
||||
let out = eval_expression(engine_state, stack, block)?;
|
||||
|
||||
let out = out.as_string()?;
|
||||
|
||||
let proc = ProcessCommand::new("git")
|
||||
.arg("checkout")
|
||||
.arg(out)
|
||||
.stdout(Stdio::piped())
|
||||
.spawn();
|
||||
|
||||
match proc {
|
||||
Ok(child) => {
|
||||
match child.wait_with_output() {
|
||||
Ok(val) => {
|
||||
let result = val.stdout;
|
||||
|
||||
Ok(Value::String {
|
||||
val: String::from_utf8_lossy(&result).to_string(),
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
}
|
||||
Err(_err) => {
|
||||
// FIXME: Move this to an external signature and add better error handling
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_err) => {
|
||||
// FIXME: Move this to an external signature and add better error handling
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
// Note: this is a temporary command that later will be converted into a pipeline
|
||||
|
||||
use std::process::Command as ProcessCommand;
|
||||
use std::process::Stdio;
|
||||
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::Command;
|
||||
use nu_protocol::engine::EngineState;
|
||||
use nu_protocol::engine::Stack;
|
||||
use nu_protocol::Category;
|
||||
use nu_protocol::IntoInterruptiblePipelineData;
|
||||
use nu_protocol::PipelineData;
|
||||
use nu_protocol::{Signature, Value};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ListGitBranches;
|
||||
|
||||
//NOTE: this is not a real implementation :D. It's just a simple one to test with until we port the real one.
|
||||
impl Command for ListGitBranches {
|
||||
fn name(&self) -> &str {
|
||||
"list-git-branches"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"List the git branches of the current directory."
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("list-git-branches").category(Category::Experimental)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let list_branches = ProcessCommand::new("git")
|
||||
.arg("branch")
|
||||
.stdout(Stdio::piped())
|
||||
.spawn();
|
||||
|
||||
if let Ok(child) = list_branches {
|
||||
if let Ok(output) = child.wait_with_output() {
|
||||
let val = output.stdout;
|
||||
|
||||
let s = String::from_utf8_lossy(&val).to_string();
|
||||
|
||||
#[allow(clippy::needless_collect)]
|
||||
let lines: Vec<_> = s
|
||||
.lines()
|
||||
.filter_map(|x| {
|
||||
if x.starts_with("* ") {
|
||||
None
|
||||
} else {
|
||||
Some(x.trim())
|
||||
}
|
||||
})
|
||||
.map(|x| Value::String {
|
||||
val: x.into(),
|
||||
span: call.head,
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(lines
|
||||
.into_iter()
|
||||
.into_pipeline_data(engine_state.ctrlc.clone()))
|
||||
} else {
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
} else {
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +1,3 @@
|
||||
mod git;
|
||||
mod git_checkout;
|
||||
mod list_git_branches;
|
||||
mod view_source;
|
||||
|
||||
pub use git::Git;
|
||||
pub use git_checkout::GitCheckout;
|
||||
pub use list_git_branches::ListGitBranches;
|
||||
pub use view_source::ViewSource;
|
||||
|
Reference in New Issue
Block a user