summaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea89
diff options
context:
space:
mode:
authornea <romangraef@loves.dicksinhisan.us>2021-12-07 05:28:25 +0100
committernea <romangraef@loves.dicksinhisan.us>2021-12-07 19:08:02 +0100
commit5843b02bad28bc82567c195ca0bb31515e3dbb36 (patch)
tree673f842605f37be928a36811ddd67ed8948afdab /src/main/kotlin/moe/nea89
parent6b44e60fc4efc23a30f16492bf9dc17aa30b66c5 (diff)
downloadneamoe-5843b02bad28bc82567c195ca0bb31515e3dbb36.tar.gz
neamoe-5843b02bad28bc82567c195ca0bb31515e3dbb36.tar.bz2
neamoe-5843b02bad28bc82567c195ca0bb31515e3dbb36.zip
uwu
Diffstat (limited to 'src/main/kotlin/moe/nea89')
-rw-r--r--src/main/kotlin/moe/nea89/website/App.kt26
-rw-r--r--src/main/kotlin/moe/nea89/website/AsciiArt.kt5
-rw-r--r--src/main/kotlin/moe/nea89/website/Command.kt7
-rw-r--r--src/main/kotlin/moe/nea89/website/KConsole.kt112
-rw-r--r--src/main/kotlin/moe/nea89/website/Styles.kt31
-rw-r--r--src/main/kotlin/moe/nea89/website/index.kt24
6 files changed, 176 insertions, 29 deletions
diff --git a/src/main/kotlin/moe/nea89/website/App.kt b/src/main/kotlin/moe/nea89/website/App.kt
deleted file mode 100644
index aa96825..0000000
--- a/src/main/kotlin/moe/nea89/website/App.kt
+++ /dev/null
@@ -1,26 +0,0 @@
-package moe.nea89.website
-
-import com.bnorm.react.RFunction
-import react.RBuilder
-import react.dom.li
-import react.dom.nav
-import react.dom.ul
-
-
-@RFunction
-fun RBuilder.App() {
- Navigation()
-}
-
-@RFunction
-fun RBuilder.Navigation() {
- nav {
- ul {
- li { +"Hehe" }
- li { +"Hihi" }
- li { +"Hoho" }
- li { +"Haha" }
- li { +"Huhu" }
- }
- }
-}
diff --git a/src/main/kotlin/moe/nea89/website/AsciiArt.kt b/src/main/kotlin/moe/nea89/website/AsciiArt.kt
new file mode 100644
index 0000000..6acc8b5
--- /dev/null
+++ b/src/main/kotlin/moe/nea89/website/AsciiArt.kt
@@ -0,0 +1,5 @@
+package moe.nea89.website
+
+import kotlinext.js.require
+
+val boobs = require("./asciiart/boob.txt") as String \ No newline at end of file
diff --git a/src/main/kotlin/moe/nea89/website/Command.kt b/src/main/kotlin/moe/nea89/website/Command.kt
new file mode 100644
index 0000000..f896987
--- /dev/null
+++ b/src/main/kotlin/moe/nea89/website/Command.kt
@@ -0,0 +1,7 @@
+package moe.nea89.website
+
+interface Command {
+ val name: String
+ val aliases: Set<String>
+ fun run(console: KConsole, name: String, args: List<String>)
+} \ No newline at end of file
diff --git a/src/main/kotlin/moe/nea89/website/KConsole.kt b/src/main/kotlin/moe/nea89/website/KConsole.kt
new file mode 100644
index 0000000..d6e4349
--- /dev/null
+++ b/src/main/kotlin/moe/nea89/website/KConsole.kt
@@ -0,0 +1,112 @@
+package moe.nea89.website
+
+import kotlinx.browser.document
+import kotlinx.html.dom.append
+import kotlinx.html.js.pre
+import org.w3c.dom.HTMLElement
+import org.w3c.dom.HTMLPreElement
+import org.w3c.dom.events.KeyboardEvent
+
+class KConsole(private val root: HTMLElement, private val text: HTMLPreElement) {
+
+ companion object {
+ val shlexRegex =
+ """"([^"\\]+|\\.)+"|([^ "'\\]+|\\.)+|'([^'\\]+|\\.)+'""".toRegex()
+
+ fun createFor(element: HTMLElement): KConsole {
+ val text = element.append.pre()
+ element.classList.add(Styles.consoleClass)
+ val console = KConsole(element, text)
+ document.body!!.onkeydown = console::keydown
+ console.addLine("Starting up terminal.")
+ console.rerender()
+ return console
+ }
+ }
+
+ val lines = mutableListOf<String>()
+
+ var input: String = ""
+
+ fun addLines(newLines: List<String>) {
+ lines.addAll(newLines)
+ }
+
+ fun addMultilineText(text: String) {
+ addLines(text.split("\n"))
+ }
+
+ fun addLine(line: String) {
+ lines.add(line)
+ scrollDown()
+ }
+
+ fun rerender() {
+ val view = lines.joinToString(separator = "\n") + "\n${'$'} $input"
+ text.innerText = view
+ }
+
+ fun registerCommand(command: Command) {
+ command.aliases.forEach {
+ commands[it] = command
+ }
+ commands[command.name] = command
+ }
+
+ val commands = mutableMapOf<String, Command>()
+
+ fun scrollDown() {} // TODO scroooooll
+
+ fun executeCommand(command: String) {
+ val parts = shlex(command)
+ if (parts.isNullOrEmpty()) {
+ addLine("Syntax Error")
+ return
+ }
+ val command = parts[0]
+ println("Running command: $command")
+ val arguments = parts.drop(1)
+ val commandThing = commands[command]
+ if (commandThing == null) {
+ addLine("Unknown command")
+ return
+ }
+ commandThing.run(this, command, arguments)
+ }
+
+ @OptIn(ExperimentalStdlibApi::class)
+ fun shlex(command: String): List<String>? {
+ var i = 0
+ val parts = mutableListOf<String>()
+ while (i < command.length) {
+ val match = shlexRegex.matchAt(command, i)
+ if (match == null) {
+ println("Could not shlex: $command")
+ return null
+ }
+ parts.add(match.groupValues.drop(1).firstOrNull { it != "" } ?: "")
+ i += match.value.length
+ while (command[i] == ' ' && i < command.length)
+ i++
+ }
+ return parts
+ }
+
+ fun keydown(event: KeyboardEvent) {
+ if (event.altKey || event.ctrlKey || event.metaKey) return
+ if (event.isComposing || event.keyCode == 229) return
+ when (event.key) {
+ "Enter" -> {
+ val toExecute = input
+ addLine("${'$'} $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' })
+ input += event.key
+ }
+ rerender()
+ }
+}
diff --git a/src/main/kotlin/moe/nea89/website/Styles.kt b/src/main/kotlin/moe/nea89/website/Styles.kt
new file mode 100644
index 0000000..6d10e7e
--- /dev/null
+++ b/src/main/kotlin/moe/nea89/website/Styles.kt
@@ -0,0 +1,31 @@
+package moe.nea89.website
+
+import kotlinx.css.*
+import styled.StyleSheet
+
+
+object Styles : StyleSheet("Styles") {
+ val consoleClass = "Console"
+
+ val bgColor = Color("#123456")
+ val fgColor = Color("#efefef")
+ val comicMono = "\"Comic Mono\", monospace"
+
+ val global by css {
+ root {
+ margin(0.px)
+ boxSizing = BoxSizing.borderBox
+ }
+ body {
+ width = 100.pct
+ height = 100.pct
+ backgroundColor = bgColor
+ color = fgColor
+ fontFamily = comicMono
+ }
+ ".$consoleClass" {
+ width = 100.pct
+ height = 100.pct
+ }
+ }
+} \ No newline at end of file
diff --git a/src/main/kotlin/moe/nea89/website/index.kt b/src/main/kotlin/moe/nea89/website/index.kt
index f10728a..8496b3a 100644
--- a/src/main/kotlin/moe/nea89/website/index.kt
+++ b/src/main/kotlin/moe/nea89/website/index.kt
@@ -1,10 +1,28 @@
package moe.nea89.website
-import kotlinx.browser.document
-import react.dom.render
import kotlinext.js.require
+import kotlinx.browser.document
+import kotlinx.html.dom.append
+import kotlinx.html.js.div
+import styled.injectGlobal
fun main() {
require("@fontsource/comic-mono/index.css")
- render(document.getElementById("root") ?: throw RuntimeException("Could not find root element")) { App() }
+ injectGlobal(Styles.global)
+ val root = document.body!!.append.div()
+ val console = KConsole.createFor(root)
+ console.registerCommand(object : Command {
+ override val name: String = "dick"
+ override val aliases: Set<String> = setOf("cock")
+ override fun run(console: KConsole, name: String, args: List<String>) {
+ console.addMultilineText("Hehe")
+ }
+ })
+ console.registerCommand(object : Command {
+ override val name: String = "booob"
+ override val aliases: Set<String> = setOf("boob")
+ override fun run(console: KConsole, name: String, args: List<String>) {
+ console.addMultilineText(boobs)
+ }
+ })
} \ No newline at end of file