<commit_before>/*
 * Copyright 2003-2007 the original author or authors.
 *
 * 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.codehaus.groovy.tools.shell

import java.lang.reflect.Method

import groovy.text.MessageSource

import org.codehaus.groovy.runtime.InvokerHelper
import org.codehaus.groovy.runtime.InvokerInvocationException
import org.codehaus.groovy.runtime.MethodClosure

import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.control.CompilationFailedException

import org.codehaus.groovy.tools.shell.commands.*

/**
 * An interactive shell for evaluating Groovy code from the command-line (aka. groovysh).
 *
 * @version $Id$
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
class Groovysh
    extends Shell
{
    private static final String NEWLINE = System.properties['line.separator']
    
    private final MessageSource messages = new MessageSource(this.class)
    
    private final InteractiveShellRunner runner
    
    private final GroovyShell shell
    
    private final BufferManager buffers = new BufferManager()

    private final List imports = []
    
    Groovysh(final ClassLoader classLoader, final Binding binding, final IO io) {
        super(io)
        
        assert classLoader
        assert binding
        
        shell = new GroovyShell(classLoader, binding)
        
        //
        // NOTE: Command registration must be done after the shell is initialized and before the runner is created
        //
        
        registerCommands()
        
        runner = new InteractiveShellRunner(this)
        runner.prompt = this.&renderPrompt
    }

    Groovysh(final Binding binding, final IO io) {
        this(Thread.currentThread().contextClassLoader, binding, io)
    }

    Groovysh(final IO io) {
        this(new Binding(), io)
    }
    
    Groovysh() {
        this(new IO())
    }
    
    protected void registerCommands() {
        //
        // TODO: Add CommandSeperator for better visual grouping
        //

        //
        // TODO: Add properties-based (or simple xml) loading of commands & aliases ?
        //
        
        registry << new HelpCommand(this)

        alias('?', '\\?', 'help')

        registry << new ExitCommand(this)

        alias('quit', '\\q', 'exit')
        
        registry << new HistoryCommand(this)
        
        //
        // TODO: Rename to display-buffer, display-variables, display-classes, display-imports?
        //
        //       or one display command with args [buffer|variables|classes|imports]
        //
        
        registry << new DisplayCommand(this)
        
        registry << new ClearCommand(this)

        registry << new VariablesCommand(this)
        
        registry << new ClassesCommand(this)
        
        registry << new ImportCommand(this)
        
        registry << new ImportsCommand(this)
        
        registry << new InspectCommand(this)
        
        //
        // TODO: Create one purge command with args [variables|classes|imports|all] ?
        //
        
        registry << new PurgeVariablesCommand(this)
        
        registry << new PurgeClassesCommand(this)
        
        registry << new PurgeImportsCommand(this)
        
        registry << new EditCommand(this)
        
        registry << new LoadCommand(this)

        alias('.', '\\.', 'load')

        registry << new SaveCommand(this)

        registry << new BufferCommand(this)

        alias('#', '\\#', 'buffer')
    }
    
    protected String renderPrompt() {
        //
        // TODO: Create a fancy ANSI-color prompt thingy?
        //

        def buffer = buffers.current()
        def lineNum = formatLineNumber(buffer.size())

        return "groovy:(${buffers.selected}):${lineNum}> "
    }

    /**
     * Execute a single line, where the line may be a command or Groovy code (complete or incomplete).
     */
    Object execute(final String line) {
        assert line != null
        <commit_msg>yet another thing to do someday<commit_after>/*
 * Copyright 2003-2007 the original author or authors.
 *
 * 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.codehaus.groovy.tools.shell

import java.lang.reflect.Method

import groovy.text.MessageSource

import org.codehaus.groovy.runtime.InvokerHelper
import org.codehaus.groovy.runtime.InvokerInvocationException
import org.codehaus.groovy.runtime.MethodClosure

import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.control.CompilationFailedException

import org.codehaus.groovy.tools.shell.commands.*

/**
 * An interactive shell for evaluating Groovy code from the command-line (aka. groovysh).
 *
 * @version $Id$
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
class Groovysh
    extends Shell
{
    private static final String NEWLINE = System.properties['line.separator']
    
    private final MessageSource messages = new MessageSource(this.class)
    
    private final InteractiveShellRunner runner
    
    private final GroovyShell shell
    
    private final BufferManager buffers = new BufferManager()

    private final List imports = []
    
    Groovysh(final ClassLoader classLoader, final Binding binding, final IO io) {
        super(io)
        
        assert classLoader
        assert binding
        
        shell = new GroovyShell(classLoader, binding)
        
        //
        // NOTE: Command registration must be done after the shell is initialized and before the runner is created
        //
        
        registerCommands()
        
        runner = new InteractiveShellRunner(this)
        runner.prompt = this.&renderPrompt
    }

    Groovysh(final Binding binding, final IO io) {
        this(Thread.currentThread().contextClassLoader, binding, io)
    }

    Groovysh(final IO io) {
        this(new Binding(), io)
    }
    
    Groovysh() {
        this(new IO())
    }
    
    protected void registerCommands() {
        //
        // TODO: Add CommandSeperator for better visual grouping
        //

        //
        // TODO: Add properties-based (or simple xml) loading of commands & aliases ?
        //
        
        registry << new HelpCommand(this)

        alias('?', '\\?', 'help')

        registry << new ExitCommand(this)

        alias('quit', '\\q', 'exit')
        
        registry << new HistoryCommand(this)
        
        //
        // TODO: Rename to display-buffer, display-variables, display-classes, display-imports?
        //
        //       or one display command with args [buffer|variables|classes|imports]
        //
        
        registry << new DisplayCommand(this)
        
        registry << new ClearCommand(this)

        registry << new VariablesCommand(this)
        
        registry << new ClassesCommand(this)
        
        registry << new ImportCommand(this)
        
        registry << new ImportsCommand(this)
        
        registry << new InspectCommand(this)
        
        //
        // TODO: Create one purge command with args [variables|classes|imports|all] ?
        //
        
        registry << new PurgeVariablesCommand(this)
        
        registry << new PurgeClassesCommand(this)
        
        registry << new PurgeImportsCommand(this)
        
        registry << new EditCommand(this)
        
        registry << new LoadCommand(this)

        alias('.', '\\.', 'load')

        registry << new SaveCommand(this)

        registry << new BufferCommand(this)

        alias('#', '\\#', 'buffer')
        
        //
        // TODO: Add a command to record buffer activity
        //
    }
    
    protected String renderPrompt() {
        //
        // TODO: Create a fancy ANSI-color prompt thingy?
        //

        def buffer = buffers.current()
        def lineNum = formatLineNumber(buffer.size())

        return "groovy:(${buffers.selected}):${lineNum}> "
    }

    /**
     * Execute a single line, where the line may be a command or Groovy code (complete or incomplete).
     */
    Object execute(final String line) {
        assert line != null
        
{'fraction_non_alphanumeric': 0.09388069557561082, 'fraction_numerical': 0.0026414263702399295, 'mean_word_length': 2.206422018348624, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '2365970', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2347, 'n_words': 729}