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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
import kotlinx.browser.document
import kotlinx.browser.window
import org.w3c.dom.Element
import org.w3c.dom.asList
fun main() {
console.log("Hello from Kotlin")
val webos = WebOS()
document.body?.addEventListener("load", {
document.body?.querySelectorAll(".webosconsole")?.asList()?.forEach {
if (it !is Element) return@forEach
webos.registerConsole(it)
}
})
}
data class CharacterRun(val text: String, val color: String)
abstract class Activity(val console: Console) {
abstract fun render(columns: Int, rows: Int): List<List<CharacterRun>>
}
class Console(val os: WebOS, val renderElement: Element?) {
val isVirtual get() = renderElement == null
val activityStack = ArrayDeque<Activity>()
var columns: Int = 80
var rows: Int = 46
var shouldRerender = true
lateinit var currentUser: User
private var _workingDirectory: AbsolutPath? = null
var workingDirectory: AbsolutPath
get() {
if (_workingDirectory == null) _workingDirectory = currentUser.homeDirectory; return _workingDirectory!!
}
set(value) { _workingDirectory = value }
fun openActivity(activity: Activity) {
activityStack.addLast(activity)
invalidateRender()
}
fun render() {
if (renderElement == null) return
if (!shouldRerender) return
shouldRerender = false
activityStack.last()
}
fun invalidateRender() {
shouldRerender = true
window.requestAnimationFrame { render() }
}
fun resize(newColumns: Int, newRows: Int) {
invalidateRender()
}
// TODO: Handle resizes of the renderElement
}
class WebOS {
private val _consoles = mutableListOf<Console>()
val consoles get() = _consoles.toList()
val IOHandler = IOHandler()
fun registerConsole(element: Element) {
_consoles.add(Console(this, element))
}
}
data class User(
val name: String,
val homeDirectory: AbsolutPath
)
data class RelativePath internal constructor(override val parts: List<String>) : Path {
override fun toAbsolutPath(context: Console): AbsolutPath {
val result = mutableListOf<String>()
for (part in parts) {
when (part) {
"." -> result.addAll(context.workingDirectory.parts)
"~" -> result.addAll(context.currentUser.homeDirectory.parts)
".." -> { result.removeLast() }
else -> result.add(part)
}
}
return AbsolutPath(result)
}
}
data class AbsolutPath internal constructor(override val parts: List<String>) : Path {
override fun toAbsolutPath(context: Console): AbsolutPath = this
}
sealed interface Path {
val parts: List<String>
fun toAbsolutPath(context: Console): AbsolutPath
companion object {
fun of(string: String): Path {
val isAbsolut = string.first() == '/'
val parts = string.split("/")
val aparts = parts.subList(1, parts.size)
if (aparts.contains(".") || aparts.contains("~"))
throw IllegalArgumentException("'.' and '~' are only allowed at the beginning of a relative path")
if (!isAbsolut) return RelativePath(parts)
if (aparts.contains("..")) throw IllegalArgumentException("'..' is only allowed in a relative path")
return AbsolutPath(aparts)
}
}
}
class FileSystem {
fun read(relativePath: Path): ReadResult = TODO()
fun write(path: Path, data: ByteArray): Unit = TODO()
fun stat(path: Path): Unit = TODO()
}
class IOHandler {
val mounts = mutableListOf<Mount>()
fun mount(absolutPath: AbsolutPath, fileSystem: FileSystem) {
mounts += Mount(absolutPath, fileSystem)
}
fun unmount(mountPoint: AbsolutPath) {
mounts.removeAll { it.mountPoint == mountPoint }
}
fun <T> findMountFor(context: Console, path: Path, operation: FileSystem.(relativePath: Path) -> T): T {
val absolutPath = path.toAbsolutPath(context)
val mount = mounts.filter {
var result = true
it.mountPoint.parts.forEachIndexed { index, part -> if (absolutPath.parts[index] != part) {
result = false
} }
result
}.maxByOrNull { it.mountPoint.parts.size} ?: throw IllegalStateException("No mount present")
return mount.fileSystem.operation(
AbsolutPath(absolutPath.parts.subList(mount.mountPoint.parts.size, absolutPath.parts.size))
)
}
fun read(context: Console, path: Path): ReadResult = findMountFor(context, path) { read(it) }
fun write(context: Console, path: Path, data: ByteArray): Unit = findMountFor(context, path) { write(it, data) }
fun stat(context: Console, path: Path): Unit = findMountFor(context, path) { stat(it) }
}
sealed class ReadResult {
class Success(val text: String) : ReadResult()
object NotFound
object NoAccess
}
data class Mount (
val mountPoint: AbsolutPath,
val fileSystem: FileSystem
)
data class Stat(
val exists: Boolean,
var owner: User,
val created: Long,
val edited: Long,
val size: Long
)
|