-
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.
Merge pull request #5 from Paulanerus/dev
Dev
- Loading branch information
Showing
18 changed files
with
580 additions
and
98 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
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,14 @@ | ||
package dev.paulee.api.data | ||
|
||
data class Change(val str: String, val tokens: List<Pair<String, IntRange>>) | ||
|
||
interface DiffService { | ||
|
||
fun getDiff(strings: List<String>): Set<Change> | ||
|
||
fun getDiff(original: String, str: String): Change? | ||
|
||
fun oldValue(change: Change): String | ||
|
||
fun newValue(change: Change): String | ||
} |
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
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
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
53 changes: 53 additions & 0 deletions
53
core/src/main/kotlin/dev/paulee/core/data/DiffServiceImpl.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,53 @@ | ||
package dev.paulee.core.data | ||
|
||
import com.github.difflib.text.DiffRowGenerator | ||
import dev.paulee.api.data.Change | ||
import dev.paulee.api.data.DiffService | ||
|
||
class DiffServiceImpl : DiffService { | ||
|
||
private val generator = | ||
DiffRowGenerator.create().mergeOriginalRevised(true).showInlineDiffs(true).oldTag { f -> "~~" } | ||
.newTag { f -> "**" }.build() | ||
|
||
override fun getDiff(strings: List<String>): Set<Change> { | ||
|
||
if (strings.size <= 1) return emptySet() | ||
|
||
val first = listOf(strings[0]) | ||
|
||
return (1 until strings.size).map { | ||
val output = generator.generateDiffRows(first, listOf(strings[it])) | ||
|
||
val oldLine = output.first().oldLine | ||
|
||
Change(oldLine, extractToken(oldLine)) | ||
}.toSet() | ||
} | ||
|
||
override fun getDiff(original: String, str: String): Change? = this.getDiff(listOf(original, str)).firstOrNull() | ||
|
||
override fun oldValue(change: Change): String = with(change) { | ||
tokens.fold(str) { acc, token -> | ||
if (token.first.startsWith("**")) acc.replace(token.first, "") | ||
else acc.replace(token.first, token.first.trim('~')) | ||
} | ||
} | ||
|
||
override fun newValue(change: Change): String = with(change) { | ||
tokens.fold(str) { acc, token -> | ||
if (token.first.startsWith("~~")) acc.replace(token.first, "") | ||
else acc.replace(token.first, token.first.trim('*')) | ||
} | ||
} | ||
|
||
private fun extractToken(str: String): List<Pair<String, IntRange>> { | ||
val patternOld = Regex("~~([^~]*)~~") | ||
val patternNew = Regex("\\*\\*([^*]*)\\*\\*") | ||
|
||
val entriesOld = patternOld.findAll(str).map { it.groupValues[0] to it.range }.toList() | ||
val entriesNew = patternNew.findAll(str).map { it.groupValues[0] to it.range }.toList() | ||
|
||
return entriesOld + entriesNew | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package dev.paulee.ui | ||
|
||
import java.nio.file.Path | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.bufferedReader | ||
import kotlin.io.path.bufferedWriter | ||
import kotlin.io.path.notExists | ||
import kotlin.reflect.KMutableProperty | ||
import kotlin.reflect.KVisibility | ||
import kotlin.reflect.full.memberProperties | ||
|
||
object Config { | ||
|
||
var noWidthRestriction = false | ||
|
||
private var configFile = "config" | ||
|
||
private var configPath = Path(configFile) | ||
|
||
fun save() { | ||
this.configPath.bufferedWriter().use { writer -> | ||
|
||
this::class.memberProperties | ||
.filter { it.visibility == KVisibility.PUBLIC && it is KMutableProperty<*> } | ||
.forEach { | ||
val value = it.getter.call(this) | ||
|
||
writer.write("${it.name} = $value\n") | ||
writer.newLine() | ||
} | ||
} | ||
} | ||
|
||
fun load(path: Path) { | ||
this.configPath = path.resolve(configFile) | ||
|
||
if(this.configPath.notExists()) return | ||
|
||
this.configPath.bufferedReader().useLines { lines -> | ||
lines.filter { it.contains("=") }.forEach { | ||
val (field, value) = it.split("=", limit = 2).map { it.trim() } | ||
|
||
val member = this::class.memberProperties.find { it.name == field } | ||
|
||
if (member != null && member is KMutableProperty<*>) { | ||
val converted: Any = when (member.returnType.classifier) { | ||
Boolean::class -> value.toBooleanStrictOrNull() ?: false | ||
Short::class -> value.toShortOrNull() ?: 0 | ||
Int::class -> value.toIntOrNull() ?: 0 | ||
Long::class -> value.toLongOrNull() ?: 0L | ||
Float::class -> value.toFloatOrNull() ?: 0f | ||
Double::class -> value.toDoubleOrNull() ?: 0 | ||
else -> value | ||
} | ||
|
||
runCatching { | ||
member.setter.call( | ||
this, | ||
converted | ||
) | ||
}.onFailure { e -> println("Failed to set value for $field (${e.message}).") } | ||
} else { | ||
//TODO | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.