blob: 59bc2ce6bd197b73978b17fb4f4664d8270cd9cb (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package cask.internal
object Util {
def pluralize(s: String, n: Int) = {
if (n == 1) s else s + "s"
}
def splitPath(p: String) =
p.dropWhile(_ == '/').reverse.dropWhile(_ == '/').reverse.split('/').filter(_.nonEmpty)
def softWrap(s: String, leftOffset: Int, maxWidth: Int) = {
val oneLine = s.lines.mkString(" ").split(' ')
lazy val indent = " " * leftOffset
val output = new StringBuilder(oneLine.head)
var currentLineWidth = oneLine.head.length
for(chunk <- oneLine.tail){
val addedWidth = currentLineWidth + chunk.length + 1
if (addedWidth > maxWidth){
output.append("\n" + indent)
output.append(chunk)
currentLineWidth = chunk.length
} else{
currentLineWidth = addedWidth
output.append(' ')
output.append(chunk)
}
}
output.mkString
}
}
|