diff options
author | nea <romangraef@gmail.com> | 2022-10-15 14:11:56 +0200 |
---|---|---|
committer | nea <romangraef@gmail.com> | 2022-10-15 14:11:56 +0200 |
commit | e4dddad9950cecf14c4adcc3a2de997fc0b03f33 (patch) | |
tree | 621020f4fc4a5c11f3400fd7c48f00367527e67e /src/jsMain/kotlin/moe | |
parent | 17f03f850e49b36e59f7381680e31fdc5fc45ed1 (diff) | |
download | neamoe-master.tar.gz neamoe-master.tar.bz2 neamoe-master.zip |
Diffstat (limited to 'src/jsMain/kotlin/moe')
-rw-r--r-- | src/jsMain/kotlin/moe/nea89/website/KConsole.kt | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/src/jsMain/kotlin/moe/nea89/website/KConsole.kt b/src/jsMain/kotlin/moe/nea89/website/KConsole.kt index 9b073a2..28e9e16 100644 --- a/src/jsMain/kotlin/moe/nea89/website/KConsole.kt +++ b/src/jsMain/kotlin/moe/nea89/website/KConsole.kt @@ -1,7 +1,7 @@ package moe.nea89.website -import kotlinx.browser.window import kotlinx.browser.document +import kotlinx.browser.window import kotlinx.dom.addClass import kotlinx.html.InputType import kotlinx.html.dom.append @@ -31,8 +31,8 @@ class KConsole( private lateinit var uninjectKeyHandler: () -> Unit val fileAccessor = fileSystem?.let { FileAccessor(it) } - var currenthistory = 0 - var commandHistory : Array<String> = emptyArray() + var currentHistoryIndex = -1 + var commandHistory: Array<String> = emptyArray() var PS1: KConsole.() -> String = { "$" } private lateinit var mobileInput: HTMLInputElement @@ -144,9 +144,6 @@ class KConsole( return } val command = parts[0] - println("whole command:") - println(commandLine) - commandHistory += commandLine val arguments = parts.drop(1) val commandThing = commands[command] @@ -177,8 +174,9 @@ class KConsole( } fun handleSubmit() { - currenthistory = 0 val toExecute = input + currentHistoryIndex = -1 + commandHistory += toExecute addLine("${PS1.invoke(this)} $toExecute") input = "" executeCommand(toExecute) @@ -224,8 +222,8 @@ class KConsole( } - fun handleControlDown(event: KeyboardEvent){ - if (event.key == "v"){ + fun handleControlDown(event: KeyboardEvent) { + if (event.key == "v") { event.preventDefault() window.navigator.clipboard.readText().then { input += it @@ -234,20 +232,20 @@ class KConsole( } } } - fun handleArrowKeys(event: KeyboardEvent){ + + fun handleArrowKeys(event: KeyboardEvent) { + val last = currentHistoryIndex if (event.keyCode == 40) { - if (commandHistory.isEmpty() || currenthistory > commandHistory.size || currenthistory == 0){ - return - } - input = commandHistory[commandHistory.size-currenthistory] - currenthistory -= 1 + currentHistoryIndex = (currentHistoryIndex - 1).coerceAtLeast(-1) } if (event.keyCode == 38) { - if (commandHistory.isEmpty() || currenthistory == commandHistory.size){ - return - } - input = commandHistory[commandHistory.size-1-currenthistory] - currenthistory += 1 + currentHistoryIndex = (currentHistoryIndex + 1).coerceAtMost(commandHistory.size - 1) + } + if (last == currentHistoryIndex) return + if (currentHistoryIndex in commandHistory.indices) { + input = commandHistory[commandHistory.size - currentHistoryIndex - 1] + } else { + input = "" } } } |