Skip to content

Commit

Permalink
Implement Filename Escaper
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick committed May 31, 2017
1 parent 5614d77 commit 29497ad
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
30 changes: 29 additions & 1 deletion src/shell/completer.rs
@@ -1,4 +1,32 @@
use liner::Completer;
use liner::{Completer, FilenameCompleter};

pub struct IonFileCompleter {
inner: FilenameCompleter
}

impl IonFileCompleter {
pub fn new(path: Option<&str>) -> IonFileCompleter {
IonFileCompleter { inner: FilenameCompleter::new(path) }
}
}

impl Completer for IonFileCompleter {
fn completions(&self, start: &str) -> Vec<String> {
self.inner.completions(start).iter().map(|x| escape(x.as_str())).collect()
}
}

fn escape(input: &str) -> String {
let mut output = Vec::with_capacity(input.len());
for character in input.bytes() {
match character {
b'(' | b')' | b'[' | b']' => output.push(b'\\'),
_ => ()
}
output.push(character);
}
unsafe { String::from_utf8_unchecked(output) }
}

/// A completer that combines suggestions from multiple completers.
#[derive(Clone, Eq, PartialEq)]
Expand Down
12 changes: 6 additions & 6 deletions src/shell/mod.rs
Expand Up @@ -24,12 +24,12 @@ use std::time::SystemTime;
use std::iter::FromIterator;
use smallvec::SmallVec;

use liner::{Context, CursorPosition, Event, EventKind, FilenameCompleter, BasicCompleter};
use liner::{Context, CursorPosition, Event, EventKind, BasicCompleter};

use builtins::*;
use types::*;
use smallstring::SmallString;
use self::completer::MultiCompleter;
use self::completer::{MultiCompleter, IonFileCompleter};
use self::directory_stack::DirectoryStack;
use self::flow_control::{FlowControl, Function};
use self::variables::Variables;
Expand Down Expand Up @@ -105,7 +105,7 @@ impl<'a> Shell<'a> {
if filename {
if let Ok(current_dir) = env::current_dir() {
if let Some(url) = current_dir.to_str() {
let completer = FilenameCompleter::new(Some(url));
let completer = IonFileCompleter::new(Some(url));
mem::replace(&mut editor.context().completer, Some(Box::new(completer)));
}
}
Expand All @@ -116,13 +116,13 @@ impl<'a> Shell<'a> {
Ok(val) => {
if cfg!(unix) {
// UNIX systems separate paths with the `:` character.
val.split(':').map(|x| FilenameCompleter::new(Some(x))).collect::<Vec<_>>()
val.split(':').map(|x| IonFileCompleter::new(Some(x))).collect::<Vec<_>>()
} else {
// Redox and Windows use the `;` character to separate paths
val.split(';').map(|x| FilenameCompleter::new(Some(x))).collect::<Vec<_>>()
val.split(';').map(|x| IonFileCompleter::new(Some(x))).collect::<Vec<_>>()
}
},
Err(_) => vec![FilenameCompleter::new(Some("/bin/"))],
Err(_) => vec![IonFileCompleter::new(Some("/bin/"))],
};

// Creates a list of definitions from the shell environment that will be used
Expand Down

0 comments on commit 29497ad

Please sign in to comment.