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? {
ReturnSuccess::Action(action) => match action {
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) => {
context.add_span_source(uuid, span_source);

View File

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

View File

@ -10,6 +10,6 @@ where
{
fn ls(&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 set_path(&mut self, path: &std::path::PathBuf);
fn path(&self) -> String;
fn set_path(&mut self, path: String);
}

View File

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