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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package cask.internal
import java.io.{InputStream, PrintWriter, StringWriter}
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable
import java.io.OutputStream
object Util {
def transferTo(in: InputStream, out: OutputStream) = {
val buffer = new Array[Byte](8192)
while ({
in.read(buffer) match{
case -1 => false
case n =>
out.write(buffer, 0, n)
true
}
}) ()
}
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 stackTraceString(e: Throwable) = {
val trace = new StringWriter()
val pw = new PrintWriter(trace)
e.printStackTrace(pw)
pw.flush()
trace.toString
}
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
}
def sequenceEither[A, B, M[X] <: TraversableOnce[X]](in: M[Either[A, B]])(
implicit cbf: CanBuildFrom[M[Either[A, B]], B, M[B]]): Either[A, M[B]] = {
in.foldLeft[Either[A, mutable.Builder[B, M[B]]]](Right(cbf(in))) {
case (acc, el) =>
for (a <- acc; e <- el) yield a += e
}
.map(_.result())
}
}
|