summaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/moe/nea89/website/KConsole.kt28
-rw-r--r--src/main/kotlin/moe/nea89/website/ScrollIntoViewOptions.kt7
-rw-r--r--src/main/kotlin/moe/nea89/website/Styles.kt18
-rw-r--r--src/main/kotlin/moe/nea89/website/util.kt6
4 files changed, 42 insertions, 17 deletions
diff --git a/src/main/kotlin/moe/nea89/website/KConsole.kt b/src/main/kotlin/moe/nea89/website/KConsole.kt
index 56a6e3c..4a51219 100644
--- a/src/main/kotlin/moe/nea89/website/KConsole.kt
+++ b/src/main/kotlin/moe/nea89/website/KConsole.kt
@@ -4,6 +4,7 @@ import kotlinx.browser.document
import kotlinx.dom.addClass
import kotlinx.html.dom.append
import kotlinx.html.dom.create
+import kotlinx.html.input
import kotlinx.html.js.p
import kotlinx.html.js.pre
import kotlinx.html.js.span
@@ -11,19 +12,27 @@ import org.w3c.dom.HTMLElement
import org.w3c.dom.HTMLParagraphElement
import org.w3c.dom.HTMLPreElement
import org.w3c.dom.events.KeyboardEvent
+import styled.injectGlobal
import kotlin.collections.set
class KConsole(
- private val root: HTMLElement,
- private val text: HTMLPreElement,
- private val prompt: HTMLElement,
+ val root: HTMLElement,
+ val text: HTMLPreElement,
+ val prompt: HTMLElement,
fileSystem: KFileSystem?,
) {
+
val fileAccessor = fileSystem?.let { FileAccessor(it) }
- var PS1 = "$"
+ var PS1: KConsole.() -> String = { "$" }
+
companion object {
+
+ init {
+ injectGlobal(Styles.global)
+ }
+
val shlexRegex =
""""([^"\\]+|\\.)+"|([^ "'\\]+|\\.)+|'([^'\\]+|\\.)+'""".toRegex()
@@ -33,6 +42,9 @@ class KConsole(
prompt.addClass(Styles.promptClass)
element.classList.add(Styles.consoleClass)
val console = KConsole(element, text, prompt, fileSystem)
+ val inp = element.append.input()
+ inp.hidden = true
+ inp.focus()
document.body!!.onkeydown = console::keydown
console.rerender()
return console
@@ -65,6 +77,7 @@ class KConsole(
el.style.color = it.color.color.toString()
el.append(it.text)
})
+
is String -> append(it)
else -> throw RuntimeException("Unknown element")
}
@@ -78,7 +91,7 @@ class KConsole(
fun rerender() {
if (state == KConsole.ConsoleState.SHELLPROMPT) {
- prompt.innerText = "$PS1 $input"
+ prompt.innerText = "${PS1.invoke(this)} $input"
} else {
prompt.innerText = ""
}
@@ -115,9 +128,9 @@ class KConsole(
return
}
ShellExecutionContext.run(this, commandThing, command, arguments)
+ scrollDown()
}
- @OptIn(ExperimentalStdlibApi::class)
fun shlex(command: String): List<String>? {
var i = 0
val parts = mutableListOf<String>()
@@ -143,10 +156,11 @@ class KConsole(
when (event.key) {
"Enter" -> {
val toExecute = input
- addLine("$PS1 $toExecute")
+ addLine("${PS1.invoke(this)} $toExecute")
input = ""
executeCommand(toExecute)
}
+
"Backspace" -> input = input.substring(0, input.length - 1)
else ->
if (event.key.length == 1 || event.key.any { it !in 'a'..'z' && it !in 'A'..'Z' })
diff --git a/src/main/kotlin/moe/nea89/website/ScrollIntoViewOptions.kt b/src/main/kotlin/moe/nea89/website/ScrollIntoViewOptions.kt
new file mode 100644
index 0000000..aab14e2
--- /dev/null
+++ b/src/main/kotlin/moe/nea89/website/ScrollIntoViewOptions.kt
@@ -0,0 +1,7 @@
+package moe.nea89.website
+
+interface ScrollIntoViewOptions {
+ var behavior: String
+ var block: String
+ var inline: String
+} \ No newline at end of file
diff --git a/src/main/kotlin/moe/nea89/website/Styles.kt b/src/main/kotlin/moe/nea89/website/Styles.kt
index a6d7c1f..e1470d7 100644
--- a/src/main/kotlin/moe/nea89/website/Styles.kt
+++ b/src/main/kotlin/moe/nea89/website/Styles.kt
@@ -8,13 +8,13 @@ import styled.StyleSheet
import styled.animation
-object Styles : StyleSheet("Styles") {
+object Styles : StyleSheet("DefaultConsoleStyles") {
val consoleClass = "Console"
val promptClass = "prompt"
val bgColor = CustomColor.BLACK.color
val fgColor = CustomColor.WHITE.color
- val comicMono = "\"Comic Mono\", monospace"
+ val monospacedFont = "monospace"
val global by css {
"*" {
@@ -22,13 +22,6 @@ object Styles : StyleSheet("Styles") {
margin(0.px)
boxSizing = BoxSizing.borderBox
}
- body {
- width = 100.pct
- height = 100.pct
- backgroundColor = bgColor
- color = fgColor
- fontFamily = comicMono
- }
".$promptClass" {
width = LinearDimension.fitContent
@@ -49,8 +42,13 @@ object Styles : StyleSheet("Styles") {
".$consoleClass" {
width = 100.pct
height = 100.pct
+ backgroundColor = bgColor
+ color = fgColor
+ fontFamily = monospacedFont
+ width = 100.pct
+ height = 100.pct
pre {
- fontFamily = comicMono
+ fontFamily = monospacedFont
}
}
}
diff --git a/src/main/kotlin/moe/nea89/website/util.kt b/src/main/kotlin/moe/nea89/website/util.kt
new file mode 100644
index 0000000..47c7843
--- /dev/null
+++ b/src/main/kotlin/moe/nea89/website/util.kt
@@ -0,0 +1,6 @@
+package moe.nea89.website
+
+fun <T> dyn(init: T.() -> Unit): dynamic = js("{}").also(init)
+
+
+