nushell/crates/nu-cli/src/commands/shells.rs

63 lines
1.5 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
2019-08-07 19:49:11 +02:00
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{Signature, TaggedDictBuilder};
use std::sync::atomic::Ordering;
2019-08-07 19:49:11 +02:00
pub struct Shells;
impl WholeStreamCommand for Shells {
fn name(&self) -> &str {
"shells"
}
fn signature(&self) -> Signature {
Signature::build("shells")
}
fn usage(&self) -> &str {
"Display the list of current shells."
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
shells(args, registry)
}
}
fn shells(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
2019-08-07 19:49:11 +02:00
let mut shells_out = VecDeque::new();
let tag = args.call_info.name_tag;
2019-08-07 19:49:11 +02:00
Restructure and streamline token expansion (#1123) Restructure and streamline token expansion The purpose of this commit is to streamline the token expansion code, by removing aspects of the code that are no longer relevant, removing pointless duplication, and eliminating the need to pass the same arguments to `expand_syntax`. The first big-picture change in this commit is that instead of a handful of `expand_` functions, which take a TokensIterator and ExpandContext, a smaller number of methods on the `TokensIterator` do the same job. The second big-picture change in this commit is fully eliminating the coloring traits, making coloring a responsibility of the base expansion implementations. This also means that the coloring tracer is merged into the expansion tracer, so you can follow a single expansion and see how the expansion process produced colored tokens. One side effect of this change is that the expander itself is marginally more error-correcting. The error correction works by switching from structured expansion to `BackoffColoringMode` when an unexpected token is found, which guarantees that all spans of the source are colored, but may not be the most optimal error recovery strategy. That said, because `BackoffColoringMode` only extends as far as a closing delimiter (`)`, `]`, `}`) or pipe (`|`), it does result in fairly granular correction strategy. The current code still produces an `Err` (plus a complete list of colored shapes) from the parsing process if any errors are encountered, but this could easily be addressed now that the underlying expansion is error-correcting. This commit also colors any spans that are syntax errors in red, and causes the parser to include some additional information about what tokens were expected at any given point where an error was encountered, so that completions and hinting could be more robust in the future. Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com> Co-authored-by: Andrés N. Robalino <andres@androbtech.com>
2020-01-21 23:45:03 +01:00
for (index, shell) in args.shell_manager.shells.lock().iter().enumerate() {
let mut dict = TaggedDictBuilder::new(&tag);
2019-08-10 22:18:14 +02:00
if index == (*args.shell_manager.current_shell).load(Ordering::SeqCst) {
2020-04-06 19:30:54 +02:00
dict.insert_untagged("active", "X".to_string());
2019-08-10 22:18:14 +02:00
} else {
2020-04-06 19:30:54 +02:00
dict.insert_untagged("active", " ".to_string());
2019-08-10 22:18:14 +02:00
}
dict.insert_untagged("name", shell.name());
dict.insert_untagged("path", shell.path());
2019-08-07 19:49:11 +02:00
shells_out.push_back(dict.into_value());
2019-08-07 19:49:11 +02:00
}
Futures v0.3 upgrade (#1344) * Upgrade futures, async-stream, and futures_codec These were the last three dependencies on futures-preview. `nu` itself is now fully dependent on `futures@0.3`, as opposed to `futures-preview` alpha. Because the update to `futures` from `0.3.0-alpha.19` to `0.3.0` removed the `Stream` implementation of `VecDeque` ([changelog][changelog]), most commands that convert a `VecDeque` to an `OutputStream` broke and had to be fixed. The current solution is to now convert `VecDeque`s to a `Stream` via `futures::stream::iter`. However, it may be useful for `futures` to create an `IntoStream` trait, implemented on the `std::collections` (or really any `IntoIterator`). If something like this happends, it may be worthwhile to update the trait implementations on `OutputStream` and refactor these commands again. While upgrading `futures_codec`, we remove a custom implementation of `LinesCodec`, as one has been added to the library. There's also a small refactor to make the stream output more idiomatic. [changelog]: https://github.com/rust-lang/futures-rs/blob/master/CHANGELOG.md#030---2019-11-5 * Upgrade sys & ps plugin dependencies They were previously dependent on `futures-preview`, and `nu_plugin_ps` was dependent on an old version of `futures-timer`. * Remove dependency on futures-timer from nu * Update Cargo.lock * Fix formatting * Revert fmt regressions CI is still on 1.40.0, but the latest rustfmt v1.41.0 has changes to the `val @ pattern` syntax, causing the linting job to fail. * Fix clippy warnings
2020-02-06 04:46:48 +01:00
Ok(shells_out.into())
2019-08-07 19:49:11 +02:00
}
#[cfg(test)]
mod tests {
use super::Shells;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Shells {})
}
}