/
PrintAction.scala
147 lines (126 loc) · 5.33 KB
/
PrintAction.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package info.kwarc.mmt.api.frontend.actions
import info.kwarc.mmt.api.frontend.{Controller, actions}
import info.kwarc.mmt.api.utils.{MMTSystem, OS}
import scala.collection.mutable.ListBuffer
import scala.util.Try
/** an action that responds to the user */
trait ResponsiveAction extends Action {
/** prints a response to the caller */
def respond(x: Any*) = controller.report.apply("user", x.map(_.toString).mkString(", "))
}
/** Shared base class for Actions for printing something */
sealed abstract class PrintAction extends ResponsiveAction {}
case object MMTLegal extends PrintAction {
def apply(): Unit = respond(MMTSystem.legalNotices)
def toParseString = "show notices"
}
object MMTLegalCompanion extends ObjectActionCompanion(MMTLegal, "show legal notices", "show notices")
case object MMTInfo extends PrintAction {
def apply(): Unit = {
respond(s"MMT Version : ${MMTSystem.version}")
respond(s"Run Style : ${MMTSystem.runStyle}")
MMTSystem.buildTime foreach {s =>
respond(s"Build time : $s")
}
respond(s"Operation System: ${OS.detect}")
respond("use 'show extensions' to show current extensions. ")
respond("use 'show mathpath' to show loaded content. ")
respond("use 'show server' to show current server. ")
}
def toParseString = "show mmt"
}
object MMTInfoCompanion extends ObjectActionCompanion(MMTInfo, "show mmt system information", "show mmt")
case object MMTVersion extends PrintAction {
def apply(): Unit = {
respond(MMTSystem.version)
}
def toParseString = "show version"
}
object MMTVersionCompanion extends ObjectActionCompanion(MMTVersion, "show mmt version information", "show version")
/** print all loaded knowledge items to STDOUT in text syntax */
case object ClearConsole extends PrintAction {
def apply(): Unit = {
System.out.print("\u001b[H\u001b[2J")
System.out.flush()
}
def toParseString = "clear console"
}
object ClearConsoleCompanion extends ObjectActionCompanion(ClearConsole, "clears the console", "clear console")
/** print all loaded knowledge items to STDOUT in text syntax */
case object PrintAll extends PrintAction {
def apply(): Unit = {
respond("\n" + controller.library.toString)
}
def toParseString = "show knowledge"
}
object PrintAllCompanion extends ObjectActionCompanion(PrintAll, "print all loaded knowledge items to STDOUT in text syntax", "show knowledge")
/** print all loaded knowledge items to STDOUT in XML syntax */
case object PrintAllXML extends PrintAction {
def apply(): Unit = {
respond("\n" + controller.library.getModules.map(_.toNode).mkString("\n"))
}
def toParseString = "show xml"
}
object PrintAllXMLCompanion extends ObjectActionCompanion(PrintAllXML, "print all loaded knowledge items to STDOUT in xml syntax", "show xml")
/** print all configuration entries to STDOUT */
case object PrintConfig extends PrintAction {
def apply(): Unit = {
respond(controller.getConfigString())
}
def toParseString = "show config"
}
object PrintConfigCompanion extends ObjectActionCompanion(PrintConfig, "print all configuration to stdout", "show config")
case class HelpAction(topic: String) extends PrintAction {
// list of all known help Topics
private def helpTopics : List[String] = (
MMTSystem.getResourceList("/help-text/").flatMap({
case s: String if s.endsWith(".txt") => Some(s.stripSuffix(".txt"))
case _ => None
}) ::: controller.extman.get(classOf[ActionCompanion]).flatMap(_.keywords).distinct ::: List("topics")
).sorted
/** gets dynamically generated help entries */
private def getDynamicHelp(topic: String) : Option[String] = topic match {
case "" => getHelpText("help")
case "topics" =>
val lines = new ListBuffer[String]()
lines += "Type 'help <topic>' for more information about a specific topic. "
lines += ""
helpTopics.map(lines +=)
Some(lines.mkString("\n"))
case _ => None
}
/** gets the (static) help text for a given topic or None */
private def getHelpText(topic: String) : Option[String] = if(topic.matches("[A-Za-z_-]+")) {
Try(MMTSystem.getResourceAsString("/help-text/" + topic + ".txt")).toOption
} else {
None
}
/** gets the help text for a given action or None */
private def getActionHelp(action: String) : Option[String] = {
val companionO = controller.extman.getOrAddExtension(classOf[ActionCompanion], action)
companionO map {ac =>
ac.mainKeyword + ": " + ac.helpText
}
}
def apply(): Unit = {
val topicActual = topic.trim
// try and get a string that represents help
getDynamicHelp(topicActual).getOrElse(getHelpText(topicActual).getOrElse(getActionHelp(topicActual).getOrElse(""))) match {
case "" => respond(s"No help on '$topic' available")
case s: String => logGroup {
respond(s)
}
}
}
def toParseString: String = s"show help $topic".trim
}
object HelpActionCompanion extends ActionCompanion("print help about a given topic", "show help", "help") {
import Action._
override def parserActual(implicit state: ActionState): actions.Action.Parser[HelpAction] = (strMaybeQuoted *) ^^ { s => HelpAction(s.mkString(" ")) }
}
/** utility methods for handling [[PrintAction]]s */
trait PrintActionHandling {
self: Controller =>
/** returns a string expressing the current configuration */
def getConfigString(): String = state.config.toString
}