4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-06-11 15:09:24 +02:00

terminal output will now be colored if the console supports it

This commit is contained in:
LordMZTE 2020-08-05 19:26:34 +02:00
parent db43778715
commit f28375a245
6 changed files with 78 additions and 12 deletions

View file

@ -35,6 +35,8 @@ dependencies {
compile 'org.reflections:reflections:0.9.12'
compile 'commons-io:commons-io:2.7'
compile 'net.sourceforge.argparse4j:argparse4j:0.8.1'
compile 'com.diogonunes:JColor:5.0.0'
compile 'com.github.ajalt:mordant:1.2.1'
}
compileKotlin {

View file

@ -2,12 +2,14 @@
package ley.anvil.modpacktools
import com.github.ajalt.mordant.TermColors
import com.google.gson.GsonBuilder
import ley.anvil.modpacktools.command.CommandLoader
import ley.anvil.modpacktools.command.ICommand
import ley.anvil.modpacktools.util.ModpackJsonHandler
import ley.anvil.modpacktools.util.config.Config
import ley.anvil.modpacktools.util.config.MissingConfigValueException
import ley.anvil.modpacktools.util.fPrintln
import net.sourceforge.argparse4j.inf.ArgumentParserException
import okhttp3.Dispatcher
import okhttp3.OkHttpClient
@ -21,6 +23,8 @@ val CONFIG by lazy {Config("modpacktoolsconfig.toml")}
val LOADER by lazy {CommandLoader("ley.anvil.modpacktools.commands")}
val MPJH by lazy {ModpackJsonHandler(File(CONFIG.config.pathOrException<String>("Locations/src"), "modpack.json"))}
val GSON by lazy {GsonBuilder().setPrettyPrinting().create()}
//TODO thinks term has no color support on win with edited registry. probably no big deal
val TERMC by lazy {TermColors()}
//for checking if the client has been initialized when closing it
private val httpClient0 = lazy {
@ -55,27 +59,36 @@ fun main(args: Array<out String>) {
}
fun runCommand(args: Array<out String>) {
val errorColor = arrayOf(TERMC.red, TERMC.bold)
val successColor = TERMC.green
if(args.isEmpty()) {
println(helpMessage)
} else {
try {
val ret = LOADER.runCommand(args[0].toLowerCase(), args)
if(ret.hasRet())
println(ret.ret)
fPrintln(
ret.ret,
*if(ret.success)
arrayOf(successColor)
else
errorColor
)
} catch(e: NoSuchElementException) {
println("Command ${args[0]} not found")
fPrintln("Command ${args[0]} not found", *errorColor)
println(helpMessage)
} catch(e: CommandLoader.ConfigMissingException) {
println("Config is needed for this command yet it is not present. Run 'init' to generate")
fPrintln("Config is needed for this command yet it is not present. Run 'init' to generate", *errorColor)
} catch(e: CommandLoader.ModpackJsonMissingException) {
println("Modpackjson is needed for this command yet it is not present.")
fPrintln("Modpackjson is needed for this command yet it is not present.", *errorColor)
} catch(e: MissingConfigValueException) {
println("The Config value ${e.missingValue} was expected but was not found")
fPrintln("The Config value ${e.missingValue} was expected but was not found", *errorColor)
e.message?.let {println(it)}
println('\n')
e.printStackTrace()
} catch(e: ArgumentParserException) {
println("Invalid args: ${e.message}")
fPrintln("Invalid args: ${e.message}", *errorColor)
}
}
}

View file

@ -2,12 +2,14 @@ package ley.anvil.modpacktools.commands
import ley.anvil.modpacktools.CONFIG
import ley.anvil.modpacktools.MPJH
import ley.anvil.modpacktools.TERMC
import ley.anvil.modpacktools.command.CommandReturn
import ley.anvil.modpacktools.command.CommandReturn.Companion.fail
import ley.anvil.modpacktools.command.CommandReturn.Companion.success
import ley.anvil.modpacktools.command.ICommand
import ley.anvil.modpacktools.command.LoadCommand
import ley.anvil.modpacktools.util.downloadFiles
import ley.anvil.modpacktools.util.fPrintln
import ley.anvil.modpacktools.util.manifest.convertAStoManifest
import ley.anvil.modpacktools.util.mergeTo
import ley.anvil.modpacktools.util.toZip
@ -68,16 +70,16 @@ object BuildTwitch : ICommand {
}
downloadFiles(toDownload.mapValues {it.value.first}, {
println("downloaded file ${it.file}")
println(installFile(toDownload[it.url]!!.second, it.file))
fPrintln("downloaded file ${it.file}", TERMC.brightBlue)
fPrintln(installFile(toDownload[it.url]!!.second, it.file), TERMC.green)
}, false)
println("Creating zip")
fPrintln("Creating zip", TERMC.brightGreen)
val zip = ZipOutputStream(FileOutputStream("${dir.path}/$archiveName.zip"))
tmp.toZip(zip)
zip.flush()
zip.close()
return success("Successfully build twitch zip")
return success("Successfully built twitch zip")
}
private fun installFile(installer: String, file: File): String {

View file

@ -6,10 +6,12 @@ import ley.anvil.addonscript.wrapper.ASWrapper
import ley.anvil.addonscript.wrapper.ArtifactDestination
import ley.anvil.addonscript.wrapper.MetaData
import ley.anvil.modpacktools.MPJH
import ley.anvil.modpacktools.TERMC
import ley.anvil.modpacktools.command.CommandReturn
import ley.anvil.modpacktools.command.CommandReturn.Companion.success
import ley.anvil.modpacktools.command.ICommand
import ley.anvil.modpacktools.command.LoadCommand
import ley.anvil.modpacktools.util.fPrintln
import net.sourceforge.argparse4j.ArgumentParsers
import net.sourceforge.argparse4j.impl.Arguments.storeTrue
import net.sourceforge.argparse4j.impl.type.CaseInsensitiveEnumNameArgumentType
@ -88,7 +90,7 @@ object CreateModlist : ICommand {
}
private fun doHtml(outFile: File, all: Boolean, sorting: Comparator<MetaData>): CommandReturn {
println("Making HTML file $outFile")
fPrintln("Making HTML file $outFile", TERMC.green)
val writer = FileWriter(outFile)
val html = html(
head(
@ -106,6 +108,7 @@ object CreateModlist : ICommand {
td(b("Description"))
),
each(getMods(all, sorting)) {
fPrintln("Writing relation ${it.name}", TERMC.blue)
tr(
td(if(it.icon != null) a(
img().withSrc(it.icon)

View file

@ -1,10 +1,12 @@
package ley.anvil.modpacktools.commands
import ley.anvil.modpacktools.TERMC
import ley.anvil.modpacktools.command.CommandReturn
import ley.anvil.modpacktools.command.CommandReturn.Companion.success
import ley.anvil.modpacktools.command.ICommand
import ley.anvil.modpacktools.command.LoadCommand
import ley.anvil.modpacktools.runCommand
import ley.anvil.modpacktools.util.fPrint
import net.sourceforge.argparse4j.ArgumentParsers
import net.sourceforge.argparse4j.inf.ArgumentParser
import net.sourceforge.argparse4j.inf.Namespace
@ -24,7 +26,7 @@ object Shell : ICommand {
println("enter \'exit\' to exit the shell\n")
while(true) {
print(">>>")
fPrint(">>>", TERMC.bold, TERMC.cyan)
val arg = readLine()!!.split(' ')
if(arg.getOrNull(0) == "exit")
break

View file

@ -0,0 +1,44 @@
@file:JvmName("CLIUtil")
package ley.anvil.modpacktools.util
import java.io.PrintStream
/**
* applies all given formatters to the string representation of the object and then prints it to stdout
*
* @param x the object to print
* @param formatters the formatters to apply to x, they will be ran in the order they are supplied in
*/
fun fPrint(x: Any?, vararg formatters: (String) -> String) = System.out.fPrint(x, *formatters)
/**
* applies all given formatters to the string representation of the object and then prints it
*
* @receiver the printStream to print to
* @param x the object to print
* @param formatters the formatters to apply to x, they will be ran in the order they are supplied in
*/
fun PrintStream.fPrint(x: Any?, vararg formatters: (String) -> String) {
var str = x.toString()
formatters.forEach {str = it(str)}
this.print(str)
}
/**
* applies all given formatters to the string representation of the object and then prints it with a newline at the end
*
* @receiver the printStream to print to
* @param x the object to print
* @param formatters the formatters to apply to x, they will be ran in the order they are supplied in
*/
fun PrintStream.fPrintln(x: Any?, vararg formatters: (String) -> String) = this.fPrint("${x.toString()}\n", *formatters)
/**
* applies all given formatters to the string representation of the object and then prints it to stdout
* with a newline at the end
*
* @param x the object to print
* @param formatters the formatters to apply to x, they will be ran in the order they are supplied in
*/
fun fPrintln(x: Any?, vararg formatters: (String) -> String) = fPrint("${x.toString()}\n", *formatters)