Skip to content

Commit

Permalink
add and remove tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ManApart committed Jun 22, 2024
1 parent ca82e6e commit 4a9c004
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/Mod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data class Mod(
var enabled: Boolean = false,
var categoryId: Int? = null,
var endorsed: Boolean? = null,
val tags: MutableList<String> = mutableListOf(),
val tags: MutableSet<String> = mutableSetOf(),
) {
@Transient
var show: Boolean = true
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/commands/ChangeMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import save
import toolData
import java.io.File

val changeHelp = """
val changeDescription = """
Change various parts of a mod.
Use id to set its nexus id
Use file to delete a mods stage folder and restage it from zip
Expand All @@ -14,7 +14,7 @@ val renameHelp = """
Use name to rename a mod without changing its file paths
""".trimIndent()

val changeDescription = """
val changeUsage = """
mod <index> id <new id>
mod <index> file <file-path>
mod <index> name <new name>
Expand All @@ -27,11 +27,11 @@ fun moveMod(args: List<String>){
fun changeMod(args: List<String>) {
val i = args.firstOrNull()?.toIntOrNull()
when {
args.isEmpty() -> println(changeHelp)
args.isEmpty() -> println(changeDescription)
args.size == 3 && args[1] == "id" -> updateId(i!!, args.last().toInt())
args.size == 3 && args[1] == "file" -> updateFile(i!!, args.last())
args.size == 3 && args[1] == "name" -> updateName(i!!, args.last())
else -> println(changeHelp)
else -> println(changeDescription)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/commands/CommandType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ enum class CommandType(
SOURCE("Open app source", Category.OPEN, openDescription, "", ::openSource),
NEXUS("Open app nexus page", Category.OPEN, openDescription, "", ::openNexus),
PURGE("Purge all sym links", Category.DEPLOY, purgeDescription, purgeUsage, ::purge),
MOD("Update a mod", Category.EDIT, changeHelp, changeDescription, ::changeMod),
MOD("Update a mod", Category.EDIT, changeDescription, changeUsage, ::changeMod),
TAG("Edit tags on a mod", Category.EDIT, tagDescription, tagUsage, ::tag),
PROFILE("Create and use local mod lists", Category.DEPLOY, profileDescription, profileUsage, ::profile),
RENAME("Rename a mod", Category.EDIT, renameHelp, "rename <index> <new name>", ::moveMod, "mv"),
REFRESH("Refresh mods by id", Category.UPDATE, refreshDescription, refreshUsage, ::refresh),
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/commands/Creation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ fun addCreation(creation: Creation) {
println(red("No files found for ${creation.title}"))
}

val existing = toolData.byName(creation.title, true)
val existing = toolData.byName(creation.title.lowercase(), true)
val mod = if (existing != null) existing else {
val loadOrder = toolData.nextLoadOrder()
val stagePath = modFolder.path + "/" + creation.title.replace(" ", "-")
Mod(creation.title, stagePath, loadOrder + 1).also {
Mod(creation.title.lowercase(), stagePath, loadOrder + 1).also {
it.index = toolData.mods.size
it.creationId = creation.creationId
it.add(Tag.CREATION)
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/commands/ExternalPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ val externalModUsage = """
fun externalMod(args: List<String>) {
val firstArg = args.firstOrNull() ?: ""
when {
args.isEmpty() -> println(creationDescription)
args.isEmpty() -> println(externalModDescription)
listOf("ls", "list").contains(firstArg) -> listExternal()
args.getOrNull(1) == "all" && firstArg == "add" -> addAllExternal()
firstArg == "add" -> addExternal(args.getOrNull(1)!!, args.getOrNull(2))
Expand Down Expand Up @@ -77,7 +77,7 @@ fun addExternal(esp: String, name: String? = null) {
println("Mod already managed as ${mods[esp]?.description()}")
return
}
val usedName = name ?: esp
val usedName = (name ?: esp).lowercase()

val loadOrder = toolData.nextLoadOrder()
val stagePath = modFolder.path + "/" + usedName.replace(" ", "-")
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/commands/ListMods.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ fun display(mods: List<Pair<Mod, Boolean>>) {
Column("Status", 10),
Column("Category", 20),
Column("Index", 7, true),
Column("Name", 22),
Column("Name", 60),
Column("Tags", 30),
)
val data = mods.map { (mod, displayed) ->
with(mod) {
Expand All @@ -82,6 +83,7 @@ fun display(mods: List<Pair<Mod, Boolean>>) {
"Version" to versionClean,
"Category" to category,
"Name" to name,
"Tags" to tags.joinToString(", "),
)
} to displayed
}.filter { it.second }.map { it.first }
Expand Down
79 changes: 45 additions & 34 deletions src/main/kotlin/commands/Tags.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package commands

import Column
import Mod
import Table
import confirmation
import modFolder
import cyan
import red
import save
import toolConfig
import toolData
import yellow
import java.io.File
import java.nio.file.Files

enum class Tag(val tag: String) {
CREATION("Creation"),
Expand All @@ -26,36 +22,51 @@ external add all - Attempts to add _all_ unmanaged plugins found in the data fol
""".trimIndent()

val tagUsage = """
external ls
external add <id>
external add all
tag 1 add essential
tag 1 rm essential
tag 1 rm 0
""".trimIndent()

fun tag(args: List<String>) {
val firstArg = args.firstOrNull() ?: ""
// when {
// args.isEmpty() -> println(creationDescription)
// listOf("ls", "list").contains(firstArg) -> listExternal()
// args.getOrNull(1) == "all" && firstArg == "add" -> addAllExternal()
// firstArg == "add" -> addExternal(args.getOrNull(1)!!, args.getOrNull(2))
//
// else -> println("Unknown args: ${args.joinToString(" ")}")
// }
val i = args.firstOrNull()?.toIntOrNull()
val mod = i?.let { toolData.byIndex(it) }
val command = args.getOrNull(1)?.replace("rm", "remove")
val tagArg = args.getOrNull(2)
when {
args.isEmpty() -> println(tagDescription)
mod == null -> println("Must provide the index of a valid mod to update")
command == "add" -> addTag(mod, tagArg)
command == "remove" && tagArg?.toIntOrNull() != null -> removeTag(mod, tagArg.toInt())
command == "remove" && tagArg != null -> removeTag(mod, tagArg)

else -> println("Unknown args: ${args.joinToString(" ")}")
}
}

private fun addTag(mod: Mod, tag: String?) {
if (tag == null) {
println("No tag value found to add")
return
}
mod.tags.add(tag)
save()
}

private fun removeTag(mod: Mod, tagId: Int) = removeTag(mod, mod.tags.elementAt(tagId))

private fun removeTag(mod: Mod, tag: String) {
if(!mod.tags.contains(tag)){
println(red("Tag ") + cyan("'$tag'") +red(" doesn't exist in ") + cyan("'${mod.tags.joinToString(", ")}'") + ". (Command is case sensitive.)")
return
}
println(yellow("Remove tag $tag? ") + " (y/n)")
confirmation = { c ->
if (c.firstOrNull() == "y") {
mod.tags.remove(tag)
save()
}
}
}

private fun listExternal() {
// val columns = listOf(
// Column("Esp", 42),
// Column("Index", 15),
// Column("Name", 22),
// )
// val data = getExternalMods().map { (esp, mod) ->
// mapOf(
// "Esp" to esp,
// "Index" to (mod?.index ?: ""),
// "Name" to (mod?.name ?: ""),
// )
// }
//
// Table(columns, data).print()
}


0 comments on commit 4a9c004

Please sign in to comment.