<commit_before>/*
 * Copyright (C) 2009 the original author(s).
 *
 * 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.sonatype.gshell.command;

import org.fusesource.jansi.AnsiRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonatype.gshell.branding.Branding;
import org.sonatype.gshell.io.PrefixingStream;
import org.sonatype.gshell.shell.ShellHolder;
import org.sonatype.gshell.util.ReplacementParser;
import org.sonatype.gshell.util.cli2.CliProcessor;
import org.sonatype.gshell.util.cli2.HelpPrinter;
import org.sonatype.gshell.util.i18n.AggregateMessageSource;
import org.sonatype.gshell.util.i18n.MessageSource;
import org.sonatype.gshell.util.i18n.PrefixingMessageSource;
import org.sonatype.gshell.util.i18n.ResourceBundleMessageSource;
import org.sonatype.gshell.util.pref.PreferenceDescriptor;
import org.sonatype.gshell.util.pref.PreferenceProcessor;
import org.sonatype.gshell.vars.Variables;

import java.io.PrintStream;

/**
 * The default {@link CommandDocumenter} component.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @since 2.0
 */
public class CommandDocumenterImpl
    implements CommandDocumenter
{
    private final Logger log = LoggerFactory.getLogger(getClass());

    private final MessageSource messages = new ResourceBundleMessageSource(getClass());

    private String evaluate(final CommandAction command, final String input) {
        if (input.contains("${")) {
            ReplacementParser parser = new ReplacementParser()
            {
                @Override
                protected Object replace(final String key) throws Exception {
                    Object rep = null;
                    if (key.equals("command.name")) {
                        rep = command.getName();
                    }

                    if (rep == null) {
                        Variables vars = ShellHolder.get().getVariables();
                        rep = vars.get(key);
                    }

                    if (rep == null) {
                        rep = System.getProperty(key);
                    }
                    return rep;
                }
            };

            return parser.parse(input);

        }

        return input;
    }

    public String getDescription(final CommandAction command) {
        assert command != null;

        String text = command.getMessages().getMessage(COMMAND_DESCRIPTION);
        return evaluate(command, text);
    }

    public String getManual(final CommandAction command) {
        assert command != null;

        String text = command.getMessages().getMessage(COMMAND_MANUAL);
        return evaluate(command, text);
    }

    public void renderUsage(final CommandAction command, final IO io) {
        assert command != null;
        assert io != null;

        log.trace("Rendering command usage");

        CliProcessor clp = new CliProcessor();

        // Attach our helper to inject --help
        CommandHelpSupport help = new CommandHelpSupport();
        clp.addBean(help);

        // And then the beans options
        clp.addBean(command);

        // Render the help
        io.out.println(getDescription(command));
        io.out.println();

        HelpPrinter printer = new HelpPrinter(clp);
        AggregateMessageSource messages = new AggregateMessageSource(command.getMessages(), help.getMessages());
        printer.addMessages(new PrefixingMessageSource(messages, COMMAND_DOT));
        printer.printUsage(io.out, command.getName());
    }

    public void renderManual(final CommandAction command, final IO io) {
        assert command != null;
        assert io != null;

        log.trace("Rendering command manual");

        PrintStream out = new PrintStream(new PrefixingStream("    ", io.streams.out));

        io.out.format("@|bold %s|@", messages.getMessage("section.name")).println();
        io.out.println();
        out.println(command.getName());
        io.out.println();

        String text;

        io.out.format("@|bold %s|@", messages.getMessage("section.description")).println();
        text = getDescription(command);
        out.println();
        out.println(AnsiRenderer.render(text));
        io.out.println();

        text = getManual(command);
        if (text != null && text.trim().length() != 0) {
            io.out.format("@|bold %s|@", messages.getMessage("section.manual")).println();
            out.println();
            out.println(AnsiRenderer.render(text));
            io.out.println();
        }

        PreferenceProcessor pp = new PreferenceProcessor();
        Branding branding = ShellHolder.get().getBranding();
        pp.setBasePath(branding.getPreferencesBasePath());
        pp.addBean(command);

        if (!pp.getDescriptors().isEmpty()) {
            io.out.format("@|bold %s|@", messages.getMessage("section.preferences")).println();
            out.println();

            for (PreferenceDescriptor pd : pp.getDescriptors()) {
                text = String.format("%s: @|bold %s|@ (%s)", pd.getPreferences().absolutePath(), pd.getId(), pd.getSetter().getType().getSimpleName());
                out.println(AnsiRenderer.render(text));
            }

            io.out.println();
        }
    }
}<commit_msg>Drop ':' in prefs doc, so users can copy/paste<commit_after>/*
 * Copyright (C) 2009 the original author(s).
 *
 * 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.sonatype.gshell.command;

import org.fusesource.jansi.AnsiRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonatype.gshell.branding.Branding;
import org.sonatype.gshell.io.PrefixingStream;
import org.sonatype.gshell.shell.ShellHolder;
import org.sonatype.gshell.util.ReplacementParser;
import org.sonatype.gshell.util.cli2.CliProcessor;
import org.sonatype.gshell.util.cli2.HelpPrinter;
import org.sonatype.gshell.util.i18n.AggregateMessageSource;
import org.sonatype.gshell.util.i18n.MessageSource;
import org.sonatype.gshell.util.i18n.PrefixingMessageSource;
import org.sonatype.gshell.util.i18n.ResourceBundleMessageSource;
import org.sonatype.gshell.util.pref.PreferenceDescriptor;
import org.sonatype.gshell.util.pref.PreferenceProcessor;
import org.sonatype.gshell.vars.Variables;

import java.io.PrintStream;

/**
 * The default {@link CommandDocumenter} component.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @since 2.0
 */
