nu_cli refactor in preparation for a crate called nu_command (#2907)

* move basic_shell_manager to nu-engine

* move basic_evaluation_context to nu-engine

* fix failing test in feature which commands/classified/external.rs
This commit is contained in:
Michael Angerman
2021-01-10 20:58:15 -08:00
committed by GitHub
parent 231a445809
commit 481c6d4511
13 changed files with 60 additions and 40 deletions

View File

@ -0,0 +1,20 @@
use crate::basic_shell_manager;
use crate::env::basic_host::BasicHost;
use crate::EvaluationContext;
use crate::Scope;
use parking_lot::Mutex;
use std::error::Error;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
pub fn basic_evaluation_context() -> Result<EvaluationContext, Box<dyn Error>> {
Ok(EvaluationContext {
scope: Scope::new(),
host: Arc::new(parking_lot::Mutex::new(Box::new(BasicHost))),
current_errors: Arc::new(Mutex::new(vec![])),
ctrl_c: Arc::new(AtomicBool::new(false)),
user_recently_used_autoenv_untrust: Arc::new(AtomicBool::new(false)),
shell_manager: basic_shell_manager::basic_shell_manager()?,
windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())),
})
}

View File

@ -0,0 +1,14 @@
use crate::filesystem::filesystem_shell::FilesystemShell;
use crate::shell::shell_manager::ShellManager;
use parking_lot::Mutex;
use std::error::Error;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
pub fn basic_shell_manager() -> Result<ShellManager, Box<dyn Error>> {
Ok(ShellManager {
current_shell: Arc::new(AtomicUsize::new(0)),
shells: Arc::new(Mutex::new(vec![Box::new(FilesystemShell::basic()?)])),
})
}

77
crates/nu-engine/src/env/basic_host.rs vendored Normal file
View File

@ -0,0 +1,77 @@
use crate::Host;
use nu_protocol::{errln, outln};
use std::ffi::OsString;
#[derive(Debug)]
pub struct BasicHost;
impl Host for BasicHost {
fn stdout(&mut self, out: &str) {
match out {
"\n" => outln!(""),
other => outln!("{}", other),
}
}
fn stderr(&mut self, out: &str) {
match out {
"\n" => errln!(""),
other => errln!("{}", other),
}
}
#[allow(unused_variables)]
fn vars(&mut self) -> Vec<(String, String)> {
#[cfg(not(target_arch = "wasm32"))]
{
std::env::vars().collect::<Vec<_>>()
}
#[cfg(target_arch = "wasm32")]
{
vec![]
}
}
#[allow(unused_variables)]
fn env_get(&mut self, key: OsString) -> Option<OsString> {
#[cfg(not(target_arch = "wasm32"))]
{
std::env::var_os(key)
}
#[cfg(target_arch = "wasm32")]
{
None
}
}
#[allow(unused_variables)]
fn env_set(&mut self, key: OsString, value: OsString) {
#[cfg(not(target_arch = "wasm32"))]
{
std::env::set_var(key, value);
}
}
#[allow(unused_variables)]
fn env_rm(&mut self, key: OsString) {
#[cfg(not(target_arch = "wasm32"))]
{
std::env::remove_var(key);
}
}
fn out_termcolor(&self) -> termcolor::StandardStream {
termcolor::StandardStream::stdout(termcolor::ColorChoice::Auto)
}
fn err_termcolor(&self) -> termcolor::StandardStream {
termcolor::StandardStream::stderr(termcolor::ColorChoice::Auto)
}
fn width(&self) -> usize {
let (mut term_width, _) = term_size::dimensions().unwrap_or((80, 20));
term_width -= 1;
term_width
}
}

View File

@ -1,2 +1,3 @@
pub(crate) mod basic_host;
pub(crate) mod environment;
pub(crate) mod host;

View File

@ -1,3 +1,5 @@
pub mod basic_evaluation_context;
pub mod basic_shell_manager;
mod call_info;
mod command_args;
pub mod deserializer;
@ -13,6 +15,8 @@ pub mod plugin;
pub mod shell;
mod whole_stream_command;
pub use crate::basic_evaluation_context::basic_evaluation_context;
pub use crate::basic_shell_manager::basic_shell_manager;
pub use crate::call_info::UnevaluatedCallInfo;
pub use crate::command_args::{
CommandArgs, EvaluatedCommandArgs, EvaluatedWholeStreamCommandArgs, RawCommandArgs,