<commit_before>/*
 * Copyright (c) 2011, <NAME> <karltk at strategoxt dot org>
 *
 * Licensed under the GNU Lesser General Public License, v2.1
 */
package org.spoofax.interpreter.ui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Writer;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.console.IOConsole;
import org.eclipse.ui.console.IOConsoleOutputStream;
import org.spoofax.interpreter.library.IOAgent;

public class SpoofaxConsole extends IOConsole implements Runnable {

	public static class ConsoleIOAgent extends IOAgent {

		final PrintWriter out;
		final PrintWriter err;

		public ConsoleIOAgent(IOConsoleOutputStream out,
				IOConsoleOutputStream err) {
			this.out = new PrintWriter(out);
			this.err = new PrintWriter(err);
		}

		@Override
		public Writer getWriter(int fd) {
			if (fd == CONST_STDERR) {
				return err;
			} else if (fd == CONST_STDOUT) {
				return out;
			}
			return super.getWriter(fd);
		}

	}

	public SpoofaxConsole() {
		super("Spoofax Console", ImageDescriptor.getMissingImageDescriptor());
		Thread t = new Thread(this);
		t.start();
	}

	@Override
	public String getName() {
		return "Spoofax Console";
	}

	@Override
	public void run() {
		try {
			final IOConsoleOutputStream err = newOutputStream();
			final IOConsoleOutputStream out = newOutputStream();
			final IOConsoleOutputStream prompt = newOutputStream();

			Display.getDefault().syncExec(new Runnable() {
				
				@Override
				public void run() {
					err.setColor(new Color(null, 255, 0, 0));
					out.setColor(new Color(null, 234, 123, 195));
					prompt.setColor(new Color(null, 95, 200, 23));
				}
			});
			
			ConsoleIOAgent ioAgent = new ConsoleIOAgent(out, err);
			SpoofaxInterpreter intp = new SpoofaxInterpreter(ioAgent);
			BufferedReader br = new BufferedReader(new InputStreamReader(
					getInputStream()));
			for (;;) {
				try {
					prompt.write("> ");
					prompt.flush();
					String line = br.readLine();
					if (!intp.eval(line))<commit_msg>unified Spoofax and Spoofax/IMP console (right now, this only works if you started the shell first)<commit_after>/*
 * Copyright (c) 2011, <NAME> <karltk at strategoxt dot org>
 *
 * Licensed under the GNU Lesser General Public License, v2.1
 */
package org.spoofax.interpreter.ui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Writer;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.console.IOConsole;
import org.eclipse.ui.console.IOConsoleOutputStream;
import org.spoofax.interpreter.library.IOAgent;

public class SpoofaxConsole extends IOConsole implements Runnable {
	
	public static final String CONSOLE_NAME =
		"Spoofax Console";

	public static class ConsoleIOAgent extends IOAgent {

		final PrintWriter out;
		final PrintWriter err;

		public ConsoleIOAgent(IOConsoleOutputStream out,
				IOConsoleOutputStream err) {
			this.out = new PrintWriter(out);
			this.err = new PrintWriter(err);
		}

		@Override
		public Writer getWriter(int fd) {
			if (fd == CONST_STDERR) {
				return err;
			} else if (fd == CONST_STDOUT) {
				return out;
			}
			return super.getWriter(fd);
		}

	}

	public SpoofaxConsole() {
		super(CONSOLE_NAME, ImageDescriptor.getMissingImageDescriptor());
		Thread t = new Thread(this);
		t.start();
	}

	@Override
	public String getName() {
		return "Spoofax Console";
	}

	@Override
	public void run() {
		try {
			final IOConsoleOutputStream err = newOutputStream();
			final IOConsoleOutputStream out = newOutputStream();
			final IOConsoleOutputStream prompt = newOutputStream();

			Display.getDefault().syncExec(new Runnable() {
				
				@Override
				public void run() {
					err.setColor(new Color(null, 255, 0, 0));
					out.setColor(new Color(null, 234, 123, 195));
					prompt.setColor(new Color(null, 95, 200, 23));
				}
			});
			
			ConsoleIOAgent ioAgent = new ConsoleIOAgent(out, err);
			SpoofaxInterpreter intp = new SpoofaxInterpreter(ioAgent);
			BufferedReader br = new BufferedReader(new InputStreamReader(
					getInputStream()));
			for (;;) {
				try {
					prompt.write("> ");
					prompt.flush();
					
					String line = br.readLine();
					if (!intp.eval(line))
{'fraction_non_alphanumeric': 0.1103448275862069, 'fraction_numerical': 0.012013348164627364, 'mean_word_length': 3.9679558011049725, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2501883', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1384, 'n_words': 360}