-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathkotlin-shell.main.kts
executable file
·47 lines (40 loc) · 1.48 KB
/
kotlin-shell.main.kts
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
#!/usr/bin/env kotlin
@file:DependsOn("eu.jrie.jetbrains:kotlin-shell-core:0.2.1")
@file:CompilerOptions("-Xopt-in=kotlin.RequiresOptIn")
@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
@file:Suppress("OPT_IN_ARGUMENT_IS_NOT_MARKER")
import eu.jrie.jetbrains.kotlinshell.shell.shell
shell {
if (args.isEmpty()) {
"echo this is 'ls -l' command for current directory: ${env("PWD")}"()
"ls -l"()
} else {
var lines = 0
var words = 0
var chars = 0
var wasSpace = true
pipeline {
"cat ${args[0]}".process() pipe
streamLambda { strm, _, _ ->
while (true) {
val byte = strm.read()
if (byte < 0) break
val ch = byte.toChar()
chars++
if (ch == '\n') lines++
val isSpace = ch == '\n' || ch == '\t' || ch == ' '
if (!wasSpace && isSpace) {
wasSpace = true
} else if (wasSpace && !isSpace) {
words++
wasSpace = false
}
}
}
}
println("My wc:")
println(" $lines $words $chars ${args[0]}")
println("System wc:")
"wc ${args[0]}"()
}
}