ADD save command input history and use arrow keys to navigate

pull/1/head
serapath 7 years ago
parent c5a017233a
commit b7dc1e95f4
  1. 34
      src/app/panels/terminal.js

@ -189,6 +189,7 @@ class Terminal {
self._view.input = yo`
<span class=${css.input} contenteditable="true" onkeydown=${change}></span>
`
self._view.input.innerText = '\n'
self._view.cli = yo`
<div class=${css.cli}>
<span class=${css.prompt}>${'>'}</span>
@ -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) { // <ctrl+enter>
self._view.input.appendChild(document.createElement('br'))
self.scroll2bottom()
self._view.input.innerText += '\n'
putCursor2End(self._view.input)
self.scroll2bottom()
} else { // <enter>
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) { // <arrowUp>
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) { // <arrowDown>
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) {

Loading…
Cancel
Save