-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpackage.scala
54 lines (43 loc) · 1.62 KB
/
package.scala
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
package replpp
import java.nio.file.{FileSystems, Files, Path}
import replpp.shaded.fansi
import scala.collection.immutable.Seq
import scala.io.Source
import scala.util.{Try, Using}
package object util {
def currentWorkingDirectory = Path.of(".")
def sequenceTry[A](tries: Seq[Try[A]]): Try[Seq[A]] = {
tries.foldRight(Try(Seq.empty[A])) {
case (next, accumulator) =>
for {
a <- next
acc <- accumulator
} yield a +: acc
}
}
def linesFromFile(path: Path): Seq[String] =
Using.resource(Source.fromFile(path.toFile))(_.getLines.toSeq)
def deleteRecursively(path: Path): Unit = {
if (Files.isDirectory(path))
Files.list(path).forEach(deleteRecursively)
Files.deleteIfExists(path)
}
def readFileFromZip(zipFile: Path, fileName: String): Try[Array[Byte]] = {
Using(FileSystems.newFileSystem(zipFile, null)) { fileSystem =>
Files.readAllBytes(fileSystem.getPath(fileName))
}
}
def colorise(value: String, color: fansi.EscapeAttr)(using colors: Colors): String = {
colors match {
case Colors.BlackWhite => value
case Colors.Default => color(value).render
}
}
/** Lookup the current terminal width - useful e.g. if you want to render something using the maximum space available.
* Uses jline-jna, which therefor needs to be in the repl's classpath, which is why it's listed in
* {{{replpp.Config.ForClasspath.DefaultInheritClasspathIncludes}}} */
def terminalWidth: Try[Int] =
Using(org.jline.terminal.TerminalBuilder.terminal)(_.getWidth)
def pathAsString(path: Path): String =
path.toAbsolutePath.toString
}