Skip to content

Instantly share code, notes, and snippets.

@stania
Created July 5, 2018 19:44
Show Gist options
  • Save stania/ba84fe793a3ba2abd5bd2c321a1eec42 to your computer and use it in GitHub Desktop.
Save stania/ba84fe793a3ba2abd5bd2c321a1eec42 to your computer and use it in GitHub Desktop.
import org.araqne.api.*;
import org.araqne.ansicode.*;
class StringScriptInputStream implements ScriptInputStream {
def scanner;
StringScriptInputStream(String str) {
scanner = new Scanner(new StringReader(str));
}
void flush() { while(reader.read() != -1) {} }
String readLine() { scanner.nextLine() }
String readLine(String initialValue) { scanner.nextLine() }
char read() { '' }
void supplyInput(char c) { }
void flush(Collection<Character> d) { }
void supplyFunctionKey(FunctionKeyEvent e) { }
void addFunctionKeyEventListener(FunctionKeyEventListener cb) { }
void removeFunctionKeyEventListener(FunctionKeyEventListener cb) { }
}
class CapturingOutputStream extends ByteArrayOutputStream implements ScriptOutputStream {
def console;
def mute;
def sb = new StringBuilder();
CapturingOutputStream(ScriptOutputStream os, def mute = false) { console = os; this.mute = mute; }
ScriptOutputStream println(String value) { sb.append(value + "\n"); if (!mute) console.println(value) }
ScriptOutputStream print(String value) { sb.append(value); if (!mute) console.print(value) }
ScriptOutputStream printf(String format, Object... args) { sb.append(String.format(format, args)); if (!mute) console.printf(format, args) }
ScriptOutputStream print(TelnetCommand command) { if (!mute) console.print(command) }
ScriptOutputStream print(AnsiEscapeCode code) { if (!mute) console.print(code) }
String toString() { sb.toString() }
}
def execm(String script, List<String> args = [], def inputs = null) {
exec(script, args, inputs, true)
}
def exec(String script, List<String> args = [], def inputs = null, def mute = false) {
def (alias, method) = script.split("\\.");
def selected = null;
def svcs = _bundleContext.getServiceReferences("org.araqne.api.ScriptFactory", "(alias=${alias})");
for (svc in svcs) {
try {
def scriptFactory = _bundleContext.getService(svc) as ScriptFactory;
if (!scriptFactory)
continue;
Script _script = scriptFactory.createScript();
if (_script.metaClass.respondsTo(script, method)) {
selected = _script;
break;
}
} finally {
if (svc)
_bundleContext.ungetService(svc);
}
}
if (selected) {
def old = _ctx.getInputStream();
def os = _ctx.getOutputStream();
if (inputs) {
def sb = new StringBuilder();
for (item in inputs) {
sb.append(sprintf("%s\n", item.getValue() ?: ""));
}
_ctx.setInputStream(new StringScriptInputStream(sb.toString()));
}
//selected.metaClass.getMetaMethod(method).invoke(delegate, args as String[]);
def out = new CapturingOutputStream(os, mute);
_ctx.setOutputStream(out);
selected.setScriptContext(_ctx);
try {
selected.invokeMethod(method, args as String[]);
} finally {
_ctx.setInputStream(old);
_ctx.setOutputStream(os);
}
return out.toString();
} else {
throw new IllegalArgumentException("script not found: ${script}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment