<commit_before>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.jreadline.console;

import org.jboss.jreadline.edit.EditMode;
import org.jboss.jreadline.edit.EmacsEditMode;
import org.jboss.jreadline.edit.PasteManager;
import org.jboss.jreadline.edit.ViEditMode;
import org.jboss.jreadline.edit.actions.*;
import org.jboss.jreadline.history.History;
import org.jboss.jreadline.history.InMemoryHistory;
import org.jboss.jreadline.history.SearchDirection;
import org.jboss.jreadline.terminal.POSIXTerminal;
import org.jboss.jreadline.terminal.Terminal;
import org.jboss.jreadline.undo.UndoAction;
import org.jboss.jreadline.undo.UndoManager;

import java.io.*;

/**
 * @author <NAME> <<EMAIL>>
 */
public class Console {

    private Writer outStream;
    private Buffer buffer;
    private Terminal terminal;

    private UndoManager undoManager;
    private PasteManager pasteManager;
    private EditMode editMode;
    private History history;

    private Action prevAction = Action.EDIT;

    private static final String CR = System.getProperty("line.separator");

    public Console() throws IOException {
        this(new FileInputStream(FileDescriptor.in),
             new PrintWriter( new OutputStreamWriter(System.out)));

    }

    public Console(InputStream in, Writer out)  {
        this(in, out, null);
    }

    public Console(InputStream in, Writer out, Terminal terminal) {
        this(in, out, terminal, null);
    }

    public Console(InputStream in, Writer out, Terminal terminal, EditMode mode) {
        setOutWriter(out);
        if(terminal == null)
            setTerminal(new POSIXTerminal(), in);
        else
            setTerminal(terminal, in);

        if(mode == null)
            editMode = new EmacsEditMode();
        else
            editMode = mode;

        undoManager = new UndoManager();
        pasteManager = new PasteManager();
        buffer = new Buffer(null);
        history = new InMemoryHistory();

    }

    private void setTerminal(Terminal term, InputStream in) {
        terminal = term;
        terminal.init(in);
    }

    private void setOutWriter(Writer out) {
        outStream = out;
    }

    private void flushOut() throws IOException {
        outStream.flush();
    }

    public void reset() throws Exception {
        terminal.reset();
    }

    public void pushToConsole(String input) throws IOException {
        outStream.write(input);
        flushOut();
    }

    public void pushToConsole(char[] input) throws IOException {
        outStream.write(input);
        flushOut();
    }

    public String read(String prompt) throws IOException {

        buffer.reset(prompt);
        outStream.write(buffer.getPrompt());
        flushOut();
        StringBuilder searchTerm = null;
        StringBuilder result = null;

        while(true) {

            int c = terminal.read();
            //System.out.println("got int:"+c);
            if (c == -1) {
                return null;
            }
            Operation operation = editMode.parseInput(c);

            Action action = operation.getAction();

            if (action == Action.EDIT) {
                writeChar(c);
            }<commit_msg>make sure that we read from the beginning of a line<commit_after>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.jreadline.console;

import org.jboss.jreadline.edit.EditMode;
import org.jboss.jreadline.edit.EmacsEditMode;
import org.jboss.jreadline.edit.PasteManager;
import org.jboss.jreadline.edit.ViEditMode;
import org.jboss.jreadline.edit.actions.*;
import org.jboss.jreadline.history.History;
import org.jboss.jreadline.history.InMemoryHistory;
import org.jboss.jreadline.history.SearchDirection;
import org.jboss.jreadline.terminal.POSIXTerminal;
import org.jboss.jreadline.terminal.Terminal;
import org.jboss.jreadline.undo.UndoAction;
import org.jboss.jreadline.undo.UndoManager;

import java.io.*;

/**
 * A console reader.
 * Supports ansi terminals
 *
 * @author <NAME> <<EMAIL>>
 */
public class Console {

    private Writer outStream;
    private Buffer buffer;
    private Terminal terminal;

    private UndoManager undoManager;
    private PasteManager pasteManager;
    private EditMode editMode;
    private History history;

    private Action prevAction = Action.EDIT;

    private boolean toConsole = false;

    private static final String CR = System.getProperty("line.separator");

    public Console() throws IOException {
        this(new FileInputStream(FileDescriptor.in),
             new PrintWriter( new OutputStreamWriter(System.out)));

    }

    public Console(InputStream in, Writer out)  {
        this(in, out, null);
    }

    public Console(InputStream in, Writer out, Terminal terminal) {
        this(in, out, terminal, null);
    }

    public Console(InputStream in, Writer out, Terminal terminal, EditMode mode) {
        setOutWriter(out);
        if(terminal == null)
            setTerminal(new POSIXTerminal(), in);
        else
            setTerminal(terminal, in);

        if(mode == null)
            editMode = new EmacsEditMode();
        else
            editMode = mode;

        undoManager = new UndoManager();
        pasteManager = new PasteManager();
        buffer = new Buffer(null);
        history = new InMemoryHistory();

    }

    private void setTerminal(Terminal term, InputStream in) {
        terminal = term;
        terminal.init(in);
    }

    private void setOutWriter(Writer out) {
        outStream = out;
    }

    private void flushOut() throws IOException {
        outStream.flush();
    }

    public void reset() throws Exception {
        terminal.reset();
    }

    public void pushToConsole(String input) throws IOException {
        outStream.write(input);
        flushOut();
        toConsole = true;
    }

    public void pushToConsole(char[] input) throws IOException {
        outStream.write(input);
        flushOut();
        toConsole = true;
    }

    public String read(String prompt) throws IOException {
        //make sure that we end the line if pushToConsole(..) have been used
        if(toConsole) {
            toConsole = false;
            printNewline();
        }

        buffer.reset(prompt);
        outStream.write(buffer.getPrompt());
        flushOut();
        StringBuilder searchTerm = null;
        StringBuilder result = null;

        while(true) {

            int c = terminal.read();
            //System.out.println("got int:"+c);
            if (c == -1) {
                return null;
            }
            Operation operation = editMode.parseInput(c);

            Action action = operation.getAction();

            if (action == Action.EDIT) {
                writeChar(c);
            }
{'fraction_non_alphanumeric': 0.09225720189619545, 'fraction_numerical': 0.002187917831530327, 'mean_word_length': 2.6946564885496183, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4039649', 'n_tokens_mistral': 2285, 'n_tokens_neox': 2171, 'n_words': 684}