Skip to content

Commit

Permalink
Fixes #326 introducing new parameter to disable ansi colors on repl. …
Browse files Browse the repository at this point in the history
…Backward compatible. Still no UI pref page to configure from the IDE
  • Loading branch information
javierfernandes committed Sep 17, 2016
1 parent 5b3c9ac commit f5b667f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import java.rmi.ConnectException
import net.sf.lipermi.handler.CallHandler
import net.sf.lipermi.net.Client
import org.uqbar.project.wollok.debugger.server.XDebuggerImpl
import org.uqbar.project.wollok.debugger.server.out.AsyncXTextInterpreterEventPublisher
import org.uqbar.project.wollok.debugger.server.out.XTextInterpreterEventPublisher
import org.uqbar.project.wollok.debugger.server.rmi.CommandHandlerFactory
import org.uqbar.project.wollok.interpreter.WollokInterpreter
import org.uqbar.project.wollok.interpreter.WollokRuntimeException
import org.uqbar.project.wollok.interpreter.api.XDebugger
import org.uqbar.project.wollok.interpreter.debugger.XDebuggerOff
import org.uqbar.project.wollok.launch.repl.AnsiColoredReplOutputFormatter
import org.uqbar.project.wollok.launch.repl.RegularReplOutputFormatter
import org.uqbar.project.wollok.launch.repl.WollokRepl
import org.uqbar.project.wollok.wollokDsl.WFile
import org.uqbar.project.wollok.debugger.server.out.AsyncXTextInterpreterEventPublisher

/**
* Main program launcher for the interpreter.
Expand All @@ -39,7 +41,8 @@ class WollokLauncher extends WollokChecker {
interpreter.interpret(parsed)

if (parameters.hasRepl) {
new WollokRepl(this, injector, interpreter, mainFile, parsed).startRepl
val formatter = if (parameters.noAnsiFormat) new RegularReplOutputFormatter else new AnsiColoredReplOutputFormatter
new WollokRepl(this, injector, interpreter, mainFile, parsed, formatter).startRepl
}
System.exit(0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class WollokLauncherParameters {
boolean jsonOutput = false
@Accessors
boolean tests = false
@Accessors
boolean noAnsiFormat = false

def build() {
val sb = new StringBuilder
Expand All @@ -39,6 +41,8 @@ class WollokLauncherParameters {
if (testPort != null) sb.append("-testPort " + testPort.toString).append(" ")
if (tests) sb.append("-t ")
if (jsonOutput) sb.append("-jsonOutput ")
if (noAnsiFormat) sb.append("-noAnsiFormat ")

wollokFiles.forEach [ sb.append(it).append(" ") ]
sb.toString
}
Expand All @@ -52,6 +56,8 @@ class WollokLauncherParameters {
testPort = parseParameter(cmdLine, "testPort")

jsonOutput = cmdLine.hasOption("jsonOutput")

noAnsiFormat = cmdLine.hasOption("noAnsiFormat")

requestsPort = parseParameter(cmdLine, "requestsPort")
eventsPort = parseParameter(cmdLine, "eventsPort")
Expand All @@ -62,16 +68,16 @@ class WollokLauncherParameters {

wollokFiles = cmdLine.argList

if(!wollokFiles.empty && hasRepl && !wollokFiles.get(0).endsWith(".wlk")){
if (!wollokFiles.empty && hasRepl && !wollokFiles.get(0).endsWith(".wlk")){
throw new RuntimeException("Repl can only be used with .wlk files.")
}

if(wollokFiles.empty && !hasRepl){
if (wollokFiles.empty && !hasRepl){
throw new RuntimeException("You must provide a file or use the REPL")
}

//If the parameters are empty and we are in the REPL, I generate an empty file to be able of loading the REPL
if(wollokFiles.empty){
if (wollokFiles.empty){
val temp = new File("wollokREPL.wlk")
temp.deleteOnExit

Expand Down Expand Up @@ -109,6 +115,8 @@ class WollokLauncherParameters {

addOption(new Option("jsonOutput", "JSON test report output"))

addOption(new Option("noAnsiFormat", "Disables ANSI colors for the console"))

add("testPort", "Server port for tests", "port", 1)
add("requestsPort", "Request ports", "port", 1)
add("eventsPort", "Events ports", "port", 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.uqbar.project.wollok.launch.repl

import static org.fusesource.jansi.Ansi.*
import static org.fusesource.jansi.Ansi.Color.*

/**
* An strategy to allow ANSI colors output
* or regular output without special characters
*
* @author jfernandes
*/
interface ReplOutputFormatter {
def String errorStyle(CharSequence msg)
def String importantMessageStyle(CharSequence msg)
def String messageStyle(CharSequence msg)
def String returnStyle(CharSequence msg)
}

