Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not sure why this happens on Ubuntu and not on OS X, but one possibile work-around on Ubuntu may be to decorate the writer with your own class which appends the written characters to the StringBuffer. </p> <p>It not <em>very</em> pretty, since you will have to add OS specific code to make sure that the output is not included twice on OS X.</p> <p>Anyway, here is the script in full with the decorator class, which works fine for me on Ubuntu 12.04 using Groovy 2.0.5.</p> <pre><code>import groovy.io.GroovyPrintWriter def OUTPUT_FILE = "output.txt" def sysout = new StringBuffer() def syserr = new StringBuffer() // On Linux (Ubuntu 12.04) - starts the bc command with script and capture session to OUTPUT_FILE Process proc = "script -q -f -c bc ${OUTPUT_FILE}".execute() // On OS X with a slightly different version of Script // Process proc = "script -q ${OUTPUT_FILE} bc".execute() proc.consumeProcessOutput(sysout, syserr) // Spawns separate threads that will consume the out to prevent overflow proc.withWriter { writer -&gt; // Writes the following commands to the spawned process def gWriter = new GroovyPrintWriter(new MyCaptureWriter(writer, sysout)) // Imagine that the user enters these lines gWriter.println "a = 5" gWriter.println "b = 4" gWriter.println "c = a * b" gWriter.println "c" gWriter.println "quit" } proc.waitForOrKill(20000) // Give it 20 seconds to complete or kill the process println 'Standard out captured from process:\n' + sysout println 'Standard err captured from process:\n' + syserr class MyCaptureWriter extends Writer { @Delegate OutputStreamWriter writer def sysout MyCaptureWriter(writer, sysout) { this.writer = writer this.sysout = sysout } @Override void write(char[] cbuf, int off, int len) throws IOException { sysout.append(cbuf, off, len) writer.write(cbuf, off, len) } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload