Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
2.3 kB
2
Indexable
Never
    private fun printKeyValueLine(keyValueLine: KeyValueLine) {
        if (keyValueLine.keySegment.isEmpty() && keyValueLine.valueSegment.isEmpty()) return

        val logicalWidth = getLogicalWidth(keyValueLine.textFormat.fontSize)

        val bufferArray = CharArray(logicalWidth)
        bufferArray.fill(' ')

        val nextLine = KeyValueLine(textFormat = keyValueLine.textFormat)

        val maxCountForSegment = logicalWidth / 2

        if (keyValueLine.keySegment.isNotEmpty()) {
            if (keyValueLine.leftSegmentCount > maxCountForSegment) {
                val indexOfNextCharacter = copySegmentToCharArrayFromStart(
                    keyValueLine.keySegment,
                    bufferArray, 0, maxCountForSegment - 1
                )
                if (indexOfNextCharacter < keyValueLine.leftSegmentCount) {
                    nextLine.keySegment =
                        LineSegment(keyValueLine.keySegment.text.substring(indexOfNextCharacter))
                }
            } else {
                copySegmentToCharArrayFromStart(
                    keyValueLine.keySegment,
                    bufferArray,
                    0,
                    maxCountForSegment - 1
                )
            }
        }

        if (keyValueLine.valueSegment.isNotEmpty()) {
            if (keyValueLine.rightSegmentCount > maxCountForSegment) {
                val indexOfNextCharacter = copySegmentToCharArrayFromStart(
                    keyValueLine.valueSegment,
                    bufferArray, maxCountForSegment, logicalWidth - 1
                )
                if (indexOfNextCharacter > 0) {
                    nextLine.valueSegment =
                        LineSegment(keyValueLine.valueSegment.text.substring(indexOfNextCharacter))
                }
            } else {
                copySegmentToCharArrayFromStart(
                    keyValueLine.valueSegment,
                    bufferArray,
                    maxCountForSegment,
                    logicalWidth - 1
                )
            }
        }

        val string = String(bufferArray)
        addPrintText(string, format = keyValueLine.textFormat)

        if (nextLine.keySegment.isNotEmpty() || nextLine.valueSegment.isNotEmpty()) {
            printKeyValueLine(nextLine)
        }
    }
Leave a Comment