summaryrefslogtreecommitdiff
path: root/example/src/main/kotlin/moe/nea89/website/test/index.kt
blob: c5588687051df229687c54cd4cac0ac1171ec7e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
import kotlin.time.Duration.Companion.milliseconds

val defaultFileSystem = fileSystem {
    "etc" {
        "passwd" text "hunter2"
    }
    "home/nea" {
        "todo" text """
                | - git gud
                | - finish this website
                | - convince the general public that comic sans is a viable font
            """.trimMargin()
        "moisturized" image require("images/moisturized.jpg")
        "download" download require("images/me.jpeg")
    }
    "flag" text "CTF{12345abcdefghijklmonp3.1.4.1.5.9.2.8}"
}

fun main() {

    require("@fontsource/comic-mono/index.css")
    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 = { "${this.fileAccessor?.currentDir?.joinToString("/", "/") ?: ""} >" }
    console.rerender()
    console.registerCommand(command("cwd", "pwd") {
        val fa = requireFileAccessor()
        console.addLine(fa.currentDir.joinToString(separator = "/", prefix = "/"))
    })
    console.registerCommand(command("cd") {
        val fa = requireFileAccessor()
        val path = args.singleOrNull()
        if (path == null) {
            console.addLine("Usage: cd <directory>")
            return@command
        }
        val error = fa.cd(path)
        if (error != null) {
            console.addLine("cd: ${error.name}")
        }
    })
    console.registerCommand(command("ls") {
        val fa = requireFileAccessor()
        val path = when (args.size) {
            0 -> "."
            1 -> args[0]
            else -> {
                console.addLine("Usage: ls [directory or file]")
                return@command
            }
        }
        val file = fa.resolve(path)
        if (file == null) {
            console.addLine("ls: Could not find file or directory")
            return@command
        }
        when (file) {
            is KFile.Directory -> {
                val longestName = file.files.keys.maxOf { it.length }
                file.files.forEach { (name, file) ->
                    wait(200.milliseconds)
                    console.addLine(
                        name + " ".repeat(longestName + 1 - name.length) + file.fileType
                    )
                    console.rerender()
                }
            }

            else -> console.addLine("ls: is a ${file.fileType}")
        }
    })
    console.registerCommand(command("color") {
        console.addLine("This is a ", red("red"), " word: ", green("1.0"), " ", blue("BLUUEEE"))
    })
    console.registerCommand(command("cat") {
        val fa = requireFileAccessor()
        val path = when (args.size) {
            1 -> args[0]
            else -> {
                console.addLine("Usage: cat [directory or file]")
                return@command
            }
        }
        val file = fa.resolve(path)
        if (file == null) {
            console.addLine("cat: Could not find file or directory")
            return@command
        }
        when (file) {
            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) {
                    this.onLoadFunction = { console.scrollDown() }
                }
            })

            is KFile.Download -> {
                val link = document.create.a(file.url)
                link.download = file.name.last()
                document.body!!.append(link)
                link.click()
                link.remove()
                console.addLine("Download started")
            }
        }
    })
    console.registerCommand(command("dick", "cock") {
        console.addMultilineText("Hehe")
    })
    console.registerCommand(command("boob", "booob") {
        console.addMultilineText(boobs)
    })
}