-
Notifications
You must be signed in to change notification settings - Fork 9
/
convert-labels.kts
executable file
Β·103 lines (87 loc) Β· 3.24 KB
/
convert-labels.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
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
#!/usr/bin/env kscript
@file:DependsOn("info.picocli:picocli:4.3.2")
import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters
import java.io.File
import java.nio.file.*
import java.util.concurrent.Callable
import java.time.*
import java.time.format.*
import java.util.concurrent.*
import kotlin.time.toDuration
@OptIn(kotlin.time.ExperimentalTime::class)
@Command(name = "convert-labels", mixinStandardHelpOptions = true, version = ["1.0"])
class GenerateSnippets : Callable<Int> {
@Option(names = ["-i", "--input"], paramLabel = "INPUT", description = ["The label file to read from"])
private var input: String = ""
override fun call(): Int {
info("π·π·π· convert-labels π·π·π·", "")
info("Welcome to convert-labels", "π")
info("Converting labels now...", "")
if (input.isNullOrBlank()) {
error("No input file provided!")
}
File(input).readLines().forEach {
val tokens = it.split('\t').drop(1)
val time = tokens[0].toDouble().toDuration(TimeUnit.SECONDS).toComponents { minutes: Int, seconds: Int, nanoseconds: Int ->
"%02d.%02d".format(minutes, seconds)
}
System.err.println("- **${time}** ${tokens.last()}")
}
succ("convert-labels finished successfully!")
return 0
}
/*
* DEBUG Prints function
******************************************************************/
fun error(message: String, throwable: Throwable? = null, statusCode: Int = 1): Nothing {
System.err.println("β\t${Colors.ANSI_RED}$message${Colors.ANSI_RESET}")
throwable?.let {
System.err.print(Colors.ANSI_RED)
it.printStackTrace()
System.err.print(Colors.ANSI_RESET)
}
System.exit(statusCode)
throw Error()
}
fun warn(message: String) {
System.out.println("β οΈ\t${Colors.ANSI_YELLOW}$message${Colors.ANSI_RESET}")
}
fun succ(message: String) {
System.out.println("β
\t${Colors.ANSI_GREEN}$message${Colors.ANSI_RESET}")
}
fun info(message: String, emoji: String = "βΉοΈ") {
System.out.println("$emoji\t$message")
}
fun String.runCommand(
workingDir: File = File("."),
timeoutAmount: Long = 60
): String? = try {
ProcessBuilder(split("\\s".toRegex()))
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start().apply { waitFor(timeoutAmount, TimeUnit.SECONDS) }
.inputStream.bufferedReader().readText()
} catch (e: java.io.IOException) {
e.printStackTrace()
null
}
}
CommandLine(GenerateSnippets()).execute(*args)
/*
* ASCII Color
******************************************************************/
object Colors {
val ANSI_RESET = "\u001B[0m"
val ANSI_BLACK = "\u001B[30m"
val ANSI_RED = "\u001B[31m"
val ANSI_GREEN = "\u001B[32m"
val ANSI_YELLOW = "\u001B[33m"
val ANSI_BLUE = "\u001B[34m"
val ANSI_PURPLE = "\u001B[35m"
val ANSI_CYAN = "\u001B[36m"
val ANSI_WHITE = "\u001B[37m"
}