From d11db9738f88ec55d84ff2838938f00af2049149 Mon Sep 17 00:00:00 2001 From: Indra Date: Thu, 4 Oct 2018 15:42:45 +0200 Subject: [PATCH 1/2] added command history --- main_windows.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/main_windows.go b/main_windows.go index d682fa3..72c8276 100644 --- a/main_windows.go +++ b/main_windows.go @@ -27,6 +27,9 @@ var ( dlg *mainDialog dlgOriginalTitle string + + history []string + historyIndex = 0 ) func init() { @@ -124,10 +127,35 @@ func runGraphicalUi() (err error) { // Handle input dlg.ui.rconInput.KeyPress().Attach(func(key walk.Key) { + // handle history (arrow up/down) + if key == walk.KeyUp || key == walk.KeyDown { + if len(history) == 0 { + return + } + + if key == walk.KeyUp { + if historyIndex == 0 { + return + } + + historyIndex -= 1 + dlg.ui.rconInput.SetText(history[historyIndex]) + }else{ + if (historyIndex + 1) >= len(history) { + return + } + + historyIndex += 1 + dlg.ui.rconInput.SetText(history[historyIndex]) + } + + return + } + if key != walk.KeyReturn { return } - + if address == nil { uiLogError("No server configured.") return @@ -138,6 +166,10 @@ func runGraphicalUi() (err error) { uiLog(address.String() + "> " + cmd) sendRcon(cmd) + + // add to history + history = append(history, cmd) + historyIndex = len(history) }) // When window is initialized we can let a secondary routine print all From cb9facd9cc71575768f7c5d2a7185434387ac454 Mon Sep 17 00:00:00 2001 From: Indra Date: Thu, 4 Oct 2018 23:09:01 +0200 Subject: [PATCH 2/2] added history limit and fixed formatting --- main_windows.go | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/main_windows.go b/main_windows.go index 72c8276..30bab8c 100644 --- a/main_windows.go +++ b/main_windows.go @@ -27,8 +27,8 @@ var ( dlg *mainDialog dlgOriginalTitle string - - history []string + + history []string historyIndex = 0 ) @@ -74,6 +74,16 @@ func uiUpdateAddress() { } } +func addToHistory(command string) { + // limit history to 20 items + if len(history) > 20 { + history = append(history[:0], history[0+1:]...) + } + + history = append(history, command) + historyIndex = len(history) +} + func runGraphicalUi() (err error) { dlg = new(mainDialog) if err := dlg.init(); err != nil { @@ -132,30 +142,30 @@ func runGraphicalUi() (err error) { if len(history) == 0 { return } - + if key == walk.KeyUp { - if historyIndex == 0 { + if historyIndex == 0 { return } - + historyIndex -= 1 dlg.ui.rconInput.SetText(history[historyIndex]) - }else{ + } else { if (historyIndex + 1) >= len(history) { return } - + historyIndex += 1 dlg.ui.rconInput.SetText(history[historyIndex]) } - + return } - + if key != walk.KeyReturn { return } - + if address == nil { uiLogError("No server configured.") return @@ -166,10 +176,9 @@ func runGraphicalUi() (err error) { uiLog(address.String() + "> " + cmd) sendRcon(cmd) - + // add to history - history = append(history, cmd) - historyIndex = len(history) + addToHistory(cmd) }) // When window is initialized we can let a secondary routine print all