summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornea <romangraef@gmail.com>2022-08-26 14:09:53 +0200
committernea <romangraef@gmail.com>2022-08-26 14:09:53 +0200
commited931bbb10219b573b6227631bf3600319367ec4 (patch)
tree5a79cc3050bb0c11afbb6eebefdc5c1767b72f2b
parent6207fd482a685527fda286e0df5d4fa87aa85149 (diff)
downloadneamoe-ed931bbb10219b573b6227631bf3600319367ec4.tar.gz
neamoe-ed931bbb10219b573b6227631bf3600319367ec4.tar.bz2
neamoe-ed931bbb10219b573b6227631bf3600319367ec4.zip
move shit around
-rw-r--r--.gitignore1
-rw-r--r--build.gradle.kts3
-rw-r--r--example/src/main/kotlin/moe/nea89/website/test/index.kt25
-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
7 files changed, 66 insertions, 22 deletions
diff --git a/.gitignore b/.gitignore
index e469921..c9b8cf9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
.idea/
build/
.gradle/
+kotlin-js-store
diff --git a/build.gradle.kts b/build.gradle.kts
index af77441..a5fd1df 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -1,6 +1,5 @@
plugins {
- kotlin("js") version "1.6.0"
- id("com.github.node-gradle.node") version "3.1.1"
+ kotlin("js") version "1.7.10"
}
allprojects {
diff --git a/example/src/main/kotlin/moe/nea89/website/test/index.kt b/example/src/main/kotlin/moe/nea89/website/test/index.kt
index 9fdb2c1..c558868 100644
--- a/example/src/main/kotlin/moe/nea89/website/test/index.kt
+++ b/example/src/main/kotlin/moe/nea89/website/test/index.kt
@@ -2,11 +2,13 @@ package moe.nea89.website.test
import kotlinext.js.require
import kotlinx.browser.document
+import kotlinx.css.*
import kotlinx.html.dom.append
import kotlinx.html.dom.create
import kotlinx.html.img
import kotlinx.html.js.a
import kotlinx.html.js.div
+import kotlinx.html.js.onLoadFunction
import kotlinx.html.js.p
import moe.nea89.website.*
import styled.injectGlobal
@@ -31,11 +33,26 @@ val defaultFileSystem = fileSystem {
fun main() {
require("@fontsource/comic-mono/index.css")
- injectGlobal(Styles.global)
val root = document.body!!.append.div()
val console = KConsole.createFor(root, fileSystem = defaultFileSystem)
+ console.text.id = "myconsole"
+ injectGlobal {
+ body {
+ backgroundColor = Styles.bgColor.lighten(30)
+ }
+ ".${Styles.consoleClass}" {
+ margin(LinearDimension.auto)
+ width = 50.vw
+ height = 50.vh
+ marginTop = 25.vh
+ boxSizing = BoxSizing.borderBox
+ backgroundClip = BackgroundClip.contentBox
+ overflowY = Overflow.scroll
+ }
+
+ }
console.addLine("Starting up terminal.")
- console.PS1 = ">"
+ console.PS1 = { "${this.fileAccessor?.currentDir?.joinToString("/", "/") ?: ""} >" }
console.rerender()
console.registerCommand(command("cwd", "pwd") {
val fa = requireFileAccessor()
@@ -104,7 +121,9 @@ fun main() {
is KFile.Directory -> console.addLine("cat: Is a directory")
is KFile.Text -> console.addMultilineText(file.text)
is KFile.Image -> console.addLine(document.create.p {
- img(src = file.url)
+ img(src = file.url) {
+ this.onLoadFunction = { console.scrollDown() }
+ }
})
is KFile.Download -> {
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)
+
+
+