Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
hurui200320 committed Apr 19, 2024
2 parents a57bad3 + 28f500e commit b993cf7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ You can change the `EOF` to something else using `--eof=EOT`, now you can use `E
instead of `EOF`.

You can also change how many codes are printed in one line.
But do be aware this is limited by your terminal width.
By default, it's five codes per line.
But do be aware this is limited by your terminal width, if it's longer than the width, it will wrap.
By default, it's fit your terminal.
You can change it by option `-w 6`, now you have six codes per line.

When using stdin input, there is some issue with the charset on Windows.
Expand Down
4 changes: 2 additions & 2 deletions readme_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ echo "天匠染青红,花腰呈袅娜。" | ./ctc encode -c UTF-8

你也可以使用`--eof=EOT`选项来修改这个标志。这样你就可以使用`EOT`来代替`EOF`了。

你还可以修改每行打印多少个电码。但请务必知悉,这最终会受限于你终端的宽度。
默认情况下每行打印5个电码,你可以通过`-w 6`来设置为每行6个。
你还可以修改每行打印多少个电码。但请务必知悉,这最终会受限于你终端的宽度,如果你设定的值太宽,则终端会断行
默认情况下将适应你的终端,你可以通过`-w 6`来设置为每行6个。

当使用标准输入流时,在Windows系统上会有字符集的问题。
默认情况下程序使用UTF-8字符集进行输入输出。
Expand Down
23 changes: 14 additions & 9 deletions src/main/kotlin/info/skyblond/ctc/Decoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,30 @@ object Decoder {
} else error("The input is not UTF8 encoded: $startFlag")
}

private fun decode(codes: List<Int>): String {
private fun decode(codes: List<Int>, ignoreError: Boolean): String {
val sb = StringBuilder()
val codesBuffer = codes.toMutableList()

while (codesBuffer.isNotEmpty()) {
val c = codesBuffer.removeFirst()
if (c == Constants.UTF8_START || c == Constants.UTF8_COMPRESSED_START) {
codesBuffer.addFirst(c)
sb.append(codesBuffer.decodeUTF8())
} else if (codeToChar.containsKey(c)) {
sb.append(codeToChar[c]!!)
} else error("Unknown code: $c")
try {
if (c == Constants.UTF8_START || c == Constants.UTF8_COMPRESSED_START) {
codesBuffer.addFirst(c)
sb.append(codesBuffer.decodeUTF8())
} else if (codeToChar.containsKey(c)) {
sb.append(codeToChar[c]!!)
} else error("Unknown code: $c")
} catch (t: Throwable) {
if (!ignoreError) throw t
else sb.append("\u25a1")
}
}

return sb.toString()
}

fun List<Int>.decodeCTC(): String {
var str = decode(this).replace(Constants.NEW_LINE_CHAR, "\n")
fun List<Int>.decodeCTC(ignoreError: Boolean): String {
var str = decode(this, ignoreError).replace(Constants.NEW_LINE_CHAR, "\n")
Constants.reverseFixMappings.forEach { (from, to) ->
str = str.replace(from, to)
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/info/skyblond/ctc/commands/DecodeCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package info.skyblond.ctc.commands

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.groups.provideDelegate
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.help
import com.github.ajalt.clikt.parameters.options.option
import info.skyblond.ctc.Decoder.decodeCTC
import info.skyblond.ctc.derandomize
import info.skyblond.ctc.getRandomIntIter
Expand All @@ -11,6 +14,9 @@ object DecodeCommand : CliktCommand(
help = "Decode text from CTC"
) {
private val codecOptions by CodecOptions()
private val ignoreError by option("-i", "--ignore-error", help = "Ignore error")
.flag(default = false)
.help { "Ignore error and keep decoding. Default: `false`" }

override fun run() {
val content = (
Expand All @@ -28,6 +34,6 @@ object DecodeCommand : CliktCommand(
)
}

echo(codes.decodeCTC())
echo(codes.decodeCTC(ignoreError))
}
}
20 changes: 11 additions & 9 deletions src/main/kotlin/info/skyblond/ctc/commands/EncodeCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package info.skyblond.ctc.commands
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.terminal
import com.github.ajalt.clikt.parameters.groups.provideDelegate
import com.github.ajalt.clikt.parameters.options.check
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.defaultLazy
import com.github.ajalt.clikt.parameters.options.help
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.int
Expand All @@ -20,18 +19,15 @@ object EncodeCommand : CliktCommand(
private val codecOptions by CodecOptions()

private val codePerLine by option("--code-per-line", "-w")
.int().default(5)
.help { "How many code to print in one line (at most), by default is `5`" }
.check { it > 0 }

.int().defaultLazy { terminal.info.width / 6 }
.help { "How many code to print in one line, by default fit your terminal. Use `-1` to disable formatting." }


override fun run() {
val content = codecOptions.text
?: codecOptions.file?.readText()
?: askContent(codecOptions.eof, codecOptions.charset)
var codes = content.encodeCTC()
val width = (terminal.info.width / 6).coerceAtMost(codePerLine)
val secret = codecOptions.getSecret()
if (secret != null) {
codes = codes.randomize(
Expand All @@ -40,8 +36,14 @@ object EncodeCommand : CliktCommand(
)
}

codes.toCTCString().chunked(width * 6).forEach {
echo(it)
codes.toCTCString().let { s ->
if (codePerLine > 0) {
s.chunked(codePerLine * 6).forEach {
echo(it)
}
} else {
echo(s)
}
}
}
}

0 comments on commit b993cf7

Please sign in to comment.