<commit_before>/*******************************************************************************
 * Copyright (c) 2012-2015 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.ide.extension.machine.client.outputspanel.console;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Style;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SimpleHtmlSanitizer;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

/**
 * View for {@link OutputConsole}.
 *
 * @author <NAME>
 */
public class OutputConsoleViewImpl extends Composite implements OutputConsoleView {

    private static final OutputConsoleViewUiBinder UI_BINDER = GWT.create(OutputConsoleViewUiBinder.class);

    private static final String PRE_STYLE = "style='margin:0px;'";

    private ActionDelegate delegate;

    /** scroll events to the bottom if view is visible */
    private boolean scrollBottomRequired = false;

    @UiField
    ScrollPanel scrollPanel;
    @UiField
    FlowPanel   consoleArea;

    /** If true - next printed line should replace the previous one. */
    private boolean carriageReturn;

    @Inject
    public OutputConsoleViewImpl() {
        initWidget(UI_BINDER.createAndBindUi(this));
    }

    @Override
    public void setDelegate(ActionDelegate delegate) {
        this.delegate = delegate;
    }

    @Override
    public void printCommandLine(String commandLine) {
        final HTML html = new HTML(buildSafeHtmlMessage(commandLine));
        html.getElement().getStyle().setPaddingLeft(2, Style.Unit.PX);
        html.getElement().getStyle().setColor("gray");
        consoleArea.add(html);
    }

    @Override
    public void print(String message, boolean cr) {
        final HTML html = new HTML(buildSafeHtmlMessage(message));
        html.getElement().getStyle().setPaddingLeft(2, Style.Unit.PX);

        if (carriageReturn) {
            consoleArea.remove(consoleArea.getWidgetCount() - 1);
            carriageReturn = false;
        }

        carriageReturn = cr;
        consoleArea.add(html);
    }

    /** Return sanitized message (with all restricted HTML-tags escaped) in {@link SafeHtml}. */
    private SafeHtml buildSafeHtmlMessage(String message) {
        return new SafeHtmlBuilder()
                .appendHtmlConstant("<pre " + PRE_STYLE + ">")
                .append(SimpleHtmlSanitizer.sanitizeHtml(message))
                .appendHtmlConstant("</pre>")
                .toSafeHtml();
    }

    @Override
    public void scrollBottom() {
        /** scroll bottom immediately if view is visible */
        if (scrollPanel.getElement().getOffsetParent() != null) {
            scrollPanel.getElement().setScrollTop(scrollPanel.getElement().getScrollHeight());
            return;
        }

        /** otherwise, check the visibility periodically and scroll the view when it's visible */
        if (!scrollBottomRequired) {
            scrollBottomRequired = true;
<commit_msg>Empty line is not printed in Outputs panel<commit_after>/*******************************************************************************
 * Copyright (c) 2012-2015 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   <NAME>. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.ide.extension.machine.client.outputspanel.console;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Style;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SimpleHtmlSanitizer;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

/**
 * View for {@link OutputConsole}.
 *
 * @author <NAME>
 */
public class OutputConsoleViewImpl extends Composite implements OutputConsoleView {

    private static final OutputConsoleViewUiBinder UI_BINDER = GWT.create(OutputConsoleViewUiBinder.class);

    private static final String PRE_STYLE = "style='margin:0px;'";

    private ActionDelegate delegate;

    /** scroll events to the bottom if view is visible */
    private boolean scrollBottomRequired = false;

    @UiField
    ScrollPanel scrollPanel;
    @UiField
    FlowPanel   consoleArea;

    /** If true - next printed line should replace the previous one. */
    private boolean carriageReturn;

    @Inject
    public OutputConsoleViewImpl() {
        initWidget(UI_BINDER.createAndBindUi(this));
    }

    @Override
    public void setDelegate(ActionDelegate delegate) {
        this.delegate = delegate;
    }

    @Override
    public void printCommandLine(String commandLine) {
        final HTML html = new HTML(buildSafeHtmlMessage(commandLine));
        html.getElement().getStyle().setPaddingLeft(2, Style.Unit.PX);
        html.getElement().getStyle().setColor("gray");
        consoleArea.add(html);
    }

    @Override
    public void print(String message, boolean cr) {
        final HTML html = new HTML(buildSafeHtmlMessage(message));
        html.getElement().getStyle().setPaddingLeft(2, Style.Unit.PX);

        if (carriageReturn) {
            consoleArea.remove(consoleArea.getWidgetCount() - 1);
            carriageReturn = false;
        }

        carriageReturn = cr;
        consoleArea.add(html);
    }

    /** Return sanitized message (with all restricted HTML-tags escaped) in {@link SafeHtml}. */
    private SafeHtml buildSafeHtmlMessage(String message) {
        return new SafeHtmlBuilder()
                .appendHtmlConstant("<pre " + PRE_STYLE + ">")
                .append(SimpleHtmlSanitizer.sanitizeHtml(message.isEmpty() ? " " : message))
                .appendHtmlConstant("</pre>")
                .toSafeHtml();
    }

    @Override
    public void scrollBottom() {
        /** scroll bottom immediately if view is visible */
        if (scrollPanel.getElement().getOffsetParent() != null) {
            scrollPanel.getElement().setScrollTop(scrollPanel.getElement().getScrollHeight());
            return;
        }

        /** otherwise, check the visibility periodically and scroll the view when it's visible */
        if (!scrollBottomRequired) {
            scrollBottomRequired = true;

{'fraction_non_alphanumeric': 0.14365005946874587, 'fraction_numerical': 0.00422888859521607, 'mean_word_length': 3.384704519119351, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1800193', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2012, 'n_words': 478}