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

@ -154,6 +154,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
command("from-xml", Box::new(from_xml::from_xml)),
command("ps", Box::new(ps::ps)),
command("ls", Box::new(ls::ls)),
command("cd", Box::new(cd::cd)),
command("size", Box::new(size::size)),
command("from-yaml", Box::new(from_yaml::from_yaml)),
command("enter", Box::new(enter::enter)),
@ -175,7 +176,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
command("sort-by", Box::new(sort_by::sort_by)),
command("tags", Box::new(tags::tags)),
static_command(Get),
static_command(Cd),
//static_command(Cd),
static_command(Remove),
static_command(Open),
static_command(Where),

View File

@ -50,7 +50,7 @@ crate mod vtable;
crate mod where_;
crate use autoview::Autoview;
crate use cd::Cd;
//crate use cd::Cd;
crate use clip::Clip;
crate use command::{
command, static_command, Command, CommandArgs, RawCommandArgs, StaticCommand,

View File

@ -4,83 +4,89 @@ use crate::prelude::*;
use std::env;
use std::path::PathBuf;
pub struct Cd;
// pub struct Cd;
#[derive(Deserialize)]
pub struct CdArgs {
target: Option<Tagged<PathBuf>>,
}
impl StaticCommand for Cd {
fn name(&self) -> &str {
"cd"
}
fn signature(&self) -> Signature {
Signature::build("cd")
.optional("target", SyntaxType::Path)
.filter()
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, cd)?.run()
// cd(args, registry)
}
}
pub fn cd(CdArgs { target }: CdArgs, context: RunnableContext) -> Result<OutputStream, ShellError> {
let cwd = context.cwd().to_path_buf();
let path = match &target {
None => match dirs::home_dir() {
Some(o) => o,
_ => {
return Err(ShellError::labeled_error(
"Can not change to home directory",
"can not go to home",
context.name,
))
}
},
Some(v) => {
// let target = v.item.as_string()?;
match dunce::canonicalize(cwd.join(&v.item()).as_path()) {
Ok(p) => p,
Err(_) => {
return Err(ShellError::labeled_error(
"Can not change to directory",
"directory not found",
v.span(),
));
}
}
}
};
let mut stream = VecDeque::new();
match env::set_current_dir(&path) {
Ok(_) => {}
Err(_) => {
if let Some(path) = target {
return Err(ShellError::labeled_error(
"Can not change to directory",
"directory not found",
path.span(),
));
} else {
return Err(ShellError::string("Can not change to directory"));
}
}
}
stream.push_back(ReturnSuccess::change_cwd(
path.to_string_lossy().to_string(),
));
Ok(stream.into())
// pub fn cd(args: CommandArgs) -> Result<OutputStream, ShellError> {
// args.shell_manager.cd(args.call_info, args.input)
// #[derive(Deserialize)]
// pub struct CdArgs {
// target: Option<Tagged<PathBuf>>,
// }
// impl StaticCommand for Cd {
// fn name(&self) -> &str {
// "cd"
// }
// fn signature(&self) -> Signature {
// Signature::build("cd")
// .optional("target", SyntaxType::Path)
// .filter()
// }
// fn run(
// &self,
// args: CommandArgs,
// registry: &CommandRegistry,
// ) -> Result<OutputStream, ShellError> {
// args.process(registry, cd)?.run()
// // cd(args, registry)
// }
// }
// pub fn cd(CdArgs { target }: CdArgs, context: RunnableContext) -> Result<OutputStream, ShellError> {
// let cwd = context.cwd().to_path_buf();
// let path = match &target {
// None => match dirs::home_dir() {
// Some(o) => o,
// _ => {
// return Err(ShellError::labeled_error(
// "Can not change to home directory",
// "can not go to home",
// context.name,
// ))
// }
// },
// Some(v) => {
// // let target = v.item.as_string()?;
// match dunce::canonicalize(cwd.join(&v.item()).as_path()) {
// Ok(p) => p,
// Err(_) => {
// return Err(ShellError::labeled_error(
// "Can not change to directory",
// "directory not found",
// v.span(),
// ));
// }
// }
// }
// };
// let mut stream = VecDeque::new();
// match env::set_current_dir(&path) {
// Ok(_) => {}
// Err(_) => {
// if let Some(path) = target {
// return Err(ShellError::labeled_error(
// "Can not change to directory",
// "directory not found",
// path.span(),
// ));
// } else {
// return Err(ShellError::string("Can not change to directory"));
// }
// }
// }
// stream.push_back(ReturnSuccess::change_cwd(
// path.to_string_lossy().to_string(),
// ));
// Ok(stream.into())
// // pub fn cd(args: CommandArgs) -> Result<OutputStream, ShellError> {
// // args.shell_manager.cd(args.call_info, args.input)
// }
pub fn cd(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let shell_manager = args.shell_manager.clone();
let args = args.evaluate_once(registry)?;
shell_manager.cd(args)
}

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