From b3d3245c99c36ee0d3bfc01355ec63dc8edc7bc2 Mon Sep 17 00:00:00 2001 From: v-alexjo Date: Thu, 17 Dec 2015 14:46:23 -0800 Subject: [PATCH] Readline logic added to prevent out of index when history queue is null --- .../readline.cs | 91 ++++++++++++------- 1 file changed, 60 insertions(+), 31 deletions(-) diff --git a/src/Microsoft.PowerShell.Linux.Host/readline.cs b/src/Microsoft.PowerShell.Linux.Host/readline.cs index 6f41c7636..94c2c7fdd 100644 --- a/src/Microsoft.PowerShell.Linux.Host/readline.cs +++ b/src/Microsoft.PowerShell.Linux.Host/readline.cs @@ -316,43 +316,58 @@ namespace Microsoft.PowerShell.Linux.Host /// /// The up arrow was pressed to retrieve history /// - private void OnUpArrow() { + private void OnDownArrow() { if ((previousKeyPress.Key != ConsoleKey.UpArrow && previousKeyPress.Key != ConsoleKey.DownArrow) || previousKeyPress.Key == ConsoleKey.Enter) { //first time getting the history using (Pipeline pipeline = this.runspace.CreatePipeline("Get-History")) { - historyResult = pipeline.Invoke(); - + historyResult = pipeline.Invoke(); + } + + try + { + BufferFromString(historyResult[historyIndex].Members["CommandLine"].Value.ToString()); + this.Render(); + historyIndex++; + } - BufferFromString(historyResult[historyIndex].Members["CommandLine"].Value.ToString()); - this.Render(); - historyIndex++; + catch + { + return; + } } else { - BufferFromString(historyResult[historyIndex].Members["CommandLine"].Value.ToString()); - this.Render(); - - if (historyIndex < historyResult.Count -1) + try { - historyIndex++; + BufferFromString(historyResult[historyIndex].Members["CommandLine"].Value.ToString()); + this.Render(); + + if (historyIndex < historyResult.Count -1) + { + historyIndex++; + } + + else + { + historyIndex = 0; + } } - else + catch { - historyIndex = 0; - } - } + return; + } + } } - /// /// Changes the history queue when the down arrow is pressed /// - private void OnDownArrow() + private void OnUpArrow() { if ((previousKeyPress.Key != ConsoleKey.DownArrow && previousKeyPress.Key != ConsoleKey.UpArrow) || previousKeyPress.Key == ConsoleKey.Enter) { @@ -361,29 +376,43 @@ namespace Microsoft.PowerShell.Linux.Host { historyResult = pipeline.Invoke(); } - - historyIndex = historyResult.Count -1; - BufferFromString(historyResult[historyIndex].Members["CommandLine"].Value.ToString()); - this.Render(); - historyIndex--; + try + { + historyIndex = historyResult.Count -1; + BufferFromString(historyResult[historyIndex].Members["CommandLine"].Value.ToString()); + this.Render(); + historyIndex--; + } + + catch + { + return; + } } else { - BufferFromString(historyResult[historyIndex].Members["CommandLine"].Value.ToString()); - this.Render(); + try + { + BufferFromString(historyResult[historyIndex].Members["CommandLine"].Value.ToString()); + this.Render(); - if( historyIndex == 0) - { - historyIndex = historyResult.Count -1; - } + if( historyIndex == 0) + { + historyIndex = historyResult.Count -1; + } - else + else + { + historyIndex--; + } + } + + catch { - historyIndex--; + return; } - } }