<commit_before>/*
 * Copyright 2012 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.jboss.aesh.console;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jboss.aesh.cl.CommandLine;
import org.jboss.aesh.cl.exception.CommandLineParserException;
import org.jboss.aesh.cl.parser.CommandLineCompletionParser;
import org.jboss.aesh.cl.parser.ParsedCompleteObject;
import org.jboss.aesh.cl.validator.CommandValidatorException;
import org.jboss.aesh.cl.validator.OptionValidatorException;
import org.jboss.aesh.complete.CompleteOperation;
import org.jboss.aesh.complete.Completion;
import org.jboss.aesh.console.command.CommandNotFoundException;
import org.jboss.aesh.console.command.CommandResult;
import org.jboss.aesh.console.command.completer.CompleterInvocationProvider;
import org.jboss.aesh.console.command.container.CommandContainer;
import org.jboss.aesh.console.command.converter.ConverterInvocationProvider;
import org.jboss.aesh.console.command.invocation.AeshCommandInvocation;
import org.jboss.aesh.console.command.invocation.CommandInvocationProvider;
import org.jboss.aesh.console.command.invocation.CommandInvocationServices;
import org.jboss.aesh.console.command.registry.AeshInternalCommandRegistry;
import org.jboss.aesh.console.command.registry.CommandRegistry;
import org.jboss.aesh.console.command.validator.ValidatorInvocationProvider;
import org.jboss.aesh.console.export.ExportManager;
import org.jboss.aesh.console.helper.ManProvider;
import org.jboss.aesh.console.man.Man;
import org.jboss.aesh.console.settings.CommandNotFoundHandler;
import org.jboss.aesh.console.settings.Settings;
import org.jboss.aesh.parser.Parser;
import org.jboss.aesh.terminal.Shell;
import org.jboss.aesh.util.LoggerUtil;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class AeshConsoleImpl implements AeshConsole {

    private final Console console;
    private final CommandRegistry registry;
    private final CommandInvocationServices commandInvocationServices;
    private final InvocationProviders invocationProviders;

    private final Logger logger = LoggerUtil.getLogger(AeshConsoleImpl.class
        .getName());
    private final ManProvider manProvider;
    private final CommandNotFoundHandler commandNotFoundHandler;
    private AeshInternalCommandRegistry internalRegistry;
    private String commandInvocationProvider = CommandInvocationServices.DEFAULT_PROVIDER_NAME;

    AeshConsoleImpl(Settings settings, CommandRegistry registry,
        CommandInvocationServices commandInvocationServices,
        CommandNotFoundHandler commandNotFoundHandler,
        CompleterInvocationProvider completerInvocationProvider,
        ConverterInvocationProvider converterInvocationProvider,
        ValidatorInvocationProvider validatorInvocationProvider,
        ManProvider manProvider) {
        this.registry = registry;
        this.commandInvocationServices = commandInvocationServices;
        this.commandNotFoundHandler = commandNotFoundHandler;
        this.manProvider = manProvider;
        this.invocationProviders =
            new AeshInvocationProviders(converterInvocationProvider, completerInvocationProvider,
                validatorInvocationProvider);

        console = new Console(settings);
        console.setConsoleCallback(new AeshConsoleCallbackImpl(this));
        console.addCompletion(new AeshCompletion());
        processSettings(settings);
    }

    @Override
    public void start() {
        console.start();
    }

    @Override
    public void stop() {
        console.stop();
    }

    @Override
    public CommandRegistry getCommandRegistry() {
        return registry;
    }

    @Override
    public void setPrompt(Prompt prompt) {
        console.setPrompt(prompt);
    }

    @Override
    public Prompt getPrompt() {
        return console.getPrompt();
    }

    @Override
    public Shell getShell() {
        return console.getShell();
    }

    @Override
    public void clear() {
        try {
            console.clear();
        }
        catch (IOException ignored) {
        }
    }

    @Override
    public String getHelpInfo(String commandName) {
        try (CommandContainer commandContainer = registry.getCommand(commandName, "")) {
            if (commandContainer != null)
                return commandContainer.getParser().printHelp();
        }
        catch (Exception e) { // ignored
        }
        return "";
    }

    @Override
    public void setCurrentCommandInvocationProvider(String name) {
        this.commandInvocationProvider = name;
    }

    @Override
    public void registerCommandInvocationProvider(String name,
        CommandInvocationProvider commandInvocationProvider) {
        commandInvocationServices.registerProvider(name,
            commandInvocationProvider);
    }

    @Override
    public ManProvider getManProvider() {
        return manProvider;
    }

    @Override
    public AeshContext getAeshContext() {
        return console.getAeshContext();
    }

    @Override
    public boolean isRunning() {
        return console.isRunning();
    }

    @Override
    public ExportManager getExportManager() {
        return console.getExportManager();
    }

    public String getBuffer() {
        return console.getBuffer();
    }

    private void processSettings(Settings settings) {
        if (settings.isManEnabled()) {
            internalRegistry = new AeshInternalCommandRegistry();
            internalRegistry.addCommand(new Man(manProvider));
        }
    }

    private List<String> completeCommandName(String input) {
        List<String> matchedCommands = new ArrayList<>();
        try {
            Set<String> allCommandNames = new TreeSet<>();
            if(registry.getAllCommandNames() != null)
                allCommandNames.addAll(registry.getAllCommandNames());
            if (internalRegistry != null) {
                allCommandNames.addAll(internalRegistry.getAllCommandNames());
            }
            for (String commandName : allCommandNames) {
                if (commandName.startsWith(input))
                    matchedCommands.add(commandName);
            }
        }
        catch (Exception e) {
            logger.log(Level.SEVERE,
                    "Error retrieving command names from CommandRegistry", e);
        }
<commit_msg>optimize fetching command names from registry so forge2 doesnt blow up :)<commit_after>/*
 * Copyright 2012 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.jboss.aesh.console;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jboss.aesh.cl.CommandLine;
import org.jboss.aesh.cl.exception.CommandLineParserException;
import org.jboss.aesh.cl.parser.CommandLineCompletionParser;
import org.jboss.aesh.cl.parser.ParsedCompleteObject;
import org.jboss.aesh.cl.validator.CommandValidatorException;
import org.jboss.aesh.cl.validator.OptionValidatorException;
import org.jboss.aesh.complete.CompleteOperation;
import org.jboss.aesh.complete.Completion;
import org.jboss.aesh.console.command.CommandNotFoundException;
import org.jboss.aesh.console.command.CommandResult;
import org.jboss.aesh.console.command.completer.CompleterInvocationProvider;
import org.jboss.aesh.console.command.container.CommandContainer;
import org.jboss.aesh.console.command.converter.ConverterInvocationProvider;
import org.jboss.aesh.console.command.invocation.AeshCommandInvocation;
import org.jboss.aesh.console.command.invocation.CommandInvocationProvider;
import org.jboss.aesh.console.command.invocation.CommandInvocationServices;
import org.jboss.aesh.console.command.registry.AeshInternalCommandRegistry;
import org.jboss.aesh.console.command.registry.CommandRegistry;
import org.jboss.aesh.console.command.validator.ValidatorInvocationProvider;
import org.jboss.aesh.console.export.ExportManager;
import org.jboss.aesh.console.helper.ManProvider;
import org.jboss.aesh.console.man.Man;
import org.jboss.aesh.console.settings.CommandNotFoundHandler;
import org.jboss.aesh.console.settings.Settings;
import org.jboss.aesh.parser.Parser;
import org.jboss.aesh.terminal.Shell;
import org.jboss.aesh.util.LoggerUtil;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class AeshConsoleImpl implements AeshConsole {

    private final Console console;
    private final CommandRegistry registry;
    private final CommandInvocationServices commandInvocationServices;
    private final InvocationProviders invocationProviders;

    private final Logger logger = LoggerUtil.getLogger(AeshConsoleImpl.class
        .getName());
    private final ManProvider manProvider;
    private final CommandNotFoundHandler commandNotFoundHandler;
    private AeshInternalCommandRegistry internalRegistry;
    private String commandInvocationProvider = CommandInvocationServices.DEFAULT_PROVIDER_NAME;

    AeshConsoleImpl(Settings settings, CommandRegistry registry,
        CommandInvocationServices commandInvocationServices,
        CommandNotFoundHandler commandNotFoundHandler,
        CompleterInvocationProvider completerInvocationProvider,
        ConverterInvocationProvider converterInvocationProvider,
        ValidatorInvocationProvider validatorInvocationProvider,
        ManProvider manProvider) {
        this.registry = registry;
        this.commandInvocationServices = commandInvocationServices;
        this.commandNotFoundHandler = commandNotFoundHandler;
        this.manProvider = manProvider;
        this.invocationProviders =
            new AeshInvocationProviders(converterInvocationProvider, completerInvocationProvider,
                validatorInvocationProvider);

        console = new Console(settings);
        console.setConsoleCallback(new AeshConsoleCallbackImpl(this));
        console.addCompletion(new AeshCompletion());
        processSettings(settings);
    }

    @Override
    public void start() {
        console.start();
    }

    @Override
    public void stop() {
        console.stop();
    }

    @Override
    public CommandRegistry getCommandRegistry() {
        return registry;
    }

    @Override
    public void setPrompt(Prompt prompt) {
        console.setPrompt(prompt);
    }

    @Override
    public Prompt getPrompt() {
        return console.getPrompt();
    }

    @Override
    public Shell getShell() {
        return console.getShell();
    }

    @Override
    public void clear() {
        try {
            console.clear();
        }
        catch (IOException ignored) {
        }
    }

    @Override
    public String getHelpInfo(String commandName) {
        try (CommandContainer commandContainer = registry.getCommand(commandName, "")) {
            if (commandContainer != null)
                return commandContainer.getParser().printHelp();
        }
        catch (Exception e) { // ignored
        }
        return "";
    }

    @Override
    public void setCurrentCommandInvocationProvider(String name) {
        this.commandInvocationProvider = name;
    }

    @Override
    public void registerCommandInvocationProvider(String name,
        CommandInvocationProvider commandInvocationProvider) {
        commandInvocationServices.registerProvider(name,
            commandInvocationProvider);
    }

    @Override
    public ManProvider getManProvider() {
        return manProvider;
    }

    @Override
    public AeshContext getAeshContext() {
        return console.getAeshContext();
    }

    @Override
    public boolean isRunning() {
        return console.isRunning();
    }

    @Override
    public ExportManager getExportManager() {
        return console.getExportManager();
    }

    public String getBuffer() {
        return console.getBuffer();
    }

    private void processSettings(Settings settings) {
        if (settings.isManEnabled()) {
            internalRegistry = new AeshInternalCommandRegistry();
            internalRegistry.addCommand(new Man(manProvider));
        }
    }

    private List<String> completeCommandName(String input) {
        List<String> matchedCommands = new ArrayList<>();
        try {
            Set<String> allCommandNames = new TreeSet<>();
            Set<String> registryCommandNames = registry.getAllCommandNames();
            if(registryCommandNames != null)
                allCommandNames.addAll(registryCommandNames);
            if (internalRegistry != null) {
                allCommandNames.addAll(internalRegistry.getAllCommandNames());
            }
            for (String commandName : allCommandNames) {
                if (commandName.startsWith(input))
                    matchedCommands.add(commandName);
            }
        }
        catch (Exception e) {
            logger.log(Level.SEVERE,
                    "Error retrieving command names from CommandRegistry", e);
        }

{'fraction_non_alphanumeric': 0.08551444043321299, 'fraction_numerical': 0.0012785800240673888, 'mean_word_length': 3.307418205377389, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, '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': '5933504', 'n_tokens_mistral': 3565, 'n_tokens_neox': 3431, 'n_words': 670}