-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
fun wrap()
to 'commons' package.
- Loading branch information
1 parent
41e7392
commit 77cc64b
Showing
3 changed files
with
62 additions
and
55 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...ons/src/commonMain/kotlin/org/jetbrains/letsPlot/commons/formatting/string/WordWrapper.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,38 @@ | ||
/* | ||
* Copyright (c) 2024. JetBrains s.r.o. | ||
* Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
*/ | ||
|
||
package org.jetbrains.letsPlot.commons.formatting.string | ||
|
||
fun wrap(text: String, lengthLimit: Int, countLimit: Int = -1): String { | ||
if (text.length <= lengthLimit || text.contains("\n")) { | ||
return text | ||
} | ||
|
||
return text.split(" ") | ||
.let { words -> | ||
val lines = mutableListOf(mutableListOf<String>()) | ||
words.forEach { word -> | ||
val freeSpace = | ||
lengthLimit - lines.last().let { line -> line.sumOf(String::length) + line.size } | ||
.coerceAtMost(lengthLimit) | ||
when { | ||
freeSpace >= word.length -> lines.last().add(word) | ||
word.length <= lengthLimit -> lines.add(mutableListOf(word)) | ||
else -> { | ||
lines.last().takeIf { freeSpace > 0 }?.add(word.take(freeSpace)) | ||
word.drop(freeSpace) | ||
.chunked(lengthLimit) | ||
.forEach { | ||
lines.add(mutableListOf<String>(it)) | ||
} | ||
} | ||
} | ||
} | ||
lines | ||
} | ||
.joinToString(separator = "\n", limit = countLimit) { | ||
it.joinToString(separator = " ") | ||
} | ||
} |
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