From fef447a659cb069f178ac4fce2ffec9175ae8223 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 19 Aug 2019 19:29:27 +1200 Subject: [PATCH 1/3] Fix shell-ring to rotate as expected --- src/commands/shells.rs | 4 +-- src/parser/deserializer.rs | 2 -- src/prelude.rs | 2 +- src/shell/shell_manager.rs | 53 +++++++++++++++++++------------------- 4 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/commands/shells.rs b/src/commands/shells.rs index 0c59822965..6caf53a240 100644 --- a/src/commands/shells.rs +++ b/src/commands/shells.rs @@ -6,12 +6,10 @@ pub fn shells(args: CommandArgs, _registry: &CommandRegistry) -> Result>>>, } impl ShellManager { pub fn basic(commands: CommandRegistry) -> Result> { Ok(ShellManager { + current_shell: 0, shells: Arc::new(Mutex::new(vec![Box::new(FilesystemShell::basic( commands, )?)])), @@ -23,11 +25,17 @@ impl ShellManager { pub fn push(&mut self, shell: Box) { self.shells.lock().unwrap().push(shell); + self.current_shell = self.shells.lock().unwrap().len() - 1; self.set_path(self.path()); } pub fn pop(&mut self) { self.shells.lock().unwrap().pop(); + let new_len = self.shells.lock().unwrap().len(); + if new_len > 0 { + self.current_shell = new_len - 1; + self.set_path(self.path()); + } } pub fn is_empty(&self) -> bool { @@ -35,16 +43,11 @@ impl ShellManager { } pub fn path(&self) -> String { - self.shells.lock().unwrap().last().unwrap().path() + self.shells.lock().unwrap()[self.current_shell].path() } pub fn set_path(&mut self, path: String) { - self.shells - .lock() - .unwrap() - .last_mut() - .unwrap() - .set_path(path) + self.shells.lock().unwrap()[self.current_shell].set_path(path) } pub fn complete( @@ -53,37 +56,33 @@ impl ShellManager { pos: usize, ctx: &rustyline::Context<'_>, ) -> Result<(usize, Vec), rustyline::error::ReadlineError> { - self.shells - .lock() - .unwrap() - .last() - .unwrap() - .complete(line, pos, ctx) + self.shells.lock().unwrap()[self.current_shell].complete(line, pos, ctx) } pub fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option { - self.shells - .lock() - .unwrap() - .last() - .unwrap() - .hint(line, pos, ctx) + self.shells.lock().unwrap()[self.current_shell].hint(line, pos, ctx) } pub fn next(&mut self) { { - let mut x = self.shells.lock().unwrap(); - let shell = x.remove(0); - x.push(shell); + let shell_len = self.shells.lock().unwrap().len(); + if self.current_shell == (shell_len - 1) { + self.current_shell = 0; + } else { + self.current_shell += 1; + } } self.set_path(self.path()); } pub fn prev(&mut self) { { - let mut x = self.shells.lock().unwrap(); - let shell = x.pop().unwrap(); - x.insert(0, shell); + let shell_len = self.shells.lock().unwrap().len(); + if self.current_shell == 0 { + self.current_shell = shell_len - 1; + } else { + self.current_shell -= 1; + } } self.set_path(self.path()); } @@ -91,11 +90,11 @@ impl ShellManager { pub fn ls(&self, args: EvaluatedWholeStreamCommandArgs) -> Result { let env = self.shells.lock().unwrap(); - env.last().unwrap().ls(args) + env[self.current_shell].ls(args) } pub fn cd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result { let env = self.shells.lock().unwrap(); - env.last().unwrap().cd(args) + env[self.current_shell].cd(args) } } From 953d28ef747ce5766209cc2dc5b049b42588ca05 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 19 Aug 2019 20:07:55 +1200 Subject: [PATCH 2/3] Fix push/pop for shell manager --- src/commands/classified.rs | 24 ++++++++++++++---------- src/shell/shell_manager.rs | 25 ++++++++++++++++++------- tests/command_enter_test.rs | 2 ++ 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/commands/classified.rs b/src/commands/classified.rs index 45262bfee7..309af24418 100644 --- a/src/commands/classified.rs +++ b/src/commands/classified.rs @@ -140,19 +140,21 @@ impl InternalCommand { } CommandAction::Exit => std::process::exit(0), CommandAction::EnterValueShell(value) => { - context.shell_manager.push(Box::new(ValueShell::new(value))); + context + .shell_manager + .insert_at_current(Box::new(ValueShell::new(value))); } CommandAction::EnterShell(location) => { let path = std::path::Path::new(&location); if path.is_dir() { // If it's a directory, add a new filesystem shell - context - .shell_manager - .push(Box::new(FilesystemShell::with_location( + context.shell_manager.insert_at_current(Box::new( + FilesystemShell::with_location( location, context.registry().clone(), - )?)); + )?, + )); } else { // If it's a file, attempt to open the file as a value and enter it let cwd = context.shell_manager.path(); @@ -180,11 +182,13 @@ impl InternalCommand { Span::unknown(), )?; - context.shell_manager.push(Box::new(ValueShell::new(value))); + context + .shell_manager + .insert_at_current(Box::new(ValueShell::new(value))); } - value => context - .shell_manager - .push(Box::new(ValueShell::new(value.tagged(contents_tag)))), + value => context.shell_manager.insert_at_current(Box::new( + ValueShell::new(value.tagged(contents_tag)), + )), } } } @@ -195,7 +199,7 @@ impl InternalCommand { context.shell_manager.next(); } CommandAction::LeaveShell => { - context.shell_manager.pop(); + context.shell_manager.remove_at_current(); if context.shell_manager.is_empty() { std::process::exit(0); } diff --git a/src/shell/shell_manager.rs b/src/shell/shell_manager.rs index 18ccd94b91..d8f63ceb0d 100644 --- a/src/shell/shell_manager.rs +++ b/src/shell/shell_manager.rs @@ -23,19 +23,30 @@ impl ShellManager { }) } - pub fn push(&mut self, shell: Box) { + pub fn insert_at_current(&mut self, shell: Box) { self.shells.lock().unwrap().push(shell); self.current_shell = self.shells.lock().unwrap().len() - 1; self.set_path(self.path()); } - pub fn pop(&mut self) { - self.shells.lock().unwrap().pop(); - let new_len = self.shells.lock().unwrap().len(); - if new_len > 0 { - self.current_shell = new_len - 1; - self.set_path(self.path()); + pub fn remove_at_current(&mut self) { + { + let mut shells = self.shells.lock().unwrap(); + if shells.len() > 0 { + if self.current_shell == shells.len() - 1 { + shells.pop(); + let new_len = shells.len(); + if new_len > 0 { + self.current_shell = new_len - 1; + } else { + return; + } + } else { + shells.remove(self.current_shell); + } + } } + self.set_path(self.path()); } pub fn is_empty(&self) -> bool { diff --git a/tests/command_enter_test.rs b/tests/command_enter_test.rs index 051d19467f..fd11113e35 100644 --- a/tests/command_enter_test.rs +++ b/tests/command_enter_test.rs @@ -45,9 +45,11 @@ fn knows_the_filesystems_entered() { n mv andres.nu ../expected/andres.nu exit + n cd .. rm red_pill --recursive exit + n rm blue_pill --recursive exit "# From 51ec48f357c38eff76a6db7a0a28d0a04fb1d413 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Tue, 20 Aug 2019 04:40:47 +1200 Subject: [PATCH 3/3] Update enter test --- tests/command_enter_test.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/command_enter_test.rs b/tests/command_enter_test.rs index fd11113e35..077c1f66c3 100644 --- a/tests/command_enter_test.rs +++ b/tests/command_enter_test.rs @@ -45,7 +45,6 @@ fn knows_the_filesystems_entered() { n mv andres.nu ../expected/andres.nu exit - n cd .. rm red_pill --recursive exit