nushell/src/shell/shell_manager.rs

102 lines
2.6 KiB
Rust
Raw Normal View History

2019-08-15 07:02:02 +02:00
use crate::commands::command::EvaluatedWholeStreamCommandArgs;
2019-08-07 19:49:11 +02:00
use crate::errors::ShellError;
2019-08-10 07:02:15 +02:00
use crate::prelude::*;
2019-08-07 19:49:11 +02:00
use crate::shell::filesystem_shell::FilesystemShell;
use crate::shell::shell::Shell;
2019-08-09 22:49:43 +02:00
use crate::stream::OutputStream;
2019-08-07 19:49:11 +02:00
use std::error::Error;
use std::sync::{Arc, Mutex};
#[derive(Clone)]
pub struct ShellManager {
2019-08-09 06:51:21 +02:00
crate shells: Arc<Mutex<Vec<Box<dyn Shell + Send>>>>,
2019-08-07 19:49:11 +02:00
}
impl ShellManager {
2019-08-10 07:02:15 +02:00
pub fn basic(commands: CommandRegistry) -> Result<ShellManager, Box<dyn Error>> {
2019-08-07 19:49:11 +02:00
Ok(ShellManager {
2019-08-10 07:02:15 +02:00
shells: Arc::new(Mutex::new(vec![Box::new(FilesystemShell::basic(
commands,
)?)])),
2019-08-07 19:49:11 +02:00
})
}
2019-08-09 06:51:21 +02:00
pub fn push(&mut self, shell: Box<dyn Shell + Send>) {
2019-08-07 19:49:11 +02:00
self.shells.lock().unwrap().push(shell);
self.set_path(self.path());
}
pub fn pop(&mut self) {
self.shells.lock().unwrap().pop();
}
pub fn is_empty(&self) -> bool {
self.shells.lock().unwrap().is_empty()
}
pub fn path(&self) -> String {
self.shells.lock().unwrap().last().unwrap().path()
}
pub fn set_path(&mut self, path: String) {
self.shells
.lock()
.unwrap()
.last_mut()
.unwrap()
.set_path(path)
}
2019-08-09 21:42:23 +02:00
pub fn complete(
&self,
line: &str,
pos: usize,
ctx: &rustyline::Context<'_>,
) -> Result<(usize, Vec<rustyline::completion::Pair>), rustyline::error::ReadlineError> {
self.shells
.lock()
.unwrap()
.last()
.unwrap()
.complete(line, pos, ctx)
}
2019-08-07 19:49:11 +02:00
pub fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option<String> {
self.shells
.lock()
.unwrap()
.last()
.unwrap()
.hint(line, pos, ctx)
}
pub fn next(&mut self) {
{
let mut x = self.shells.lock().unwrap();
2019-08-10 22:18:14 +02:00
let shell = x.remove(0);
x.push(shell);
2019-08-07 19:49:11 +02:00
}
self.set_path(self.path());
}
pub fn prev(&mut self) {
{
let mut x = self.shells.lock().unwrap();
2019-08-10 22:18:14 +02:00
let shell = x.pop().unwrap();
x.insert(0, shell);
2019-08-07 19:49:11 +02:00
}
self.set_path(self.path());
}
2019-08-15 07:02:02 +02:00
pub fn ls(&self, args: EvaluatedWholeStreamCommandArgs) -> Result<OutputStream, ShellError> {
2019-08-07 19:49:11 +02:00
let env = self.shells.lock().unwrap();
2019-08-09 07:36:43 +02:00
env.last().unwrap().ls(args)
2019-08-07 19:49:11 +02:00
}
2019-08-15 07:02:02 +02:00
pub fn cd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result<OutputStream, ShellError> {
2019-08-07 19:49:11 +02:00
let env = self.shells.lock().unwrap();
2019-08-09 21:42:23 +02:00
env.last().unwrap().cd(args)
2019-08-07 19:49:11 +02:00
}
}