From 77e1c35e52713b32f7118ed32d923d9f05c43655 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Sun, 25 Feb 2018 09:45:56 +0100 Subject: [PATCH] FrmProxyCmd: Command history. --- VAR.Toolbox/UI/FrmProxyCmd.cs | 47 ++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/VAR.Toolbox/UI/FrmProxyCmd.cs b/VAR.Toolbox/UI/FrmProxyCmd.cs index 6c4aa17..55ffc4f 100644 --- a/VAR.Toolbox/UI/FrmProxyCmd.cs +++ b/VAR.Toolbox/UI/FrmProxyCmd.cs @@ -14,6 +14,9 @@ namespace VAR.Toolbox.UI private object _executionLock = new object(); + private List _cmdHistory = new List(); + private int _currentHistoryIndex = -1; + private void txtInput_KeyDown(object sender, KeyEventArgs e) { if (Monitor.IsEntered(_executionLock)) @@ -25,12 +28,15 @@ namespace VAR.Toolbox.UI if (e.KeyCode == Keys.Return) { e.Handled = true; - string cmd = txtInput.Text.Replace("\n", "").Replace("\r", ""); - txtInput.Text = string.Empty; - Application.DoEvents(); - txtInput.Text = string.Empty; - OutputLine(cmd); - new Thread(() => ExecuteCmd(cmd)).Start(); + string cmd = txtInput.Text.TrimStart().Replace("\n", "").Replace("\r", ""); + if (string.IsNullOrEmpty(cmd) == false) + { + txtInput.Text = string.Empty; + Application.DoEvents(); + txtInput.Text = string.Empty; + OutputLine(cmd); + new Thread(() => ExecuteCmd(cmd)).Start(); + } return; } if(e.KeyCode == Keys.Enter) @@ -46,11 +52,38 @@ namespace VAR.Toolbox.UI if (e.KeyCode == Keys.Up) { e.Handled = true; + if (_currentHistoryIndex == -1) { _currentHistoryIndex = _cmdHistory.Count; } + _currentHistoryIndex--; + if (_currentHistoryIndex < 0) + { + _currentHistoryIndex = 0; + } + if (_currentHistoryIndex>=0 && _currentHistoryIndex < _cmdHistory.Count) + { + txtInput.Text = _cmdHistory[_currentHistoryIndex]; + txtInput.SelectionStart = txtInput.Text.Length; + txtInput.SelectionLength = 0; + } return; } if (e.KeyCode == Keys.Down) { e.Handled = true; + if (_currentHistoryIndex > -1) + { + _currentHistoryIndex++; + if (_currentHistoryIndex >= _cmdHistory.Count) + { + txtInput.Text = string.Empty; + _currentHistoryIndex = -1; + } + else + { + txtInput.Text = _cmdHistory[_currentHistoryIndex]; + txtInput.SelectionStart = txtInput.Text.Length; + txtInput.SelectionLength = 0; + } + } return; } } @@ -66,6 +99,8 @@ namespace VAR.Toolbox.UI Monitor.Enter(_executionLock); try { + _cmdHistory.Add(cmdString); + _currentHistoryIndex = -1; _proxyCmdExecutor.ExecuteCmd(cmdString, this); } catch (Exception ex)