class RegularReplOutputFormatter implements ReplOutputFormatter {
override def errorStyle(CharSequence msg) { msg.toString }
override def importantMessageStyle(CharSequence msg) { msg.toString }
override def messageStyle(CharSequence msg) { msg.toString }
override def returnStyle(CharSequence msg) { msg.toString }
}


class AnsiColoredReplOutputFormatter implements ReplOutputFormatter {
static val COLOR_RETURN_VALUE = BLUE
static val COLOR_ERROR = RED
static val COLOR_REPL_MESSAGE = CYAN

override def errorStyle(CharSequence msg) { ansi.fg(COLOR_ERROR).a(msg).reset.toString() }
override def importantMessageStyle(CharSequence msg) { ansi.fg(COLOR_REPL_MESSAGE).bold.a(msg).reset.toString() }
override def messageStyle(CharSequence msg) { ansi.fg(COLOR_REPL_MESSAGE).a(msg).reset.toString() }
override def returnStyle(CharSequence msg) { ansi().fg(COLOR_RETURN_VALUE).a(msg).reset.toString() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import org.uqbar.project.wollok.interpreter.core.WollokProgramExceptionWrapper
import org.uqbar.project.wollok.launch.WollokLauncher
import org.uqbar.project.wollok.wollokDsl.WFile

import static org.fusesource.jansi.Ansi.*
import static org.fusesource.jansi.Ansi.Color.*


import static extension org.uqbar.project.wollok.model.WollokModelExtensions.*
import org.uqbar.project.wollok.interpreter.core.WollokObject
Expand All @@ -31,10 +30,6 @@ import static org.uqbar.project.wollok.launch.repl.Messages.*
*/
// I18N
class WollokRepl {
static val COLOR_RETURN_VALUE = BLUE
static val COLOR_ERROR = RED
static val COLOR_REPL_MESSAGE = CYAN

val Injector injector
val WollokLauncher launcher
val WollokInterpreter interpreter
Expand All @@ -43,14 +38,16 @@ class WollokRepl {
val static prompt = ">>> "
var static whiteSpaces = ""
val WFile parsedMainFile
val extension ReplOutputFormatter formatter

new(WollokLauncher launcher, Injector injector, WollokInterpreter interpreter, File mainFile, WFile parsedMainFile) {
new(WollokLauncher launcher, Injector injector, WollokInterpreter interpreter, File mainFile, WFile parsedMainFile, ReplOutputFormatter formatter) {
this.injector = injector
this.launcher = launcher
this.interpreter = interpreter
this.interpreter.interactive = true
this.mainFile = mainFile
this.parsedMainFile = parsedMainFile
this.formatter = formatter
}

def void startRepl() {
Expand Down Expand Up @@ -190,15 +187,9 @@ class WollokRepl {
handleException(e.cause)
}
}
def static getPrompt(){
def getPrompt(){
prompt.messageStyle.toString
}
// ********** STYLING

// applies styles for errors
def static errorStyle(CharSequence msg) { ansi.fg(COLOR_ERROR).a(msg).reset }
def static importantMessageStyle(CharSequence msg) { ansi.fg(COLOR_REPL_MESSAGE).bold.a(msg).reset }
def static messageStyle(CharSequence msg) { ansi.fg(COLOR_REPL_MESSAGE).a(msg).reset }
def static returnStyle(CharSequence msg) { ansi().fg(COLOR_RETURN_VALUE).a(msg).reset }

}

0 comments on commit f5b667f

Please sign in to comment.