<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.geronimo.gshell.commands.builtins;

import java.util.Collection;

import org.apache.geronimo.gshell.ansi.Code;
import org.apache.geronimo.gshell.ansi.Renderer;
import org.apache.geronimo.gshell.branding.Branding;
import org.apache.geronimo.gshell.clp.Argument;
import org.apache.geronimo.gshell.command.Command;
import org.apache.geronimo.gshell.command.CommandSupport;
import org.apache.geronimo.gshell.command.annotation.CommandComponent;
import org.apache.geronimo.gshell.command.annotation.Requirement;
import org.apache.geronimo.gshell.layout.LayoutManager;
import org.apache.geronimo.gshell.registry.CommandRegistry;
import org.codehaus.plexus.util.StringUtils;

/**
 * Display help
 *
 * @version $Rev$ $Date$
 */
@CommandComponent(id="gshell-builtins:help", description="Show command help")
public class HelpCommand
    extends CommandSupport
{
    @Requirement
    private CommandRegistry commandRegistry;

    @Requirement
    private LayoutManager layoutManager;

    @Requirement
    private Branding branding;

    @Argument(description = "Command name")
    private String command;

    private Renderer renderer = new Renderer();

    protected Object doExecute() throws Exception {
        assert commandRegistry != null;
        assert layoutManager != null;

        if (command == null) {
            io.out.println();
            io.out.print(branding.getAbout());
            io.out.println();

            io.out.println("Available commands:");

            Collection<Command> commands = commandRegistry.commands();

            // Figure out the maximum length of a command name
            int maxNameLen = 0;
            for (Command desc : commands) {
                if (desc.getId().length() > maxNameLen) {
                    maxNameLen = desc.getId().length();
                }
            }

            //
            // TODO: Need to ask the LayoutManager...
            //

            for (Command d : commands) {
                // Hide commands if they don't have descriptions
                String name = d.getId();
                name = StringUtils.rightPad(name, maxNameLen);

                io.out.print("  ");
                io.out.print(renderer.render(Renderer.encode(name, Code.BOLD)));

                String desc = d.getDescription();

                if (desc != null) {
                    io.out.print("  ");
                    io.out.println(desc);
                }
                else {
                    io.out.println();
                }
            }
        }
        else {
            Command cmd = commandRegistry.lookup(command);
            if (cmd == null) {
                io.out.println("Command " + Renderer.encode(command, Code.BOLD) + " not found.");
                io.out.println("Try " + Renderer.encode("help", Code.BOLD) + " for a list of available commands.");
            }
            else {
                io.out.println("Command " + Renderer.encode(command, Code.BOLD));
                io.out.println("   " + cmd.getDescription());
            }
        }

        //
        // TODO: Need to figure out a simple way to access i18n text for the command's help
        //
        
        /*
        io.out.println();
        io.out.println("For help on a specific command type:");
        io.out.println("    help <command>");
        */

        io.out.println();

        return SUCCESS;
    }
}<commit_msg>Split up into methods<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.geronimo.gshell.commands.builtins;

import java.util.Collection;

import org.apache.geronimo.gshell.ansi.Code;
import org.apache.geronimo.gshell.ansi.Renderer;
import org.apache.geronimo.gshell.branding.Branding;
import org.apache.geronimo.gshell.clp.Argument;
import org.apache.geronimo.gshell.command.Command;
import org.apache.geronimo.gshell.command.CommandSupport;
import org.apache.geronimo.gshell.command.annotation.CommandComponent;
import org.apache.geronimo.gshell.command.annotation.Requirement;
import org.apache.geronimo.gshell.layout.LayoutManager;
import org.apache.geronimo.gshell.registry.CommandRegistry;
import org.codehaus.plexus.util.StringUtils;

/**
 * Display help
 *
 * @version $Rev$ $Date$
 */
@CommandComponent(id="gshell-builtins:help", description="Show command help")
public class HelpCommand
    extends CommandSupport
{
    @Requirement
    private CommandRegistry commandRegistry;

    @Requirement
    private LayoutManager layoutManager;

    @Requirement
    private Branding branding;

    @Argument(description = "Command name")
    private String command;

    private Renderer renderer = new Renderer();

    protected Object doExecute() throws Exception {
        io.out.println();

        if (command == null) {
            displayCommands();
        }
        else {
            displayCommandHelp(command);
        }

        io.out.println();

        return SUCCESS;
    }

    private void displayCommands() throws Exception {
        io.out.print(branding.getAbout());
        io.out.println();

        io.out.println("Available commands:");

        Collection<Command> commands = commandRegistry.commands();

        // Figure out the maximum length of a command name
        int maxNameLen = 0;
        for (Command desc : commands) {
            if (desc.getId().length() > maxNameLen) {
                maxNameLen = desc.getId().length();
            }
        }

        //
        // TODO: Need to ask the LayoutManager...
        //

        for (Command d : commands) {
            // Hide commands if they don't have descriptions
            String name = d.getId();
            name = StringUtils.rightPad(name, maxNameLen);

            io.out.print("  ");
            io.out.print(renderer.render(Renderer.encode(name, Code.BOLD)));

            String desc = d.getDescription();

            if (desc != null) {
                io.out.print("  ");
                io.out.println(desc);
            }
            else {
                io.out.println();
            }
        }
    }
    private void displayCommandHelp(final String path) throws Exception {
        assert path != null;

        Command cmd = commandRegistry.lookup(path);

        if (cmd == null) {
            io.out.println("Command " + Renderer.encode(path, Code.BOLD) + " not found.");
            io.out.println("Try " + Renderer.encode("help", Code.BOLD) + " for a list of available commands.");
        }
        else {
            io.out.println("Command " + Renderer.encode(path, Code.BOLD));
            io.out.println("   " + cmd.getDescription());
        }
    }
}
{'fraction_non_alphanumeric': 0.10478276743336984, 'fraction_numerical': 0.0014603870025556773, 'mean_word_length': 2.300401606425703, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, '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': '1571838', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2153, 'n_words': 611}