-
Notifications
You must be signed in to change notification settings - Fork 0
/
man.sbt
82 lines (75 loc) · 2.94 KB
/
man.sbt
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Copyright © 2014, 2015 Typesafe, Inc. All rights reserved. [http://www.typesafe.com]
*/
import complete.DefaultParsers._
import scala.Console
import scala.util.matching._
val manDetail: String = "Displays the README.md file. Use <noarg> for setup README.md or <e> for exercise README.md"
lazy val optArg = OptSpace ~> StringBasic.?
def man: Command = Command("man")(_ => optArg) { (state, arg) =>
arg match {
case Some(a) if a == "e" =>
val base: File = Project.extract(state).get(sourceDirectory)
val basePath: String = base + "/test/resources/README.md"
printOut(basePath)
Console.print("\n")
state
case Some(a) =>
Console.print("\n")
Console.println(Console.RED + "[ERROR] " + Console.RESET + "invalid argument " + Console.RED + a + Console.RESET + ". " + manDetail)
Console.print("\n")
state
case None =>
val base: File = Project.extract(state).get(baseDirectory)
val basePath: String = base + "/README.md"
printOut(basePath)
Console.print("\n")
state
}
}
val bulletRx: Regex = """- """.r
val boldRx: Regex = """(\*\*)(\w*)(\*\*)""".r
val codeRx: Regex = """(`)([a-zA-Z >\.\/\-]*)(`)""".r
val fenceStartRx: Regex = """^```(bash|scala)$""".r
val fenceEndRx: Regex = """^```$""".r
val numberRx: Regex = """^(\d{1,3})(\. )""".r
val urlRx: Regex = """(\()(htt[a-zA-Z0-9\-\.\/:]*)(\))""".r
val ConBlue = Console.BLUE
val ConMagenta = Console.MAGENTA
val ConRed = Console.RED
val ConReset = Console.RESET
val ConYellow = Console.YELLOW
def printOut(path: String) {
IO.readLines(new sbt.File(path)) foreach {
case ln if ln.length > 0 && ln(0).equals('#') =>
Console.println(ConRed + ln + ConReset)
case ln if ln.matches(".*" + bulletRx.toString() + ".*") =>
val lne = bulletRx replaceAllIn (ln, ConRed + bulletRx.toString() + ConReset)
Console.println(rxFormat(rxFormat(rxFormat(lne, codeRx, ConBlue), boldRx, ConYellow), urlRx, ConMagenta,
keepWrapper = true))
case ln if ln.matches(numberRx.toString() + ".*") =>
val lne = numberRx replaceAllIn (ln, _ match { case numberRx(n, s) => f"$ConRed$n$s$ConReset" })
Console.println(rxFormat(rxFormat(lne, codeRx, ConBlue), boldRx, ConYellow))
case ln if ln.matches(fenceStartRx.toString()) =>
Console.print(ConBlue)
case ln if ln.matches(fenceEndRx.toString()) =>
Console.print(ConReset)
case ln =>
Console.println(rxFormat(rxFormat(rxFormat(ln, codeRx, ConBlue), boldRx, ConYellow), urlRx, ConMagenta,
keepWrapper = true))
}
}
def rxFormat(ln: String, rx: Regex, startColor: String, keepWrapper: Boolean = false): String = ln match {
case `ln` if ln.matches(".*" + rx.toString + ".*") =>
val lne = rx replaceAllIn (ln, _ match {
case rx(start, in, stop) =>
if (keepWrapper)
f"$start$startColor$in$ConReset$stop"
else
f"$startColor$in$ConReset"
})
lne
case _ =>
ln
}
commands in Global ++= Seq(man)