public class CommandDocumenterImpl
    implements CommandDocumenter
{
    private final Logger log = LoggerFactory.getLogger(getClass());

    private final MessageSource messages = new ResourceBundleMessageSource(getClass());

    private String evaluate(final CommandAction command, final String input) {
        if (input.contains("${")) {
            ReplacementParser parser = new ReplacementParser()
            {
                @Override
                protected Object replace(final String key) throws Exception {
                    Object rep = null;
                    if (key.equals("command.name")) {
                        rep = command.getName();
                    }

                    if (rep == null) {
                        Variables vars = ShellHolder.get().getVariables();
                        rep = vars.get(key);
                    }

                    if (rep == null) {
                        rep = System.getProperty(key);
                    }
                    return rep;
                }
            };

            return parser.parse(input);

        }

        return input;
    }

    public String getDescription(final CommandAction command) {
        assert command != null;

        String text = command.getMessages().getMessage(COMMAND_DESCRIPTION);
        return evaluate(command, text);
    }

    public String getManual(final CommandAction command) {
        assert command != null;

        String text = command.getMessages().getMessage(COMMAND_MANUAL);
        return evaluate(command, text);
    }

    public void renderUsage(final CommandAction command, final IO io) {
        assert command != null;
        assert io != null;

        log.trace("Rendering command usage");

        CliProcessor clp = new CliProcessor();

        // Attach our helper to inject --help
        CommandHelpSupport help = new CommandHelpSupport();
        clp.addBean(help);

        // And then the beans options
        clp.addBean(command);

        // Render the help
        io.out.println(getDescription(command));
        io.out.println();

        HelpPrinter printer = new HelpPrinter(clp);
        AggregateMessageSource messages = new AggregateMessageSource(command.getMessages(), help.getMessages());
        printer.addMessages(new PrefixingMessageSource(messages, COMMAND_DOT));
        printer.printUsage(io.out, command.getName());
    }

    public void renderManual(final CommandAction command, final IO io) {
        assert command != null;
        assert io != null;

        log.trace("Rendering command manual");

        PrintStream out = new PrintStream(new PrefixingStream("    ", io.streams.out));

        io.out.format("@|bold %s|@", messages.getMessage("section.name")).println();
        io.out.println();
        out.println(command.getName());
        io.out.println();

        String text;

        io.out.format("@|bold %s|@", messages.getMessage("section.description")).println();
        text = getDescription(command);
        out.println();
        out.println(AnsiRenderer.render(text));
        io.out.println();

        text = getManual(command);
        if (text != null && text.trim().length() != 0) {
            io.out.format("@|bold %s|@", messages.getMessage("section.manual")).println();
            out.println();
            out.println(AnsiRenderer.render(text));
            io.out.println();
        }

        PreferenceProcessor pp = new PreferenceProcessor();
        Branding branding = ShellHolder.get().getBranding();
        pp.setBasePath(branding.getPreferencesBasePath());
        pp.addBean(command);

        if (!pp.getDescriptors().isEmpty()) {
            io.out.format("@|bold %s|@", messages.getMessage("section.preferences")).println();
            out.println();

            for (PreferenceDescriptor pd : pp.getDescriptors()) {
                text = String.format("%s @|bold %s|@ (%s)", pd.getPreferences().absolutePath(), pd.getId(), pd.getSetter().getType().getSimpleName());
                out.println(AnsiRenderer.render(text));
            }

            io.out.println();
        }
    }
}
{'fraction_non_alphanumeric': 0.11688086141021188, 'fraction_numerical': 0.00399444251476207, 'mean_word_length': 2.5502466091245375, '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': '6650818', 'n_tokens_mistral': 3136, 'n_tokens_neox': 3043, 'n_words': 729}