From b7dc1e95f4017fb183d5352eec5f116c750e7ef3 Mon Sep 17 00:00:00 2001 From: serapath Date: Mon, 11 Sep 2017 22:05:11 +0200 Subject: [PATCH 1/3] ADD save command input history and use arrow keys to navigate --- src/app/panels/terminal.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/app/panels/terminal.js b/src/app/panels/terminal.js index c5215a0362..b3a2f9329a 100644 --- a/src/app/panels/terminal.js +++ b/src/app/panels/terminal.js @@ -189,6 +189,7 @@ class Terminal { self._view.input = yo` ` + self._view.input.innerText = '\n' self._view.cli = yo`
${'>'} @@ -397,19 +398,44 @@ class Terminal { self.event.trigger('resize', [self._api.getPosition(event)]) } + self._cmdHistory = [] + self._cmdTemp = '' + return self._view.el function change (event) { + if (self._view.input.innerText.length === 0) self._view.input.innerText += '\n' if (event.which === 13) { if (event.ctrlKey) { // - self._view.input.appendChild(document.createElement('br')) - self.scroll2bottom() + self._view.input.innerText += '\n' putCursor2End(self._view.input) + self.scroll2bottom() } else { // event.preventDefault() - self.commands.script(self._view.input.innerText) - self._view.input.innerHTML = '' + var script = self._view.input.innerText.trim() + self._view.input.innerText = '\n' + if (script.length) { + self._cmdHistory.push('\n' + script) + self.commands.script(script) + } } + } else if (event.which === 38) { // + var len = self._cmdHistory.length + if (len === 0) return event.preventDefault() + if (self._cmdIndex === undefined) { + self._cmdIndex = len - 1 + self._cmdTemp = self._view.input.innerText + } else if (self._cmdIndex === 0) return + else self._cmdIndex -= 1 + self._view.input.innerText = self._cmdHistory[self._cmdIndex] + self.scroll2bottom() + } else if (event.which === 40) { // + if (self._cmdIndex === undefined) return + else if (self._cmdIndex === self._cmdHistory.length - 1) self._cmdIndex = undefined + else self._cmdIndex += 1 + self._view.input.innerText = self._cmdHistory[self._cmdIndex] || self._cmdTemp + putCursor2End(self._view.input) + self.scroll2bottom() } } function putCursor2End (editable) { From 494187bea057d9689c57091b2a242c407ec610af Mon Sep 17 00:00:00 2001 From: serapath Date: Tue, 12 Sep 2017 17:51:27 +0200 Subject: [PATCH 2/3] FIX line break bug --- src/app/panels/terminal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/panels/terminal.js b/src/app/panels/terminal.js index b3a2f9329a..238b4aedd9 100644 --- a/src/app/panels/terminal.js +++ b/src/app/panels/terminal.js @@ -415,7 +415,7 @@ class Terminal { var script = self._view.input.innerText.trim() self._view.input.innerText = '\n' if (script.length) { - self._cmdHistory.push('\n' + script) + self._cmdHistory.push(script) self.commands.script(script) } } From 81bf4d02107807e51adb2bac7f372729cd8078bf Mon Sep 17 00:00:00 2001 From: yann300 Date: Thu, 14 Sep 2017 11:51:51 +0200 Subject: [PATCH 3/3] fix history --- src/app/panels/terminal.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/app/panels/terminal.js b/src/app/panels/terminal.js index 238b4aedd9..d4767d6bee 100644 --- a/src/app/panels/terminal.js +++ b/src/app/panels/terminal.js @@ -399,6 +399,7 @@ class Terminal { } self._cmdHistory = [] + self._cmdIndex = -1 self._cmdTemp = '' return self._view.el @@ -411,31 +412,34 @@ class Terminal { putCursor2End(self._view.input) self.scroll2bottom() } else { // + self._cmdIndex = -1 + self._cmdTemp = '' event.preventDefault() var script = self._view.input.innerText.trim() self._view.input.innerText = '\n' if (script.length) { - self._cmdHistory.push(script) + self._cmdHistory.unshift(script) self.commands.script(script) } } } else if (event.which === 38) { // var len = self._cmdHistory.length if (len === 0) return event.preventDefault() - if (self._cmdIndex === undefined) { - self._cmdIndex = len - 1 - self._cmdTemp = self._view.input.innerText - } else if (self._cmdIndex === 0) return - else self._cmdIndex -= 1 + if (self._cmdHistory.length - 1 > self._cmdIndex) { + self._cmdIndex++ + } self._view.input.innerText = self._cmdHistory[self._cmdIndex] + putCursor2End(self._view.input) self.scroll2bottom() } else if (event.which === 40) { // - if (self._cmdIndex === undefined) return - else if (self._cmdIndex === self._cmdHistory.length - 1) self._cmdIndex = undefined - else self._cmdIndex += 1 - self._view.input.innerText = self._cmdHistory[self._cmdIndex] || self._cmdTemp + if (self._cmdIndex > -1) { + self._cmdIndex-- + } + self._view.input.innerText = self._cmdIndex >= 0 ? self._cmdHistory[self._cmdIndex] : self._cmdTemp putCursor2End(self._view.input) self.scroll2bottom() + } else { + self._cmdTemp = self._view.input.innerText } } function putCursor2End (editable) {