-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Commands.kt
47 lines (46 loc) 路 1.65 KB
/
Commands.kt
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
val commands = mapOf(
"help" to Console.Help,
"ls" to object : Command {
override val help = "usage: ls (no additional flags supported)"
override fun complete(argv: List<String>): List<String> = listOf()
override fun exec(argv: List<String>, print: (String) -> Unit) {
if (argv.isEmpty()) {
files.forEachIndexed { i, it ->
if (argv.isEmpty() || argv[0] == it.name) {
if (i > 0) {
print(space)
}
print(it.name)
}
}
}
else {
print(help)
}
}
},
"cat" to object : Command {
override val help = "usage: cat [file ...]"
override fun complete(argv: List<String>): List<String> =
files.filter { it.name.indexOf(argv[0]) == 0 || it.name.indexOf(" ") == 0 }.map { it.name }
override fun exec(argv: List<String>, print: (String) -> Unit) {
if (argv.isNotEmpty()) {
files.forEach {
if (it.name == argv[0]) {
print("<p>${it.content}</p>")
return
}
}
print("${argv[0]}: no such file")
}
else {
print(help)
}
}
},
"clear" to object : Command {
override val help = "clear the terminal screen"
override fun complete(argv: List<String>): List<String> = listOf()
override fun exec(argv: List<String>, print: (String) -> Unit) = Console.clear()
}
)