Add back in cd/ls and completions

This commit is contained in:
Jonathan Turner
2019-08-10 07:42:23 +12:00
parent cabd5bf009
commit 34759b7646
9 changed files with 122 additions and 122 deletions

View File

@ -1,5 +1,5 @@
use derive_new::new;
use rustyline::completion::{self, FilenameCompleter};
use rustyline::completion::{self, Completer, FilenameCompleter};
use rustyline::line_buffer::LineBuffer;
#[derive(new)]
@ -23,13 +23,12 @@ impl Into<completion::Pair> for CompletionPair {
}
impl NuCompleter {
/*
pub fn complete(
&self,
line: &str,
pos: usize,
context: &rustyline::Context,
) -> rustyline::Result<(usize, Vec<CompletionPair>)> {
) -> rustyline::Result<(usize, Vec<rustyline::completion::Pair>)> {
//let commands: Vec<String> = self.commands.keys().cloned().collect();
let mut completions = self.file_completer.complete(line, pos, context)?.1;
@ -89,7 +88,6 @@ impl NuCompleter {
Ok((replace_pos, completions))
}
*/
fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) {
let end = line.pos();

View File

@ -126,15 +126,15 @@ impl Shell for FilesystemShell {
Ok(shell_entries.to_output_stream())
}
fn cd(&self, call_info: CallInfo, _input: InputStream) -> Result<OutputStream, ShellError> {
let path = match call_info.args.nth(0) {
fn cd(&self, args: EvaluatedStaticCommandArgs) -> Result<OutputStream, ShellError> {
let path = match args.nth(0) {
None => match dirs::home_dir() {
Some(o) => o,
_ => {
return Err(ShellError::labeled_error(
"Can not change to home directory",
"can not go to home",
call_info.name_span,
args.call_info.name_span,
))
}
},
@ -158,11 +158,11 @@ impl Shell for FilesystemShell {
match std::env::set_current_dir(&path) {
Ok(_) => {}
Err(_) => {
if call_info.args.len() > 0 {
if args.len() > 0 {
return Err(ShellError::labeled_error(
"Can not change to directory",
"directory not found",
call_info.args.nth(0).unwrap().span().clone(),
args.nth(0).unwrap().span().clone(),
));
} else {
return Err(ShellError::string("Can not change to directory"));
@ -194,16 +194,14 @@ impl Shell for FilesystemShell {
self.path = path.to_string_lossy().to_string();
}
/*
fn complete(
&self,
line: &str,
pos: usize,
ctx: &rustyline::Context<'_>,
) -> Result<(usize, Vec<CompletionPair>), ReadlineError> {
) -> Result<(usize, Vec<rustyline::completion::Pair>), rustyline::error::ReadlineError> {
self.completer.complete(line, pos, ctx)
}
*/
fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option<String> {
self.hinter.hint(line, pos, ctx)

View File

@ -29,8 +29,7 @@ impl Completer for Helper {
pos: usize,
ctx: &rustyline::Context<'_>,
) -> Result<(usize, Vec<rustyline::completion::Pair>), ReadlineError> {
//FIXME: Add back completions
Ok((0, vec![]))
self.helper.complete(line, pos, ctx)
}
}

View File

@ -5,16 +5,16 @@ use crate::stream::{InputStream, OutputStream};
pub trait Shell {
fn name(&self) -> String;
fn ls(&self, args: EvaluatedStaticCommandArgs) -> Result<OutputStream, ShellError>;
fn cd(&self, call_info: CallInfo, input: InputStream) -> Result<OutputStream, ShellError>;
fn cd(&self, args: EvaluatedStaticCommandArgs) -> Result<OutputStream, ShellError>;
fn path(&self) -> String;
fn set_path(&mut self, path: String);
// fn complete(
// &self,
// line: &str,
// pos: usize,
// ctx: &rustyline::Context<'_>,
// ) -> Result<(usize, Vec<CompletionPair>), ReadlineError>;
fn complete(
&self,
line: &str,
pos: usize,
ctx: &rustyline::Context<'_>,
) -> Result<(usize, Vec<rustyline::completion::Pair>), rustyline::error::ReadlineError>;
fn hint(&self, _line: &str, _pos: usize, _ctx: &rustyline::Context<'_>) -> Option<String>;
}

View File

@ -44,19 +44,19 @@ impl ShellManager {
.set_path(path)
}
// pub fn complete(
// &self,
// line: &str,
// pos: usize,
// ctx: &rustyline::Context<'_>,
// ) -> Result<(usize, Vec<CompletionPair>), ReadlineError> {
// self.shells
// .lock()
// .unwrap()
// .last()
// .unwrap()
// .complete(line, pos, ctx)
// }
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)
}
pub fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option<String> {
self.shells
@ -90,9 +90,9 @@ impl ShellManager {
env.last().unwrap().ls(args)
}
pub fn cd(&self, call_info: CallInfo, input: InputStream) -> Result<OutputStream, ShellError> {
pub fn cd(&self, args: EvaluatedStaticCommandArgs) -> Result<OutputStream, ShellError> {
let env = self.shells.lock().unwrap();
env.last().unwrap().cd(call_info, input)
env.last().unwrap().cd(args)
}
}

View File

@ -64,8 +64,8 @@ impl Shell for ValueShell {
.to_output_stream())
}
fn cd(&self, call_info: CallInfo, _input: InputStream) -> Result<OutputStream, ShellError> {
let path = match call_info.args.nth(0) {
fn cd(&self, args: EvaluatedStaticCommandArgs) -> Result<OutputStream, ShellError> {
let path = match args.nth(0) {
None => "/".to_string(),
Some(v) => {
let target = v.as_string()?;
@ -100,13 +100,12 @@ impl Shell for ValueShell {
self.path = path.clone();
}
/*
fn complete(
&self,
line: &str,
pos: usize,
_ctx: &rustyline::Context<'_>,
) -> Result<(usize, Vec<CompletionPair>), ReadlineError> {
) -> Result<(usize, Vec<rustyline::completion::Pair>), rustyline::error::ReadlineError> {
let mut completions = vec![];
let mut possible_completion = vec![];
@ -147,7 +146,7 @@ impl Shell for ValueShell {
}
if matched {
completions.push(CompletionPair {
completions.push(rustyline::completion::Pair {
display: command.to_string(),
replacement: command.to_string(),
});
@ -155,7 +154,6 @@ impl Shell for ValueShell {
}
Ok((replace_pos, completions))
}
*/
fn hint(&self, _line: &str, _pos: usize, _ctx: &rustyline::Context<'_>) -> Option<String> {
None