Working shell manager

This commit is contained in:
Jonathan Turner 2019-08-07 16:11:05 +12:00
parent ab8176d4c7
commit e80b198a0b
4 changed files with 29 additions and 27 deletions

View File

@ -145,7 +145,9 @@ impl InternalCommand {
match item? { match item? {
ReturnSuccess::Action(action) => match action { ReturnSuccess::Action(action) => match action {
CommandAction::ChangePath(path) => { CommandAction::ChangePath(path) => {
context.shell_manager.set_path(&path); context
.shell_manager
.set_path(path.to_string_lossy().to_string());
} }
CommandAction::AddSpanSource(uuid, span_source) => { CommandAction::AddSpanSource(uuid, span_source) => {
context.add_span_source(uuid, span_source); context.add_span_source(uuid, span_source);

View File

@ -8,7 +8,7 @@ use rustyline::error::ReadlineError;
use rustyline::hint::{Hinter, HistoryHinter}; use rustyline::hint::{Hinter, HistoryHinter};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
pub struct FilesystemShell { pub struct FilesystemShell {
crate path: PathBuf, crate path: String,
completer: NuCompleter, completer: NuCompleter,
hinter: HistoryHinter, hinter: HistoryHinter,
} }
@ -30,7 +30,7 @@ impl FilesystemShell {
let path = std::env::current_dir()?; let path = std::env::current_dir()?;
Ok(FilesystemShell { Ok(FilesystemShell {
path, path: path.to_string_lossy().to_string(),
completer: NuCompleter { completer: NuCompleter {
file_completer: FilenameCompleter::new(), file_completer: FilenameCompleter::new(),
}, },
@ -38,9 +38,7 @@ impl FilesystemShell {
}) })
} }
pub fn with_location(location: String) -> Result<FilesystemShell, std::io::Error> { pub fn with_location(path: String) -> Result<FilesystemShell, std::io::Error> {
let path = std::path::PathBuf::from(location);
Ok(FilesystemShell { Ok(FilesystemShell {
path, path,
completer: NuCompleter { completer: NuCompleter {
@ -139,7 +137,8 @@ impl Shell for FilesystemShell {
}, },
Some(v) => { Some(v) => {
let target = v.as_string()?; let target = v.as_string()?;
match dunce::canonicalize(self.path.join(target).as_path()) { let path = PathBuf::from(self.path());
match dunce::canonicalize(path.join(target).as_path()) {
Ok(p) => p, Ok(p) => p,
Err(_) => { Err(_) => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
@ -171,11 +170,12 @@ impl Shell for FilesystemShell {
Ok(stream.into()) Ok(stream.into())
} }
fn path(&self) -> std::path::PathBuf { fn path(&self) -> String {
self.path.clone() self.path.clone()
} }
fn set_path(&mut self, path: &std::path::PathBuf) { fn set_path(&mut self, path: String) {
let _ = std::env::set_current_dir(&path);
self.path = path.clone(); self.path = path.clone();
} }
} }

View File

@ -10,6 +10,6 @@ where
{ {
fn ls(&self, call_info: CallInfo, input: InputStream) -> Result<OutputStream, ShellError>; fn ls(&self, call_info: CallInfo, input: InputStream) -> Result<OutputStream, ShellError>;
fn cd(&self, call_info: CallInfo, input: InputStream) -> Result<OutputStream, ShellError>; fn cd(&self, call_info: CallInfo, input: InputStream) -> Result<OutputStream, ShellError>;
fn path(&self) -> std::path::PathBuf; fn path(&self) -> String;
fn set_path(&mut self, path: &std::path::PathBuf); fn set_path(&mut self, path: String);
} }

View File

@ -21,21 +21,15 @@ impl ShellManager {
} }
pub fn push(&mut self, shell: Box<dyn Shell>) { pub fn push(&mut self, shell: Box<dyn Shell>) {
self.shells.lock().unwrap().push(shell) self.shells.lock().unwrap().push(shell);
self.set_path(self.path());
} }
pub fn path(&self) -> String { pub fn path(&self) -> String {
self.shells self.shells.lock().unwrap().last().unwrap().path()
.lock()
.unwrap()
.last()
.unwrap()
.path()
.display()
.to_string()
} }
pub fn set_path(&mut self, path: &std::path::PathBuf) { pub fn set_path(&mut self, path: String) {
self.shells self.shells
.lock() .lock()
.unwrap() .unwrap()
@ -68,16 +62,22 @@ impl ShellManager {
} }
pub fn next(&mut self) { pub fn next(&mut self) {
{
let mut x = self.shells.lock().unwrap(); let mut x = self.shells.lock().unwrap();
let shell = x.pop().unwrap(); let shell = x.pop().unwrap();
x.insert(0, shell); x.insert(0, shell);
} }
self.set_path(self.path());
}
pub fn prev(&mut self) { pub fn prev(&mut self) {
{
let mut x = self.shells.lock().unwrap(); let mut x = self.shells.lock().unwrap();
let shell = x.remove(0); let shell = x.remove(0);
x.push(shell); x.push(shell);
} }
self.set_path(self.path());
}
pub fn ls(&self, call_info: CallInfo, input: InputStream) -> Result<OutputStream, ShellError> { pub fn ls(&self, call_info: CallInfo, input: InputStream) -> Result<OutputStream, ShellError> {
let env = self.shells.lock().unwrap(); let env = self.shells.lock().unwrap();