nushell/crates/nu-command/src/system/complete.rs
Ian Manske 8da27a1a09
Create Record type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
   ```rust
   record! {
       "key1" => some_value,
       "key2" => Value::string("text", span),
       "key3" => Value::int(optional_int.unwrap_or(0), span),
       "key4" => Value::bool(config.setting, span),
   }
   ```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.

Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.

# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-25 07:50:29 +12:00

137 lines
5.0 KiB
Rust

use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Type, Value,
};
use std::thread;
#[derive(Clone)]
pub struct Complete;
impl Command for Complete {
fn name(&self) -> &str {
"complete"
}
fn signature(&self) -> Signature {
Signature::build("complete")
.category(Category::System)
.input_output_types(vec![(Type::Any, Type::Record(vec![]))])
}
fn usage(&self) -> &str {
"Capture the outputs and exit code from an external piped in command in a nushell table."
}
fn extra_usage(&self) -> &str {
r#"In order to capture stdout, stderr, and exit_code, externally piped in commands need to be wrapped with `do`"#
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
match input {
PipelineData::ExternalStream {
stdout,
stderr,
exit_code,
..
} => {
let mut record = Record::new();
// use a thread to receive stderr message.
// Or we may get a deadlock if child process sends out too much bytes to stdout.
//
// For example: in normal linux system, stdout pipe's limit is 65535 bytes.
// if child process sends out 65536 bytes, the process will be hanged because no consumer
// consumes the first 65535 bytes
// So we need a thread to receive stderr message, then the current thread can continue to consume
// stdout messages.
let stderr_handler = stderr.map(|stderr| {
let stderr_span = stderr.span;
(
thread::Builder::new()
.name("stderr consumer".to_string())
.spawn(move || {
let stderr = stderr.into_bytes()?;
if let Ok(st) = String::from_utf8(stderr.item.clone()) {
Ok::<_, ShellError>(Value::String {
val: st,
span: stderr.span,
})
} else {
Ok::<_, ShellError>(Value::Binary {
val: stderr.item,
span: stderr.span,
})
}
})
.expect("failed to create thread"),
stderr_span,
)
});
if let Some(stdout) = stdout {
let stdout = stdout.into_bytes()?;
record.push(
"stdout",
if let Ok(st) = String::from_utf8(stdout.item.clone()) {
Value::string(st, stdout.span)
} else {
Value::binary(stdout.item, stdout.span)
},
)
}
if let Some((handler, stderr_span)) = stderr_handler {
let res = handler.join().map_err(|err| ShellError::ExternalCommand {
label: "Fail to receive external commands stderr message".to_string(),
help: format!("{err:?}"),
span: stderr_span,
})??;
record.push("stderr", res)
};
if let Some(exit_code) = exit_code {
let mut v: Vec<_> = exit_code.collect();
if let Some(v) = v.pop() {
record.push("exit_code", v);
}
}
Ok(Value::record(record, call.head).into_pipeline_data())
}
_ => Err(ShellError::GenericError(
"Complete only works with external streams".to_string(),
"complete only works on external streams".to_string(),
Some(call.head),
None,
Vec::new(),
)),
}
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description:
"Run the external command to completion, capturing stdout and exit_code",
example: "^external arg1 | complete",
result: None,
},
Example {
description:
"Run external command to completion, capturing, stdout, stderr and exit_code",
example: "do { ^external arg1 } | complete",
result: None,
},
]
}
}