mirror of
https://github.com/nushell/nushell.git
synced 2025-05-21 02:10:47 +02:00
# Description The PR overhauls how IO redirection is handled, allowing more explicit and fine-grain control over `stdout` and `stderr` output as well as more efficient IO and piping. To summarize the changes in this PR: - Added a new `IoStream` type to indicate the intended destination for a pipeline element's `stdout` and `stderr`. - The `stdout` and `stderr` `IoStream`s are stored in the `Stack` and to avoid adding 6 additional arguments to every eval function and `Command::run`. The `stdout` and `stderr` streams can be temporarily overwritten through functions on `Stack` and these functions will return a guard that restores the original `stdout` and `stderr` when dropped. - In the AST, redirections are now directly part of a `PipelineElement` as a `Option<Redirection>` field instead of having multiple different `PipelineElement` enum variants for each kind of redirection. This required changes to the parser, mainly in `lite_parser.rs`. - `Command`s can also set a `IoStream` override/redirection which will apply to the previous command in the pipeline. This is used, for example, in `ignore` to allow the previous external command to have its stdout redirected to `Stdio::null()` at spawn time. In contrast, the current implementation has to create an os pipe and manually consume the output on nushell's side. File and pipe redirections (`o>`, `e>`, `e>|`, etc.) have precedence over overrides from commands. This PR improves piping and IO speed, partially addressing #10763. Using the `throughput` command from that issue, this PR gives the following speedup on my setup for the commands below: | Command | Before (MB/s) | After (MB/s) | Bash (MB/s) | | --------------------------- | -------------:| ------------:| -----------:| | `throughput o> /dev/null` | 1169 | 52938 | 54305 | | `throughput \| ignore` | 840 | 55438 | N/A | | `throughput \| null` | Error | 53617 | N/A | | `throughput \| rg 'x'` | 1165 | 3049 | 3736 | | `(throughput) \| rg 'x'` | 810 | 3085 | 3815 | (Numbers above are the median samples for throughput) This PR also paves the way to refactor our `ExternalStream` handling in the various commands. For example, this PR already fixes the following code: ```nushell ^sh -c 'echo -n "hello "; sleep 0; echo "world"' | find "hello world" ``` This returns an empty list on 0.90.1 and returns a highlighted "hello world" on this PR. Since the `stdout` and `stderr` `IoStream`s are available to commands when they are run, then this unlocks the potential for more convenient behavior. E.g., the `find` command can disable its ansi highlighting if it detects that the output `IoStream` is not the terminal. Knowing the output streams will also allow background job output to be redirected more easily and efficiently. # User-Facing Changes - External commands returned from closures will be collected (in most cases): ```nushell 1..2 | each {|_| nu -c "print a" } ``` This gives `["a", "a"]` on this PR, whereas this used to print "a\na\n" and then return an empty list. ```nushell 1..2 | each {|_| nu -c "print -e a" } ``` This gives `["", ""]` and prints "a\na\n" to stderr, whereas this used to return an empty list and print "a\na\n" to stderr. - Trailing new lines are always trimmed for external commands when piping into internal commands or collecting it as a value. (Failure to decode the output as utf-8 will keep the trailing newline for the last binary value.) In the current nushell version, the following three code snippets differ only in parenthesis placement, but they all also have different outputs: 1. `1..2 | each { ^echo a }` ``` a a ╭────────────╮ │ empty list │ ╰────────────╯ ``` 2. `1..2 | each { (^echo a) }` ``` ╭───┬───╮ │ 0 │ a │ │ 1 │ a │ ╰───┴───╯ ``` 3. `1..2 | (each { ^echo a })` ``` ╭───┬───╮ │ 0 │ a │ │ │ │ │ 1 │ a │ │ │ │ ╰───┴───╯ ``` But in this PR, the above snippets will all have the same output: ``` ╭───┬───╮ │ 0 │ a │ │ 1 │ a │ ╰───┴───╯ ``` - All existing flags on `run-external` are now deprecated. - File redirections now apply to all commands inside a code block: ```nushell (nu -c "print -e a"; nu -c "print -e b") e> test.out ``` This gives "a\nb\n" in `test.out` and prints nothing. The same result would happen when printing to stdout and using a `o>` file redirection. - External command output will (almost) never be ignored, and ignoring output must be explicit now: ```nushell (^echo a; ^echo b) ``` This prints "a\nb\n", whereas this used to print only "b\n". This only applies to external commands; values and internal commands not in return position will not print anything (e.g., `(echo a; echo b)` still only prints "b"). - `complete` now always captures stderr (`do` is not necessary). # After Submitting The language guide and other documentation will need to be updated.
496 lines
19 KiB
Rust
496 lines
19 KiB
Rust
use log::trace;
|
|
use nu_ansi_term::Style;
|
|
use nu_color_config::{get_matching_brackets_style, get_shape_color};
|
|
use nu_engine::env;
|
|
use nu_parser::{flatten_block, parse, FlatShape};
|
|
use nu_protocol::ast::{Argument, Block, Expr, Expression, PipelineRedirection, RecordItem};
|
|
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
|
use nu_protocol::{Config, Span};
|
|
use reedline::{Highlighter, StyledText};
|
|
use std::sync::Arc;
|
|
|
|
pub struct NuHighlighter {
|
|
pub engine_state: Arc<EngineState>,
|
|
pub stack: Arc<Stack>,
|
|
pub config: Config,
|
|
}
|
|
|
|
impl Highlighter for NuHighlighter {
|
|
fn highlight(&self, line: &str, _cursor: usize) -> StyledText {
|
|
trace!("highlighting: {}", line);
|
|
|
|
let highlight_resolved_externals =
|
|
self.engine_state.get_config().highlight_resolved_externals;
|
|
let mut working_set = StateWorkingSet::new(&self.engine_state);
|
|
let block = parse(&mut working_set, None, line.as_bytes(), false);
|
|
let (shapes, global_span_offset) = {
|
|
let mut shapes = flatten_block(&working_set, &block);
|
|
// Highlighting externals has a config point because of concerns that using which to resolve
|
|
// externals may slow down things too much.
|
|
if highlight_resolved_externals {
|
|
for (span, shape) in shapes.iter_mut() {
|
|
if *shape == FlatShape::External {
|
|
let str_contents =
|
|
working_set.get_span_contents(Span::new(span.start, span.end));
|
|
|
|
let str_word = String::from_utf8_lossy(str_contents).to_string();
|
|
let paths = env::path_str(&self.engine_state, &self.stack, *span).ok();
|
|
let res = if let Ok(cwd) =
|
|
env::current_dir_str(&self.engine_state, &self.stack)
|
|
{
|
|
which::which_in(str_word, paths.as_ref(), cwd).ok()
|
|
} else {
|
|
which::which_in_global(str_word, paths.as_ref())
|
|
.ok()
|
|
.and_then(|mut i| i.next())
|
|
};
|
|
if res.is_some() {
|
|
*shape = FlatShape::ExternalResolved;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
(shapes, self.engine_state.next_span_start())
|
|
};
|
|
|
|
let mut output = StyledText::default();
|
|
let mut last_seen_span = global_span_offset;
|
|
|
|
let global_cursor_offset = _cursor + global_span_offset;
|
|
let matching_brackets_pos = find_matching_brackets(
|
|
line,
|
|
&working_set,
|
|
&block,
|
|
global_span_offset,
|
|
global_cursor_offset,
|
|
);
|
|
|
|
for shape in &shapes {
|
|
if shape.0.end <= last_seen_span
|
|
|| last_seen_span < global_span_offset
|
|
|| shape.0.start < global_span_offset
|
|
{
|
|
// We've already output something for this span
|
|
// so just skip this one
|
|
continue;
|
|
}
|
|
if shape.0.start > last_seen_span {
|
|
let gap = line
|
|
[(last_seen_span - global_span_offset)..(shape.0.start - global_span_offset)]
|
|
.to_string();
|
|
output.push((Style::new(), gap));
|
|
}
|
|
let next_token = line
|
|
[(shape.0.start - global_span_offset)..(shape.0.end - global_span_offset)]
|
|
.to_string();
|
|
|
|
macro_rules! add_colored_token_with_bracket_highlight {
|
|
($shape:expr, $span:expr, $text:expr) => {{
|
|
let spans = split_span_by_highlight_positions(
|
|
line,
|
|
$span,
|
|
&matching_brackets_pos,
|
|
global_span_offset,
|
|
);
|
|
spans.iter().for_each(|(part, highlight)| {
|
|
let start = part.start - $span.start;
|
|
let end = part.end - $span.start;
|
|
let text = (&next_token[start..end]).to_string();
|
|
let mut style = get_shape_color($shape.to_string(), &self.config);
|
|
if *highlight {
|
|
style = get_matching_brackets_style(style, &self.config);
|
|
}
|
|
output.push((style, text));
|
|
});
|
|
}};
|
|
}
|
|
|
|
let mut add_colored_token = |shape: &FlatShape, text: String| {
|
|
output.push((get_shape_color(shape.to_string(), &self.config), text));
|
|
};
|
|
|
|
match shape.1 {
|
|
FlatShape::Garbage => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Nothing => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Binary => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Bool => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Int => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Float => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Range => add_colored_token(&shape.1, next_token),
|
|
FlatShape::InternalCall(_) => add_colored_token(&shape.1, next_token),
|
|
FlatShape::External => add_colored_token(&shape.1, next_token),
|
|
FlatShape::ExternalArg => add_colored_token(&shape.1, next_token),
|
|
FlatShape::ExternalResolved => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Keyword => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Literal => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Operator => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Signature => add_colored_token(&shape.1, next_token),
|
|
FlatShape::String => add_colored_token(&shape.1, next_token),
|
|
FlatShape::StringInterpolation => add_colored_token(&shape.1, next_token),
|
|
FlatShape::DateTime => add_colored_token(&shape.1, next_token),
|
|
FlatShape::List => {
|
|
add_colored_token_with_bracket_highlight!(shape.1, shape.0, next_token)
|
|
}
|
|
FlatShape::Table => {
|
|
add_colored_token_with_bracket_highlight!(shape.1, shape.0, next_token)
|
|
}
|
|
FlatShape::Record => {
|
|
add_colored_token_with_bracket_highlight!(shape.1, shape.0, next_token)
|
|
}
|
|
|
|
FlatShape::Block => {
|
|
add_colored_token_with_bracket_highlight!(shape.1, shape.0, next_token)
|
|
}
|
|
FlatShape::Closure => {
|
|
add_colored_token_with_bracket_highlight!(shape.1, shape.0, next_token)
|
|
}
|
|
|
|
FlatShape::Filepath => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Directory => add_colored_token(&shape.1, next_token),
|
|
FlatShape::GlobPattern => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Variable(_) | FlatShape::VarDecl(_) => {
|
|
add_colored_token(&shape.1, next_token)
|
|
}
|
|
FlatShape::Flag => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Pipe => add_colored_token(&shape.1, next_token),
|
|
FlatShape::And => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Or => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Redirection => add_colored_token(&shape.1, next_token),
|
|
FlatShape::Custom(..) => add_colored_token(&shape.1, next_token),
|
|
FlatShape::MatchPattern => add_colored_token(&shape.1, next_token),
|
|
}
|
|
last_seen_span = shape.0.end;
|
|
}
|
|
|
|
let remainder = line[(last_seen_span - global_span_offset)..].to_string();
|
|
if !remainder.is_empty() {
|
|
output.push((Style::new(), remainder));
|
|
}
|
|
|
|
output
|
|
}
|
|
}
|
|
|
|
fn split_span_by_highlight_positions(
|
|
line: &str,
|
|
span: Span,
|
|
highlight_positions: &[usize],
|
|
global_span_offset: usize,
|
|
) -> Vec<(Span, bool)> {
|
|
let mut start = span.start;
|
|
let mut result: Vec<(Span, bool)> = Vec::new();
|
|
for pos in highlight_positions {
|
|
if start <= *pos && pos < &span.end {
|
|
if start < *pos {
|
|
result.push((Span::new(start, *pos), false));
|
|
}
|
|
let span_str = &line[pos - global_span_offset..span.end - global_span_offset];
|
|
let end = span_str
|
|
.chars()
|
|
.next()
|
|
.map(|c| pos + get_char_length(c))
|
|
.unwrap_or(pos + 1);
|
|
result.push((Span::new(*pos, end), true));
|
|
start = end;
|
|
}
|
|
}
|
|
if start < span.end {
|
|
result.push((Span::new(start, span.end), false));
|
|
}
|
|
result
|
|
}
|
|
|
|
fn find_matching_brackets(
|
|
line: &str,
|
|
working_set: &StateWorkingSet,
|
|
block: &Block,
|
|
global_span_offset: usize,
|
|
global_cursor_offset: usize,
|
|
) -> Vec<usize> {
|
|
const BRACKETS: &str = "{}[]()";
|
|
|
|
// calculate first bracket position
|
|
let global_end_offset = line.len() + global_span_offset;
|
|
let global_bracket_pos =
|
|
if global_cursor_offset == global_end_offset && global_end_offset > global_span_offset {
|
|
// cursor is at the end of a non-empty string -- find block end at the previous position
|
|
if let Some(last_char) = line.chars().last() {
|
|
global_cursor_offset - get_char_length(last_char)
|
|
} else {
|
|
global_cursor_offset
|
|
}
|
|
} else {
|
|
// cursor is in the middle of a string -- find block end at the current position
|
|
global_cursor_offset
|
|
};
|
|
|
|
// check that position contains bracket
|
|
let match_idx = global_bracket_pos - global_span_offset;
|
|
if match_idx >= line.len()
|
|
|| !BRACKETS.contains(get_char_at_index(line, match_idx).unwrap_or_default())
|
|
{
|
|
return Vec::new();
|
|
}
|
|
|
|
// find matching bracket by finding matching block end
|
|
let matching_block_end = find_matching_block_end_in_block(
|
|
line,
|
|
working_set,
|
|
block,
|
|
global_span_offset,
|
|
global_bracket_pos,
|
|
);
|
|
if let Some(pos) = matching_block_end {
|
|
let matching_idx = pos - global_span_offset;
|
|
if BRACKETS.contains(get_char_at_index(line, matching_idx).unwrap_or_default()) {
|
|
return if global_bracket_pos < pos {
|
|
vec![global_bracket_pos, pos]
|
|
} else {
|
|
vec![pos, global_bracket_pos]
|
|
};
|
|
}
|
|
}
|
|
Vec::new()
|
|
}
|
|
|
|
fn find_matching_block_end_in_block(
|
|
line: &str,
|
|
working_set: &StateWorkingSet,
|
|
block: &Block,
|
|
global_span_offset: usize,
|
|
global_cursor_offset: usize,
|
|
) -> Option<usize> {
|
|
for p in &block.pipelines {
|
|
for e in &p.elements {
|
|
if e.expr.span.contains(global_cursor_offset) {
|
|
if let Some(pos) = find_matching_block_end_in_expr(
|
|
line,
|
|
working_set,
|
|
&e.expr,
|
|
global_span_offset,
|
|
global_cursor_offset,
|
|
) {
|
|
return Some(pos);
|
|
}
|
|
}
|
|
|
|
if let Some(redirection) = e.redirection.as_ref() {
|
|
match redirection {
|
|
PipelineRedirection::Single { target, .. }
|
|
| PipelineRedirection::Separate { out: target, .. }
|
|
| PipelineRedirection::Separate { err: target, .. }
|
|
if target.span().contains(global_cursor_offset) =>
|
|
{
|
|
if let Some(pos) = target.expr().and_then(|expr| {
|
|
find_matching_block_end_in_expr(
|
|
line,
|
|
working_set,
|
|
expr,
|
|
global_span_offset,
|
|
global_cursor_offset,
|
|
)
|
|
}) {
|
|
return Some(pos);
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
fn find_matching_block_end_in_expr(
|
|
line: &str,
|
|
working_set: &StateWorkingSet,
|
|
expression: &Expression,
|
|
global_span_offset: usize,
|
|
global_cursor_offset: usize,
|
|
) -> Option<usize> {
|
|
macro_rules! find_in_expr_or_continue {
|
|
($inner_expr:ident) => {
|
|
if let Some(pos) = find_matching_block_end_in_expr(
|
|
line,
|
|
working_set,
|
|
$inner_expr,
|
|
global_span_offset,
|
|
global_cursor_offset,
|
|
) {
|
|
return Some(pos);
|
|
}
|
|
};
|
|
}
|
|
|
|
if expression.span.contains(global_cursor_offset) && expression.span.start >= global_span_offset
|
|
{
|
|
let expr_first = expression.span.start;
|
|
let span_str = &line
|
|
[expression.span.start - global_span_offset..expression.span.end - global_span_offset];
|
|
let expr_last = span_str
|
|
.chars()
|
|
.last()
|
|
.map(|c| expression.span.end - get_char_length(c))
|
|
.unwrap_or(expression.span.start);
|
|
|
|
return match &expression.expr {
|
|
Expr::Bool(_) => None,
|
|
Expr::Int(_) => None,
|
|
Expr::Float(_) => None,
|
|
Expr::Binary(_) => None,
|
|
Expr::Range(..) => None,
|
|
Expr::Var(_) => None,
|
|
Expr::VarDecl(_) => None,
|
|
Expr::ExternalCall(..) => None,
|
|
Expr::Operator(_) => None,
|
|
Expr::UnaryNot(_) => None,
|
|
Expr::Keyword(..) => None,
|
|
Expr::ValueWithUnit(..) => None,
|
|
Expr::DateTime(_) => None,
|
|
Expr::Filepath(_, _) => None,
|
|
Expr::Directory(_, _) => None,
|
|
Expr::GlobPattern(_, _) => None,
|
|
Expr::String(_) => None,
|
|
Expr::CellPath(_) => None,
|
|
Expr::ImportPattern(_) => None,
|
|
Expr::Overlay(_) => None,
|
|
Expr::Signature(_) => None,
|
|
Expr::MatchBlock(_) => None,
|
|
Expr::Nothing => None,
|
|
Expr::Garbage => None,
|
|
Expr::Spread(_) => None,
|
|
|
|
Expr::Table(hdr, rows) => {
|
|
if expr_last == global_cursor_offset {
|
|
// cursor is at table end
|
|
Some(expr_first)
|
|
} else if expr_first == global_cursor_offset {
|
|
// cursor is at table start
|
|
Some(expr_last)
|
|
} else {
|
|
// cursor is inside table
|
|
for inner_expr in hdr {
|
|
find_in_expr_or_continue!(inner_expr);
|
|
}
|
|
for row in rows {
|
|
for inner_expr in row {
|
|
find_in_expr_or_continue!(inner_expr);
|
|
}
|
|
}
|
|
None
|
|
}
|
|
}
|
|
|
|
Expr::Record(exprs) => {
|
|
if expr_last == global_cursor_offset {
|
|
// cursor is at record end
|
|
Some(expr_first)
|
|
} else if expr_first == global_cursor_offset {
|
|
// cursor is at record start
|
|
Some(expr_last)
|
|
} else {
|
|
// cursor is inside record
|
|
for expr in exprs {
|
|
match expr {
|
|
RecordItem::Pair(k, v) => {
|
|
find_in_expr_or_continue!(k);
|
|
find_in_expr_or_continue!(v);
|
|
}
|
|
RecordItem::Spread(_, record) => {
|
|
find_in_expr_or_continue!(record);
|
|
}
|
|
}
|
|
}
|
|
None
|
|
}
|
|
}
|
|
|
|
Expr::Call(call) => {
|
|
for arg in &call.arguments {
|
|
let opt_expr = match arg {
|
|
Argument::Named((_, _, opt_expr)) => opt_expr.as_ref(),
|
|
Argument::Positional(inner_expr) => Some(inner_expr),
|
|
Argument::Unknown(inner_expr) => Some(inner_expr),
|
|
Argument::Spread(inner_expr) => Some(inner_expr),
|
|
};
|
|
|
|
if let Some(inner_expr) = opt_expr {
|
|
find_in_expr_or_continue!(inner_expr);
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
Expr::FullCellPath(b) => find_matching_block_end_in_expr(
|
|
line,
|
|
working_set,
|
|
&b.head,
|
|
global_span_offset,
|
|
global_cursor_offset,
|
|
),
|
|
|
|
Expr::BinaryOp(lhs, op, rhs) => {
|
|
find_in_expr_or_continue!(lhs);
|
|
find_in_expr_or_continue!(op);
|
|
find_in_expr_or_continue!(rhs);
|
|
None
|
|
}
|
|
|
|
Expr::Block(block_id)
|
|
| Expr::Closure(block_id)
|
|
| Expr::RowCondition(block_id)
|
|
| Expr::Subexpression(block_id) => {
|
|
if expr_last == global_cursor_offset {
|
|
// cursor is at block end
|
|
Some(expr_first)
|
|
} else if expr_first == global_cursor_offset {
|
|
// cursor is at block start
|
|
Some(expr_last)
|
|
} else {
|
|
// cursor is inside block
|
|
let nested_block = working_set.get_block(*block_id);
|
|
find_matching_block_end_in_block(
|
|
line,
|
|
working_set,
|
|
nested_block,
|
|
global_span_offset,
|
|
global_cursor_offset,
|
|
)
|
|
}
|
|
}
|
|
|
|
Expr::StringInterpolation(inner_expr) => {
|
|
for inner_expr in inner_expr {
|
|
find_in_expr_or_continue!(inner_expr);
|
|
}
|
|
None
|
|
}
|
|
|
|
Expr::List(inner_expr) => {
|
|
if expr_last == global_cursor_offset {
|
|
// cursor is at list end
|
|
Some(expr_first)
|
|
} else if expr_first == global_cursor_offset {
|
|
// cursor is at list start
|
|
Some(expr_last)
|
|
} else {
|
|
// cursor is inside list
|
|
for inner_expr in inner_expr {
|
|
find_in_expr_or_continue!(inner_expr);
|
|
}
|
|
None
|
|
}
|
|
}
|
|
};
|
|
}
|
|
None
|
|
}
|
|
|
|
fn get_char_at_index(s: &str, index: usize) -> Option<char> {
|
|
s[index..].chars().next()
|
|
}
|
|
|
|
fn get_char_length(c: char) -> usize {
|
|
c.to_string().len()
|
|
}
|