Skip to content

Instantly share code, notes, and snippets.

@blizzarddreams
Created December 23, 2015 20:50
Show Gist options
  • Select an option

  • Save blizzarddreams/400d73a5c23a2648bfdc to your computer and use it in GitHub Desktop.

Select an option

Save blizzarddreams/400d73a5c23a2648bfdc to your computer and use it in GitHub Desktop.
package ircscalabot
import javax.script.ScriptException
import java.io.{PrintWriter, ByteArrayOutputStream, PrintStream}
import org.json4s._
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization.write
import scala.collection.JavaConverters._
import scala.tools.nsc.Settings
import scala.tools.nsc.interpreter.IMain
import scalaj.http.Http
import scala.util.Properties.versionString
object Commands {
val settings = new Settings()
settings.usejavacp.value = true
val out = new ByteArrayOutputStream()
val repl = new IMain(settings, new PrintWriter(out))
val old = Console.out
val commands = Map[String, (String) => String](
"scala" -> scala,
"version" -> version
)
def version(args: String): String = versionString
def scala(args: String): String = {
if (args.contains("scala.sys.process")) {
return "I can't evaluate that."
}
try {
Console.setOut(new PrintStream(out))
repl.interpret(args)
val outString = out.toString().trim()
Console.setOut(old)
println(s"it is $out")
out.reset()
if (outString.contains('\n') && outString.split("\n").length > 1) {
val str = s"Input:\n\n```scala\n$args\n```\n\nOutput:\n```\n${outString}\n```"
val gitIoUrl = CommandHelpers.makeGitIoUrl(CommandHelpers.writeToGist(str))
s"${outString.split("\n")(0).trim()} - Output truncated: $gitIoUrl"
} else {
outString.trim()
}
} catch {
case e: NullPointerException => "I can't evaluate that."
case e: ScriptException => "I can't evaluate that."
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment