diff --git a/src/commands/cd.rs b/src/commands/cd.rs index 399610b8a..78c9d053c 100644 --- a/src/commands/cd.rs +++ b/src/commands/cd.rs @@ -1,6 +1,7 @@ use crate::errors::ShellError; use crate::prelude::*; use derive_new::new; +use std::env; #[derive(new)] pub struct Cd; @@ -17,6 +18,7 @@ impl crate::Command for Cd { let mut stream = VecDeque::new(); let path = dunce::canonicalize(cwd.join(&target).as_path())?; + let _ = env::set_current_dir(&path); stream.push_back(ReturnValue::change_cwd(path)); Ok(stream) } diff --git a/src/parser/completer.rs b/src/parser/completer.rs index 8591475e6..f0efbf751 100644 --- a/src/parser/completer.rs +++ b/src/parser/completer.rs @@ -15,7 +15,7 @@ impl completion::Completer for Completer { _pos: usize, _ctx: &Context<'_>, ) -> rustyline::Result<(usize, Vec)> { - let pairs = self + let pairs: Vec = self .commands .keys() .map(|k| completion::Pair { diff --git a/src/shell/completer.rs b/src/shell/completer.rs index 0f68b3354..f693aa274 100644 --- a/src/shell/completer.rs +++ b/src/shell/completer.rs @@ -1,26 +1,48 @@ -use rustyline::completion::{Candidate, Completer}; +use rustyline::completion::Completer; +use rustyline::completion::{self, FilenameCompleter}; use rustyline::line_buffer::LineBuffer; -#[derive(Debug)] -crate struct NuCompleter; +crate struct NuCompleter { + pub file_completer: FilenameCompleter, +} impl Completer for NuCompleter { - type Candidate = NuPair; + type Candidate = completion::Pair; fn complete( &self, - _line: &str, - _pos: usize, - _context: &rustyline::Context, - ) -> rustyline::Result<(usize, Vec)> { - Ok(( - 0, - vec![ - NuPair("exit", "exit"), - NuPair("ls", "ls"), - NuPair("ps", "ps"), - ], - )) + line: &str, + pos: usize, + context: &rustyline::Context, + ) -> rustyline::Result<(usize, Vec)> { + let mut pairs = vec![ + completion::Pair { + display: "exit".to_string(), + replacement: "exit".to_string(), + }, + completion::Pair { + display: "ls".to_string(), + replacement: "ls".to_string(), + }, + completion::Pair { + display: "ps".to_string(), + replacement: "ps".to_string(), + }, + ]; + + let mut completions = self.file_completer.complete(line, pos, context)?.1; + completions.append(&mut pairs); + + let line_chars: Vec<_> = line.chars().collect(); + let mut replace_pos = pos; + while replace_pos > 0 { + if line_chars[replace_pos - 1] == ' ' { + break; + } + replace_pos -= 1; + } + + Ok((replace_pos, completions)) } fn update(&self, line: &mut LineBuffer, start: usize, elected: &str) { @@ -28,14 +50,3 @@ impl Completer for NuCompleter { line.replace(start..end, elected) } } - -crate struct NuPair(&'static str, &'static str); - -impl Candidate for NuPair { - fn display(&self) -> &str { - self.0 - } - fn replacement(&self) -> &str { - self.1 - } -} diff --git a/src/shell/helper.rs b/src/shell/helper.rs index aaa5bd9f3..287c5e5b8 100644 --- a/src/shell/helper.rs +++ b/src/shell/helper.rs @@ -1,6 +1,6 @@ -use crate::shell::completer::{NuCompleter, NuPair}; +use crate::shell::completer::NuCompleter; -use rustyline::completion::Completer; +use rustyline::completion::{self, Completer, FilenameCompleter}; use rustyline::error::ReadlineError; use rustyline::highlight::{Highlighter, MatchingBracketHighlighter}; use rustyline::hint::{Hinter, HistoryHinter}; @@ -15,7 +15,9 @@ crate struct Helper { impl Helper { crate fn new() -> Helper { Helper { - completer: NuCompleter, + completer: NuCompleter { + file_completer: FilenameCompleter::new(), + }, highlighter: MatchingBracketHighlighter::new(), hinter: HistoryHinter {}, } @@ -23,14 +25,14 @@ impl Helper { } impl Completer for Helper { - type Candidate = NuPair; + type Candidate = completion::Pair; fn complete( &self, line: &str, pos: usize, ctx: &rustyline::Context<'_>, - ) -> Result<(usize, Vec), ReadlineError> { + ) -> Result<(usize, Vec), ReadlineError> { self.completer.complete(line, pos, ctx) } }