-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor, Change build info, Command Completion, pwd, whoami, c…
…hdir
- Loading branch information
Showing
16 changed files
with
432 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# LMFSBox | ||
> Better replacement of HOJVBOX | ||
> A simple toolbox | ||
### How to build? | ||
```bash | ||
./gradlew shadowJar | ||
./gradlew build | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,119 @@ | ||
package io.github.lmfs.box | ||
|
||
import com.github.ajalt.clikt.testing.test | ||
import io.github.lmfs.box.commands.Commands | ||
import com.github.ajalt.mordant.rendering.AnsiLevel | ||
import io.github.lmfs.box.command.Commands | ||
import io.github.lmfs.box.util.makeJLineTreeCompleter | ||
import io.github.lmfs.box.util.space | ||
import org.fusesource.jansi.Ansi | ||
import org.fusesource.jansi.Ansi.ansi | ||
import org.jline.reader.EndOfFileException | ||
import org.jline.reader.LineReaderBuilder | ||
import org.jline.reader.UserInterruptException | ||
import org.jline.terminal.TerminalBuilder | ||
import java.util.* | ||
|
||
|
||
object LMFSBox { | ||
|
||
val license = this@LMFSBox::class.java.classLoader.getResourceAsStream("META-INF/lmfsbox.license.txt")?.readAllBytes()?.toString(Charsets.UTF_8) ?: "" | ||
val copyright = this@LMFSBox::class.java.classLoader.getResourceAsStream("META-INF/lmfsbox.copyright.txt")?.readAllBytes()?.toString(Charsets.UTF_8) ?: "" | ||
@JvmStatic | ||
val LICENSE = this.javaClass.classLoader | ||
.getResourceAsStream("META-INF/lmfsbox.license.txt") | ||
?.readAllBytes() | ||
?.toString(Charsets.UTF_8) | ||
?: "" | ||
|
||
@JvmStatic | ||
val COPYRIGHT = this.javaClass.classLoader | ||
.getResourceAsStream("META-INF/lmfsbox.copyright.txt") | ||
?.readAllBytes() | ||
?.toString(Charsets.UTF_8) | ||
?: "" | ||
|
||
val properties = Properties().apply { | ||
load(this@LMFSBox::class.java.classLoader.getResourceAsStream("META-INF/lmfsbox.properties")) | ||
@JvmStatic | ||
val PROPERTIES = Properties().also { | ||
it.load( | ||
this.javaClass.classLoader.getResourceAsStream("META-INF/lmfsbox.properties") | ||
) | ||
} | ||
|
||
val version = properties["lmfsbox.version"] | ||
val build = properties["lmfsbox.build"] | ||
val buildDate = properties["lmfsbox.build.date"] | ||
@JvmStatic | ||
val VERSION = PROPERTIES["lmfsbox.version"] | ||
|
||
@JvmStatic | ||
val BUILD = PROPERTIES["lmfsbox.build"] | ||
|
||
@JvmStatic | ||
val BUILD_DATE = PROPERTIES["lmfsbox.build.date"] | ||
|
||
@JvmStatic | ||
val GIT_HASH = PROPERTIES["lmfsbox.git.hash"] | ||
|
||
@JvmStatic | ||
val GIT_BRANCH = PROPERTIES["lmfsbox.git.branch"] | ||
|
||
fun main(args: Array<String>) { | ||
if (args.isNotEmpty()) return runCommand(args.toList()) | ||
if (args.isNotEmpty()) | ||
return runCommand(args.toList()) | ||
|
||
println( | ||
ansi() | ||
.fgBrightBlue().bold().a("LMFSBox").reset().space(1) | ||
.fgBrightMagenta().a("$version").reset().space(1) | ||
.fgYellow().a("($buildDate)").reset().space(1) | ||
.fgBrightYellow().a("[$build]").reset() | ||
.fgBrightMagenta().a("$VERSION").reset().space(1) | ||
.fgYellow().a("(commits/$GIT_BRANCH:$GIT_HASH, $BUILD_DATE)").reset().space(1) | ||
.fgBrightYellow().a("[$BUILD]").reset() | ||
) | ||
|
||
println( | ||
ansi().a(Ansi.Attribute.ITALIC) | ||
.a("Type \"help\", \"copyright\" or \"license\" for more information.") | ||
) | ||
println(ansi().a(Ansi.Attribute.ITALIC).a("Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.")) | ||
val builder = LineReaderBuilder.builder() | ||
.terminal( | ||
TerminalBuilder.builder() | ||
.system(true) | ||
.jansi(true) | ||
.dumb(true) | ||
.build() | ||
) | ||
.completer(Commands.makeJLineTreeCompleter()) | ||
.build() | ||
while (true) { | ||
print(ansi().fgGreen().a(">>>").space(1).reset()) | ||
val input = readln() | ||
try { | ||
val input = builder.readLine(ansi().fgGreen().a(">>>").space(1).reset().toString()) | ||
runCommand(input) | ||
} catch (exception: EndOfFileException) { | ||
break | ||
} catch (exception: UserInterruptException) { | ||
break | ||
} catch (throwable: Throwable) { | ||
throwable.printStackTrace(System.err) | ||
} | ||
} | ||
} | ||
|
||
fun runCommand(input: String) { | ||
val result = Commands.test(input, includeSystemEnvvars = true) | ||
val result = Commands.test( | ||
input, | ||
includeSystemEnvvars = true, | ||
ansiLevel = AnsiLevel.TRUECOLOR, | ||
height = Int.MAX_VALUE, | ||
width = Int.MAX_VALUE | ||
) | ||
print(result.output) | ||
} | ||
|
||
fun runCommand(input: List<String>) { | ||
val result = Commands.test(input, includeSystemEnvvars = true) | ||
val result = Commands.test( | ||
input, | ||
includeSystemEnvvars = true, | ||
ansiLevel = AnsiLevel.TRUECOLOR, | ||
height = Int.MAX_VALUE, | ||
width = Int.MAX_VALUE | ||
) | ||
print(result.output) | ||
} | ||
|
||
} | ||
|
||
fun main(args: Array<String>) = LMFSBox.main(args) | ||
fun main(args: Array<String>) = LMFSBox.main(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.lmfs.box.command | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import com.github.ajalt.clikt.core.ProgramResult | ||
import com.github.ajalt.clikt.parameters.arguments.argument | ||
import com.github.ajalt.clikt.parameters.types.path | ||
import kotlin.io.path.Path | ||
|
||
|
||
object CdCommand : CliktCommand("Change the working directory", name = "cd") { | ||
|
||
private val to by argument("to").path(mustExist = false, canBeSymlink = true, canBeDir = true, canBeFile = false) | ||
|
||
override fun run() { | ||
val current = Path(System.getProperty("user.dir")) | ||
val changed = current.resolve(to).toFile() | ||
if (!changed.isDirectory) throw ProgramResult(1) | ||
System.setProperty("user.dir", changed.toString()) | ||
throw ProgramResult(0) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.github.lmfs.box.command | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import com.github.ajalt.clikt.core.Context | ||
import com.github.ajalt.clikt.core.subcommands | ||
import com.github.ajalt.clikt.output.HelpFormatter | ||
|
||
object Commands : CliktCommand("LMFSBox", name = ">>>", hidden = true, printHelpOnEmptyArgs = false) { | ||
|
||
init { | ||
Commands.subcommands( | ||
HelpCommand, CopyrightCommand, LicenseCommand, LsCommand, ExitCommand, | ||
TimeCommand, WhoAmICommand, PwdCommand, CdCommand, EchoCommand | ||
) | ||
} | ||
|
||
@JvmStatic | ||
val COMMAND_ALIASES = mutableMapOf<String, List<String>>() | ||
|
||
@JvmStatic | ||
fun createRootContext(parent: Context? = null): Context = | ||
CliktCommand::class.java.getDeclaredMethod( | ||
"createContext", | ||
List::class.java, | ||
Context::class.java, | ||
List::class.java | ||
).apply { | ||
isAccessible = true | ||
}.invoke(this, emptyList<Any?>(), parent, emptyList<Any?>()) as Context | ||
|
||
|
||
override fun allHelpParams(): List<HelpFormatter.ParameterHelp> { | ||
return super.allHelpParams().filterNot { it is HelpFormatter.ParameterHelp.Option && "--help" in it.names } | ||
} | ||
|
||
override fun aliases() = COMMAND_ALIASES | ||
|
||
override fun run() = Unit | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/io/github/lmfs/box/command/CopyrightCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.github.lmfs.box.command | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import io.github.lmfs.box.LMFSBox | ||
|
||
object CopyrightCommand : CliktCommand("Show copyright information", name = "copyright") { | ||
|
||
override fun run() { | ||
echo(LMFSBox.COPYRIGHT) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.github.lmfs.box.command | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import com.github.ajalt.clikt.parameters.options.default | ||
import com.github.ajalt.clikt.parameters.options.option | ||
|
||
object EchoCommand : CliktCommand("Write content to the standard output.", name = "echo") { | ||
|
||
private val text by option("--text", "-T").default("") | ||
|
||
override fun run() { | ||
echo(text) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.github.lmfs.box.command | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import com.github.ajalt.clikt.parameters.arguments.argument | ||
import com.github.ajalt.clikt.parameters.arguments.default | ||
import com.github.ajalt.clikt.parameters.types.int | ||
import kotlin.system.exitProcess | ||
|
||
object ExitCommand : CliktCommand("Exit the program", name = "exit") { | ||
|
||
private val code by argument("code", help = "Status code").int().default(0) | ||
|
||
override fun run() { | ||
exitProcess(code) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.lmfs.box.command | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import com.github.ajalt.clikt.parameters.arguments.argument | ||
import com.github.ajalt.clikt.parameters.arguments.optional | ||
|
||
object HelpCommand : CliktCommand("Show this help message", name = "help") { | ||
|
||
private val argument by argument("command").optional() | ||
|
||
override fun run() { | ||
if (argument != null) { | ||
val subCommand = Commands.registeredSubcommands() | ||
.firstOrNull { it.commandName == argument } | ||
?: HelpCommand | ||
subCommand.echoFormattedHelp() | ||
} else { | ||
Commands.echoFormattedHelp() | ||
} | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/io/github/lmfs/box/command/LicenseCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.github.lmfs.box.command | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import io.github.lmfs.box.LMFSBox | ||
|
||
object LicenseCommand : CliktCommand("Show license information", name = "license") { | ||
|
||
override fun run() { | ||
echo(LMFSBox.LICENSE) | ||
} | ||
|
||
} |
Oops, something went wrong.